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 )
126 dup 0 < [ + ] [ nip ] if
128 "Non-trivial divisor found" throw
131 : ^mod ( x y n -- z )
133 [ [ neg ] dip ^mod ] keep mod-inv
138 GENERIC: absq ( x -- y ) foldable
142 : ~abs ( x y epsilon -- ? )
145 : ~rel ( x y epsilon -- ? )
146 [ [ - abs ] 2keep [ abs ] bi@ + ] dip * < ;
148 : ~ ( x y epsilon -- ? )
150 { [ 2over [ fp-nan? ] either? ] [ 3drop f ] }
151 { [ dup zero? ] [ drop number= ] }
152 { [ dup 0 < ] [ ~rel ] }
156 : conjugate ( z -- z* ) >rect neg rect> ; inline
158 : arg ( z -- arg ) >float-rect swap fatan2 ; inline
161 dup complex? [ drop f ] [ abs 1 <= ] if ; inline
164 dup complex? [ drop f ] [ 1 >= ] if ; inline
166 GENERIC: exp ( x -- y )
170 M: complex exp >rect swap fexp swap polar> ;
172 GENERIC: log ( x -- y )
174 M: real log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ;
176 M: complex log >polar swap flog swap rect> ;
178 GENERIC: cos ( x -- y ) foldable
182 [ [ fcos ] [ fcosh ] bi* * ]
183 [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
187 : sec ( x -- y ) cos recip ; inline
189 GENERIC: cosh ( x -- y ) foldable
193 [ [ fcosh ] [ fcos ] bi* * ]
194 [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
198 : sech ( x -- y ) cosh recip ; inline
200 GENERIC: sin ( x -- y ) foldable
204 [ [ fsin ] [ fcosh ] bi* * ]
205 [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
209 : cosec ( x -- y ) sin recip ; inline
211 GENERIC: sinh ( x -- y ) foldable
215 [ [ fsinh ] [ fcos ] bi* * ]
216 [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
220 : cosech ( x -- y ) sinh recip ; inline
222 GENERIC: tan ( x -- y ) foldable
224 M: complex tan [ sin ] [ cos ] bi / ;
228 GENERIC: tanh ( x -- y ) foldable
230 M: complex tanh [ sinh ] [ cosh ] bi / ;
234 : cot ( x -- y ) tan recip ; inline
236 : coth ( x -- y ) tanh recip ; inline
239 dup sq 1- sqrt + log ; inline
241 : asech ( x -- y ) recip acosh ; inline
244 dup sq 1+ sqrt + log ; inline
246 : acosech ( x -- y ) recip asinh ; inline
249 [ 1+ ] [ 1- neg ] bi / log 2 / ; inline
251 : acoth ( x -- y ) recip atanh ; inline
253 : i* ( x -- y ) >rect neg swap rect> ;
255 : -i* ( x -- y ) >rect swap neg rect> ;
258 dup [-1,1]? [ fasin ] [ i* asinh -i* ] if ; inline
261 dup [-1,1]? [ facos ] [ asin pi 2 / swap - ] if ;
264 GENERIC: atan ( x -- y ) foldable
266 M: complex atan i* atanh i* ;
270 : asec ( x -- y ) recip acos ; inline
272 : acosec ( x -- y ) recip asin ; inline
274 : acot ( x -- y ) recip atan ; inline
276 : truncate ( x -- y ) dup 1 mod - ; inline
278 : round ( x -- y ) dup sgn 2 / + truncate ; inline
282 [ drop ] [ dup 0 < [ - 1- ] [ - ] if ] if ; foldable
284 : ceiling ( x -- y ) neg floor neg ; foldable