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 )
50 1 swap [ [ dupd * ] when [ sq ] dip ] each-bit nip ; inline
53 [ factor-2s ] dip [ (^n) ] keep rot * shift ;
56 [ >fraction ] dip tuck [ ^n ] 2bi@ / ;
61 : integer^ ( x y -- z )
62 dup 0 > [ ^n ] [ neg ^n recip ] if ; inline
67 [ real-part ] [ imaginary-part ] bi ; inline
69 : >float-rect ( z -- x y )
70 >rect [ >float ] bi@ ; inline
72 : >polar ( z -- abs arg )
73 >float-rect [ [ sq ] bi@ + fsqrt ] [ swap fatan2 ] 2bi ; inline
75 : cis ( arg -- z ) dup fcos swap fsin rect> ; inline
77 : polar> ( abs arg -- z ) cis * ; inline
81 : ^mag ( w abs arg -- magnitude )
82 [ >float-rect swap ] [ swap fpow ] [ rot * fexp /f ] tri* ; inline
84 : ^theta ( w abs arg -- theta )
85 [ >float-rect ] [ flog * swap ] [ * + ] tri* ; inline
87 : ^complex ( x y -- z )
88 swap >polar [ ^mag ] [ ^theta ] 3bi polar> ; inline
91 2dup [ real? ] both? [ drop 0 >= ] [ 2drop f ] if ; inline
94 dup zero? [ drop 0./0. ] [ 0 < 1./0. 0 ? ] if ; inline
96 : (^mod) ( n x y -- z )
98 [ dupd * pick mod ] when [ sq over mod ] dip
99 ] each-bit 2nip ; inline
101 : (gcd) ( b a x y -- a d )
105 swap [ /mod [ over * swapd - ] dip ] keep (gcd)
112 { [ over zero? ] [ nip 0^ ] }
113 { [ dup integer? ] [ integer^ ] }
114 { [ 2dup real^? ] [ fpow ] }
119 [ 0 1 ] 2dip (gcd) dup 0 < [ neg ] when ; foldable
122 [ * ] 2keep gcd nip /i ; foldable
124 : mod-inv ( x n -- y )
125 [ nip ] [ gcd 1 = ] 2bi
126 [ dup 0 < [ + ] [ nip ] if ]
127 [ "Non-trivial divisor found" throw ] if ; foldable
129 : ^mod ( x y n -- z )
131 [ [ neg ] dip ^mod ] keep mod-inv
136 GENERIC: absq ( x -- y ) foldable
140 : ~abs ( x y epsilon -- ? )
143 : ~rel ( x y epsilon -- ? )
144 [ [ - abs ] 2keep [ abs ] bi@ + ] dip * < ;
146 : ~ ( x y epsilon -- ? )
148 { [ 2over [ fp-nan? ] either? ] [ 3drop f ] }
149 { [ dup zero? ] [ drop number= ] }
150 { [ dup 0 < ] [ ~rel ] }
154 : conjugate ( z -- z* ) >rect neg rect> ; inline
156 : arg ( z -- arg ) >float-rect swap fatan2 ; inline
159 dup complex? [ drop f ] [ abs 1 <= ] if ; inline
162 dup complex? [ drop f ] [ 1 >= ] if ; inline
164 GENERIC: exp ( x -- y )
168 M: complex exp >rect swap fexp swap polar> ;
170 GENERIC: log ( x -- y )
172 M: real log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ;
174 M: complex log >polar swap flog swap rect> ;
176 GENERIC: cos ( x -- y ) foldable
180 [ [ fcos ] [ fcosh ] bi* * ]
181 [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
185 : sec ( x -- y ) cos recip ; inline
187 GENERIC: cosh ( x -- y ) foldable
191 [ [ fcosh ] [ fcos ] bi* * ]
192 [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
196 : sech ( x -- y ) cosh recip ; inline
198 GENERIC: sin ( x -- y ) foldable
202 [ [ fsin ] [ fcosh ] bi* * ]
203 [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
207 : cosec ( x -- y ) sin recip ; inline
209 GENERIC: sinh ( x -- y ) foldable
213 [ [ fsinh ] [ fcos ] bi* * ]
214 [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
218 : cosech ( x -- y ) sinh recip ; inline
220 GENERIC: tan ( x -- y ) foldable
222 M: complex tan [ sin ] [ cos ] bi / ;
226 GENERIC: tanh ( x -- y ) foldable
228 M: complex tanh [ sinh ] [ cosh ] bi / ;
232 : cot ( x -- y ) tan recip ; inline
234 : coth ( x -- y ) tanh recip ; inline
237 dup sq 1- sqrt + log ; inline
239 : asech ( x -- y ) recip acosh ; inline
242 dup sq 1+ sqrt + log ; inline
244 : acosech ( x -- y ) recip asinh ; inline
247 [ 1+ ] [ 1- neg ] bi / log 2 / ; inline
249 : acoth ( x -- y ) recip atanh ; inline
251 : i* ( x -- y ) >rect neg swap rect> ;
253 : -i* ( x -- y ) >rect swap neg rect> ;
256 dup [-1,1]? [ fasin ] [ i* asinh -i* ] if ; inline
259 dup [-1,1]? [ facos ] [ asin pi 2 / swap - ] if ;
262 GENERIC: atan ( x -- y ) foldable
264 M: complex atan i* atanh i* ;
268 : asec ( x -- y ) recip acos ; inline
270 : acosec ( x -- y ) recip asin ; inline
272 : acot ( x -- y ) recip atan ; inline
274 : truncate ( x -- y ) dup 1 mod - ; inline
276 : round ( x -- y ) dup sgn 2 / + truncate ; inline
280 [ drop ] [ dup 0 < [ - 1- ] [ - ] if ] if ; foldable
282 : ceiling ( x -- y ) neg floor neg ; foldable