1 ! Copyright (C) 2004, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: math kernel math.constants math.private math.bits
4 math.libm combinators math.order sequences ;
7 : >fraction ( a/b -- a b )
8 [ numerator ] [ denominator ] bi ; inline
12 : (rect>) ( x y -- z )
13 dup 0 = [ drop ] [ <complex> ] if ; inline
18 2dup [ real? ] both? [
21 "Complex number must have real components" throw
24 GENERIC: sqrt ( x -- y ) foldable
27 >float dup 0.0 < [ neg fsqrt 0.0 swap rect> ] [ fsqrt ] if ;
29 : factor-2s ( n -- r s )
30 #! factor an integer into 2^r * s
32 0 swap [ dup even? ] [ [ 1+ ] [ 2/ ] bi* ] [ ] while
37 GENERIC# ^n 1 ( z w -- z^w )
40 make-bits 1 [ [ dupd * ] when [ sq ] dip ] reduce nip ; inline
43 [ factor-2s ] dip [ (^n) ] keep rot * shift ;
46 [ >fraction ] dip tuck [ ^n ] 2bi@ / ;
51 : integer^ ( x y -- z )
52 dup 0 > [ ^n ] [ neg ^n recip ] if ; inline
57 [ real-part ] [ imaginary-part ] bi ; inline
59 : >float-rect ( z -- x y )
60 >rect [ >float ] bi@ ; inline
62 : >polar ( z -- abs arg )
63 >float-rect [ [ sq ] bi@ + fsqrt ] [ swap fatan2 ] 2bi ; inline
65 : cis ( arg -- z ) dup fcos swap fsin rect> ; inline
67 : polar> ( abs arg -- z ) cis * ; inline
71 : ^mag ( w abs arg -- magnitude )
72 [ >float-rect swap ] [ swap fpow ] [ rot * fexp /f ] tri* ; inline
74 : ^theta ( w abs arg -- theta )
75 [ >float-rect ] [ flog * swap ] [ * + ] tri* ; inline
77 : ^complex ( x y -- z )
78 swap >polar [ ^mag ] [ ^theta ] 3bi polar> ; inline
81 2dup [ real? ] both? [ drop 0 >= ] [ 2drop f ] if ; inline
84 dup zero? [ drop 0./0. ] [ 0 < 1./0. 0 ? ] if ; inline
86 : (^mod) ( n x y -- z )
88 [ dupd * pick mod ] when [ sq over mod ] dip
89 ] reduce 2nip ; inline
91 : (gcd) ( b a x y -- a d )
95 swap [ /mod [ over * swapd - ] dip ] keep (gcd)
102 { [ over zero? ] [ nip 0^ ] }
103 { [ dup integer? ] [ integer^ ] }
104 { [ 2dup real^? ] [ fpow ] }
109 [ 0 1 ] 2dip (gcd) dup 0 < [ neg ] when ; foldable
112 [ * ] 2keep gcd nip /i ; foldable
114 : mod-inv ( x n -- y )
115 [ nip ] [ gcd 1 = ] 2bi
116 [ dup 0 < [ + ] [ nip ] if ]
117 [ "Non-trivial divisor found" throw ] if ; foldable
119 : ^mod ( x y n -- z )
121 [ [ neg ] dip ^mod ] keep mod-inv
126 GENERIC: absq ( x -- y ) foldable
130 : ~abs ( x y epsilon -- ? )
133 : ~rel ( x y epsilon -- ? )
134 [ [ - abs ] 2keep [ abs ] bi@ + ] dip * < ;
136 : ~ ( x y epsilon -- ? )
138 { [ 2over [ fp-nan? ] either? ] [ 3drop f ] }
139 { [ dup zero? ] [ drop number= ] }
140 { [ dup 0 < ] [ ~rel ] }
144 : conjugate ( z -- z* ) >rect neg rect> ; inline
146 : arg ( z -- arg ) >float-rect swap fatan2 ; inline
149 dup complex? [ drop f ] [ abs 1 <= ] if ; inline
152 dup complex? [ drop f ] [ 1 >= ] if ; inline
154 GENERIC: exp ( x -- y )
158 M: complex exp >rect swap fexp swap polar> ;
160 GENERIC: log ( x -- y )
162 M: real log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ;
164 M: complex log >polar swap flog swap rect> ;
166 GENERIC: cos ( x -- y ) foldable
170 [ [ fcos ] [ fcosh ] bi* * ]
171 [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
175 : sec ( x -- y ) cos recip ; inline
177 GENERIC: cosh ( x -- y ) foldable
181 [ [ fcosh ] [ fcos ] bi* * ]
182 [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
186 : sech ( x -- y ) cosh recip ; inline
188 GENERIC: sin ( x -- y ) foldable
192 [ [ fsin ] [ fcosh ] bi* * ]
193 [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
197 : cosec ( x -- y ) sin recip ; inline
199 GENERIC: sinh ( x -- y ) foldable
203 [ [ fsinh ] [ fcos ] bi* * ]
204 [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
208 : cosech ( x -- y ) sinh recip ; inline
210 GENERIC: tan ( x -- y ) foldable
212 M: complex tan [ sin ] [ cos ] bi / ;
216 GENERIC: tanh ( x -- y ) foldable
218 M: complex tanh [ sinh ] [ cosh ] bi / ;
222 : cot ( x -- y ) tan recip ; inline
224 : coth ( x -- y ) tanh recip ; inline
227 dup sq 1- sqrt + log ; inline
229 : asech ( x -- y ) recip acosh ; inline
232 dup sq 1+ sqrt + log ; inline
234 : acosech ( x -- y ) recip asinh ; inline
237 [ 1+ ] [ 1- neg ] bi / log 2 / ; inline
239 : acoth ( x -- y ) recip atanh ; inline
241 : i* ( x -- y ) >rect neg swap rect> ;
243 : -i* ( x -- y ) >rect swap neg rect> ;
246 dup [-1,1]? [ fasin ] [ i* asinh -i* ] if ; inline
249 dup [-1,1]? [ facos ] [ asin pi 2 / swap - ] if ;
252 GENERIC: atan ( x -- y ) foldable
254 M: complex atan i* atanh i* ;
258 : asec ( x -- y ) recip acos ; inline
260 : acosec ( x -- y ) recip asin ; inline
262 : acot ( x -- y ) recip atan ; inline
264 : truncate ( x -- y ) dup 1 mod - ; inline
266 : round ( x -- y ) dup sgn 2 / + truncate ; inline
270 [ drop ] [ dup 0 < [ - 1- ] [ - ] if ] if ; foldable
272 : ceiling ( x -- y ) neg floor neg ; foldable