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
11 dup 0 = [ drop ] [ complex boa ] if ; inline
13 GENERIC: sqrt ( x -- y ) foldable
16 >float dup 0.0 < [ neg fsqrt 0.0 swap rect> ] [ fsqrt ] if ;
18 : factor-2s ( n -- r s )
19 #! factor an integer into 2^r * s
21 0 swap [ dup even? ] [ [ 1 + ] [ 2/ ] bi* ] while
26 GENERIC# ^n 1 ( z w -- z^w ) foldable
29 make-bits 1 [ [ dupd * ] when [ sq ] dip ] reduce nip ; inline
32 [ factor-2s ] dip [ (^n) ] keep rot * shift ;
35 [ >fraction ] dip [ ^n ] curry bi@ / ;
41 : integer^ ( x y -- z )
42 dup 0 > [ ^n ] [ neg ^n recip ] if ; inline
47 [ real-part ] [ imaginary-part ] bi ; inline
49 : >float-rect ( z -- x y )
50 >rect [ >float ] bi@ ; inline
52 : >polar ( z -- abs arg )
53 >float-rect [ [ sq ] bi@ + fsqrt ] [ swap fatan2 ] 2bi ; inline
55 : cis ( arg -- z ) dup fcos swap fsin rect> ; inline
57 : polar> ( abs arg -- z ) cis * ; inline
61 : ^mag ( w abs arg -- magnitude )
62 [ >float-rect swap ] [ swap fpow ] [ rot * fexp /f ] tri* ; inline
64 : ^theta ( w abs arg -- theta )
65 [ >float-rect ] [ flog * swap ] [ * + ] tri* ; inline
67 : ^complex ( x y -- z )
68 swap >polar [ ^mag ] [ ^theta ] 3bi polar> ; inline
71 2dup [ real? ] both? [ drop 0 >= ] [ 2drop f ] if ; inline
74 dup zero? [ drop 0/0. ] [ 0 < 1/0. 0 ? ] if ; inline
76 : (^mod) ( n x y -- z )
78 [ dupd * pick mod ] when [ sq over mod ] dip
79 ] reduce 2nip ; inline
81 : (gcd) ( b a x y -- a d )
85 swap [ /mod [ over * swapd - ] dip ] keep (gcd)
92 { [ over zero? ] [ nip 0^ ] }
93 { [ dup integer? ] [ integer^ ] }
94 { [ 2dup real^? ] [ fpow ] }
99 [ 0 1 ] 2dip (gcd) dup 0 < [ neg ] when ; foldable
102 [ * ] 2keep gcd nip /i ; foldable
104 : divisor? ( m n -- ? )
107 : mod-inv ( x n -- y )
108 [ nip ] [ gcd 1 = ] 2bi
109 [ dup 0 < [ + ] [ nip ] if ]
110 [ "Non-trivial divisor found" throw ] if ; foldable
112 : ^mod ( x y n -- z )
114 [ [ neg ] dip ^mod ] keep mod-inv
119 GENERIC: absq ( x -- y ) foldable
123 : ~abs ( x y epsilon -- ? )
126 : ~rel ( x y epsilon -- ? )
127 [ [ - abs ] 2keep [ abs ] bi@ + ] dip * < ;
129 : ~ ( x y epsilon -- ? )
131 { [ 2over [ fp-nan? ] either? ] [ 3drop f ] }
132 { [ dup zero? ] [ drop number= ] }
133 { [ dup 0 < ] [ ~rel ] }
137 : conjugate ( z -- z* ) >rect neg rect> ; inline
139 : arg ( z -- arg ) >float-rect swap fatan2 ; inline
142 dup complex? [ drop f ] [ abs 1 <= ] if ; inline
145 dup complex? [ drop f ] [ 1 >= ] if ; inline
147 GENERIC: exp ( x -- y )
151 M: complex exp >rect swap fexp swap polar> ;
153 GENERIC: log ( x -- y )
155 M: real log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ;
157 M: complex log >polar swap flog swap rect> ;
159 GENERIC: cos ( x -- y ) foldable
163 [ [ fcos ] [ fcosh ] bi* * ]
164 [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
168 : sec ( x -- y ) cos recip ; inline
170 GENERIC: cosh ( x -- y ) foldable
174 [ [ fcosh ] [ fcos ] bi* * ]
175 [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
179 : sech ( x -- y ) cosh recip ; inline
181 GENERIC: sin ( x -- y ) foldable
185 [ [ fsin ] [ fcosh ] bi* * ]
186 [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
190 : cosec ( x -- y ) sin recip ; inline
192 GENERIC: sinh ( x -- y ) foldable
196 [ [ fsinh ] [ fcos ] bi* * ]
197 [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
201 : cosech ( x -- y ) sinh recip ; inline
203 GENERIC: tan ( x -- y ) foldable
205 M: complex tan [ sin ] [ cos ] bi / ;
209 GENERIC: tanh ( x -- y ) foldable
211 M: complex tanh [ sinh ] [ cosh ] bi / ;
215 : cot ( x -- y ) tan recip ; inline
217 : coth ( x -- y ) tanh recip ; inline
220 dup sq 1 - sqrt + log ; inline
222 : asech ( x -- y ) recip acosh ; inline
225 dup sq 1 + sqrt + log ; inline
227 : acosech ( x -- y ) recip asinh ; inline
230 [ 1 + ] [ 1 - neg ] bi / log 2 / ; inline
232 : acoth ( x -- y ) recip atanh ; inline
234 : i* ( x -- y ) >rect neg swap rect> ;
236 : -i* ( x -- y ) >rect swap neg rect> ;
239 dup [-1,1]? [ fasin ] [ i* asinh -i* ] if ; inline
242 dup [-1,1]? [ facos ] [ asin pi 2 / swap - ] if ;
245 GENERIC: atan ( x -- y ) foldable
247 M: complex atan i* atanh i* ;
251 : asec ( x -- y ) recip acos ; inline
253 : acosec ( x -- y ) recip asin ; inline
255 : acot ( x -- y ) recip atan ; inline
257 : truncate ( x -- y ) dup 1 mod - ; inline
259 : round ( x -- y ) dup sgn 2 / + truncate ; inline
263 [ drop ] [ dup 0 < [ - 1 - ] [ - ] if ] if ; foldable
265 : ceiling ( x -- y ) neg floor neg ; foldable
267 : lerp ( a b t -- a_t ) [ over - ] dip * + ; inline