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 ; inline
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 ) >float [ fcos ] [ fsin ] bi rect> ; inline
57 : polar> ( abs arg -- z ) cis * ; inline
59 GENERIC: exp ( x -- y )
61 M: float exp fexp ; inline
63 M: real exp >float exp ; inline
65 M: complex exp >rect swap fexp swap polar> ; inline
69 : ^mag ( w abs arg -- magnitude )
71 [ >float swap >float fpow ]
75 : ^theta ( w abs arg -- theta )
76 [ >float-rect ] [ flog * swap ] [ * + ] tri* ; inline
78 : ^complex ( x y -- z )
79 swap >polar [ ^mag ] [ ^theta ] 3bi polar> ; inline
82 2dup [ real? ] both? [ drop 0 >= ] [ 2drop f ] if ; inline
85 [ 0/0. ] [ 0 < 1/0. 0 ? ] if-zero ; inline
87 : (^mod) ( n x y -- z )
89 [ dupd * pick mod ] when [ sq over mod ] dip
90 ] reduce 2nip ; inline
92 : (gcd) ( b a x y -- a d )
96 swap [ /mod [ over * swapd - ] dip ] keep (gcd)
103 { [ over 0 = ] [ nip 0^ ] }
104 { [ dup integer? ] [ integer^ ] }
105 { [ 2dup real^? ] [ [ >float ] bi@ fpow ] }
110 [ 0 1 ] 2dip (gcd) dup 0 < [ neg ] when ; foldable
113 [ * ] 2keep gcd nip /i ; foldable
115 : divisor? ( m n -- ? )
118 ERROR: non-trivial-divisor n ;
120 : mod-inv ( x n -- y )
121 [ nip ] [ gcd 1 = ] 2bi
122 [ dup 0 < [ + ] [ nip ] if ]
123 [ non-trivial-divisor ] if ; foldable
125 : ^mod ( x y n -- z )
127 [ [ neg ] dip ^mod ] keep mod-inv
132 GENERIC: absq ( x -- y ) foldable
134 M: real absq sq ; inline
136 : ~abs ( x y epsilon -- ? )
139 : ~rel ( x y epsilon -- ? )
140 [ [ - abs ] 2keep [ abs ] bi@ + ] dip * <= ;
142 : ~ ( x y epsilon -- ? )
144 { [ dup zero? ] [ drop number= ] }
145 { [ dup 0 < ] [ neg ~rel ] }
149 : conjugate ( z -- z* ) >rect neg rect> ; inline
151 : arg ( z -- arg ) >float-rect swap fatan2 ; inline
154 dup complex? [ drop f ] [ abs 1 <= ] if ; inline
157 dup complex? [ drop f ] [ 1 >= ] if ; inline
159 GENERIC: log ( x -- y )
161 M: float log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ; inline
163 M: real log >float log ; inline
165 M: complex log >polar [ flog ] dip rect> ; inline
167 GENERIC: log1+ ( x -- y )
169 M: object log1+ 1 + log ; inline
171 M: float log1+ dup -1.0 >= [ flog1+ ] [ 1.0 + 0.0 rect> log ] if ; inline
173 : 10^ ( x -- y ) 10 swap ^ ; inline
175 GENERIC: log10 ( x -- y ) foldable
177 M: real log10 >float flog10 ; inline
179 M: complex log10 log 10 log / ; inline
181 GENERIC: cos ( x -- y ) foldable
185 [ [ fcos ] [ fcosh ] bi* * ]
186 [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
188 M: float cos fcos ; inline
190 M: real cos >float cos ; inline
192 : sec ( x -- y ) cos recip ; inline
194 GENERIC: cosh ( x -- y ) foldable
198 [ [ fcosh ] [ fcos ] bi* * ]
199 [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
201 M: float cosh fcosh ; inline
203 M: real cosh >float cosh ; inline
205 : sech ( x -- y ) cosh recip ; inline
207 GENERIC: sin ( x -- y ) foldable
211 [ [ fsin ] [ fcosh ] bi* * ]
212 [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
214 M: float sin fsin ; inline
216 M: real sin >float sin ; inline
218 : cosec ( x -- y ) sin recip ; inline
220 GENERIC: sinh ( x -- y ) foldable
224 [ [ fsinh ] [ fcos ] bi* * ]
225 [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
227 M: float sinh fsinh ; inline
229 M: real sinh >float sinh ; inline
231 : cosech ( x -- y ) sinh recip ; inline
233 GENERIC: tan ( x -- y ) foldable
235 M: complex tan [ sin ] [ cos ] bi / ;
237 M: float tan ftan ; inline
239 M: real tan >float tan ; inline
241 GENERIC: tanh ( x -- y ) foldable
243 M: complex tanh [ sinh ] [ cosh ] bi / ;
245 M: float tanh ftanh ; inline
247 M: real tanh >float tanh ; inline
249 : cot ( x -- y ) tan recip ; inline
251 : coth ( x -- y ) tanh recip ; inline
254 dup sq 1 - sqrt + log ; inline
256 : asech ( x -- y ) recip acosh ; inline
259 dup sq 1 + sqrt + log ; inline
261 : acosech ( x -- y ) recip asinh ; inline
264 [ 1 + ] [ 1 - neg ] bi / log 2 / ; inline
266 : acoth ( x -- y ) recip atanh ; inline
268 : i* ( x -- y ) >rect neg swap rect> ;
270 : -i* ( x -- y ) >rect swap neg rect> ;
273 dup [-1,1]? [ >float fasin ] [ i* asinh -i* ] if ; inline
276 dup [-1,1]? [ >float facos ] [ asin pi 2 / swap - ] if ;
279 GENERIC: atan ( x -- y ) foldable
281 M: complex atan i* atanh i* ; inline
283 M: float atan fatan ; inline
285 M: real atan >float atan ; inline
287 : asec ( x -- y ) recip acos ; inline
289 : acosec ( x -- y ) recip asin ; inline
291 : acot ( x -- y ) recip atan ; inline
293 : truncate ( x -- y ) dup 1 mod - ; inline
295 : round ( x -- y ) dup sgn 2 / + truncate ; inline
299 [ ] [ dup 0 < [ - 1 - ] [ - ] if ] if-zero ; foldable
301 : ceiling ( x -- y ) neg floor neg ; foldable
303 : floor-to ( x step -- y )
304 [ [ / floor ] [ * ] bi ] unless-zero ;
306 : lerp ( a b t -- a_t ) [ over - ] dip * + ; inline