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
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 : each-bit ( n quot: ( ? -- ) -- )
30 over [ 0 = ] [ -1 = ] bi or [
33 2dup { [ odd? ] [ call ] [ 2/ ] [ each-bit ] } spread
34 ] if ; inline recursive
36 : map-bits ( n quot: ( ? -- obj ) -- seq )
37 accumulator [ each-bit ] dip ; inline
39 : factor-2s ( n -- r s )
40 #! factor an integer into 2^r * s
42 0 swap [ dup even? ] [ [ 1+ ] [ 2/ ] bi* ] [ ] while
47 GENERIC# ^n 1 ( z w -- z^w )
49 : (^n) 1 swap [ [ dupd * ] when [ sq ] dip ] each-bit nip ; inline
52 [ factor-2s ] dip [ (^n) ] keep rot * shift ;
55 [ >fraction ] dip tuck [ ^n ] 2bi@ / ;
60 : integer^ ( x y -- z )
61 dup 0 > [ ^n ] [ neg ^n recip ] if ; inline
66 [ real-part ] [ imaginary-part ] bi ; inline
68 : >float-rect ( z -- x y )
69 >rect [ >float ] bi@ ; inline
71 : >polar ( z -- abs arg )
72 >float-rect [ [ sq ] bi@ + fsqrt ] [ swap fatan2 ] 2bi ; inline
74 : cis ( arg -- z ) dup fcos swap fsin rect> ; inline
76 : polar> ( abs arg -- z ) cis * ; inline
80 : ^mag ( w abs arg -- magnitude )
81 [ >float-rect swap ] [ swap fpow ] [ rot * fexp /f ] tri* ; inline
83 : ^theta ( w abs arg -- theta )
84 [ >float-rect ] [ flog * swap ] [ * + ] tri* ; inline
86 : ^complex ( x y -- z )
87 swap >polar [ ^mag ] [ ^theta ] 3bi polar> ; inline
90 2dup [ real? ] both? [ drop 0 >= ] [ 2drop f ] if ; inline
93 dup zero? [ drop 0./0. ] [ 0 < 1./0. 0 ? ] if ; inline
95 : (^mod) ( n x y -- z )
97 [ dupd * pick mod ] when [ sq over mod ] dip
98 ] each-bit 2nip ; inline
100 : (gcd) ( b a x y -- a d )
104 swap [ /mod [ over * swapd - ] dip ] keep (gcd)
111 { [ over zero? ] [ nip 0^ ] }
112 { [ dup integer? ] [ integer^ ] }
113 { [ 2dup real^? ] [ fpow ] }
118 [ 0 1 ] 2dip (gcd) dup 0 < [ neg ] when ; foldable
121 [ * ] 2keep gcd nip /i ; foldable
123 : mod-inv ( x n -- y )
125 dup 0 < [ + ] [ nip ] if
127 "Non-trivial divisor found" throw
130 : ^mod ( x y n -- z )
132 [ [ neg ] dip ^mod ] keep mod-inv
137 GENERIC: absq ( x -- y ) foldable
141 : ~abs ( x y epsilon -- ? )
144 : ~rel ( x y epsilon -- ? )
145 [ [ - abs ] 2keep [ abs ] bi@ + ] dip * < ;
147 : ~ ( x y epsilon -- ? )
149 { [ 2over [ fp-nan? ] either? ] [ 3drop f ] }
150 { [ dup zero? ] [ drop number= ] }
151 { [ dup 0 < ] [ ~rel ] }
155 : conjugate ( z -- z* ) >rect neg rect> ; inline
157 : arg ( z -- arg ) >float-rect swap fatan2 ; inline
160 dup complex? [ drop f ] [ abs 1 <= ] if ; inline
163 dup complex? [ drop f ] [ 1 >= ] if ; inline
165 GENERIC: exp ( x -- y )
169 M: complex exp >rect swap fexp swap polar> ;
171 GENERIC: log ( x -- y )
173 M: real log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ;
175 M: complex log >polar swap flog swap rect> ;
177 GENERIC: cos ( x -- y ) foldable
181 [ [ fcos ] [ fcosh ] bi* * ]
182 [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
186 : sec ( x -- y ) cos recip ; inline
188 GENERIC: cosh ( x -- y ) foldable
192 [ [ fcosh ] [ fcos ] bi* * ]
193 [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
197 : sech ( x -- y ) cosh recip ; inline
199 GENERIC: sin ( x -- y ) foldable
203 [ [ fsin ] [ fcosh ] bi* * ]
204 [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
208 : cosec ( x -- y ) sin recip ; inline
210 GENERIC: sinh ( x -- y ) foldable
214 [ [ fsinh ] [ fcos ] bi* * ]
215 [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
219 : cosech ( x -- y ) sinh recip ; inline
221 GENERIC: tan ( x -- y ) foldable
223 M: complex tan [ sin ] [ cos ] bi / ;
227 GENERIC: tanh ( x -- y ) foldable
229 M: complex tanh [ sinh ] [ cosh ] bi / ;
233 : cot ( x -- y ) tan recip ; inline
235 : coth ( x -- y ) tanh recip ; inline
238 dup sq 1- sqrt + log ; inline
240 : asech ( x -- y ) recip acosh ; inline
243 dup sq 1+ sqrt + log ; inline
245 : acosech ( x -- y ) recip asinh ; inline
248 [ 1+ ] [ 1- neg ] bi / log 2 / ; inline
250 : acoth ( x -- y ) recip atanh ; inline
252 : i* ( x -- y ) >rect neg swap rect> ;
254 : -i* ( x -- y ) >rect swap neg rect> ;
257 dup [-1,1]? [ fasin ] [ i* asinh -i* ] if ; inline
260 dup [-1,1]? [ facos ] [ asin pi 2 / swap - ] if ;
263 GENERIC: atan ( x -- y ) foldable
265 M: complex atan i* atanh i* ;
269 : asec ( x -- y ) recip acos ; inline
271 : acosec ( x -- y ) recip asin ; inline
273 : acot ( x -- y ) recip atan ; inline
275 : truncate ( x -- y ) dup 1 mod - ; inline
277 : round ( x -- y ) dup sgn 2 / + truncate ; inline
281 [ drop ] [ dup 0 < [ - 1- ] [ - ] if ] if ; foldable
283 : ceiling ( x -- y ) neg floor neg ; foldable