1 ! Copyright (C) 2004, 2010 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 fry math.order sequences ;
7 GENERIC: >fraction ( a/b -- a b )
9 M: integer >fraction 1 ; inline
11 M: ratio >fraction [ numerator ] [ denominator ] bi ; inline
14 ! Note: an imaginary 0.0 should still create a complex
15 dup 0 = [ drop ] [ complex boa ] if ; inline
17 GENERIC: sqrt ( x -- y ) foldable
21 [ neg fsqrt [ 0.0 ] dip rect> ] [ fsqrt ] if ; inline
23 : factor-2s ( n -- r s )
24 #! factor an integer into 2^r * s
26 [ 0 ] dip [ dup even? ] [ [ 1 + ] [ 2/ ] bi* ] while
31 : (^fixnum) ( z w -- z^w )
35 [ [ * ] keep ] [ 1 - ] bi*
36 ] when [ sq ] [ 2/ ] bi*
37 ] until 2drop ; inline
39 : (^bignum) ( z w -- z^w )
40 make-bits 1 [ [ over * ] when [ sq ] dip ] reduce nip ; inline
43 dup fixnum? [ (^fixnum) ] [ (^bignum) ] if ; inline
45 GENERIC# ^n 1 ( z w -- z^w ) foldable
50 [ factor-2s ] dip [ (^n) ] keep rot * shift ;
53 [ >fraction ] dip '[ _ ^n ] bi@ / ;
59 : integer^ ( x y -- z )
60 dup 0 >= [ ^n ] [ [ recip ] dip neg ^n ] if ; inline
64 GENERIC: >rect ( z -- x y )
66 M: real >rect 0 ; inline
68 M: complex >rect [ real-part ] [ imaginary-part ] bi ; inline
70 : >float-rect ( z -- x y )
71 >rect [ >float ] bi@ ; inline
73 : >polar ( z -- abs arg )
74 >float-rect [ [ sq ] bi@ + fsqrt ] [ swap fatan2 ] 2bi ; inline
76 : cis ( arg -- z ) >float [ fcos ] [ fsin ] bi rect> ; inline
78 : polar> ( abs arg -- z ) cis * ; inline
80 GENERIC: e^ ( x -- y )
82 M: float e^ fexp ; inline
84 M: real e^ >float e^ ; inline
86 M: complex e^ >rect [ e^ ] dip polar> ; inline
90 : ^mag ( w abs arg -- magnitude )
92 [ >float swap >float fpow ]
96 : ^theta ( w abs arg -- theta )
97 [ >float-rect ] [ flog * swap ] [ * + ] tri* ; inline
99 : ^complex ( x y -- z )
100 swap >polar [ ^mag ] [ ^theta ] 3bi polar> ; inline
102 : real^? ( x y -- ? )
103 2dup [ real? ] both? [ drop 0 >= ] [ 2drop f ] if ; inline
106 swap [ 0/0. ] swap '[ 0 < 1/0. _ ? ] if-zero ; inline
108 : (^mod) ( x y n -- z )
109 [ make-bits 1 ] dip dup
110 '[ [ over * _ mod ] when [ sq _ mod ] dip ] reduce nip ; inline
112 : (gcd) ( b a x y -- a d )
116 swap [ /mod [ over * swapd - ] dip ] keep (gcd)
117 ] if ; inline recursive
123 { [ over zero? ] [ 0^ ] }
124 { [ dup integer? ] [ integer^ ] }
125 { [ 2dup real^? ] [ [ >float ] bi@ fpow ] }
129 : nth-root ( n x -- y ) swap recip ^ ; inline
132 [ 0 1 ] 2dip (gcd) dup 0 < [ neg ] when ; inline
134 MATH: fast-gcd ( x y -- d ) foldable
138 : simple-gcd ( x y -- d ) gcd nip ; inline
142 M: real fast-gcd simple-gcd ; inline
144 M: bignum fast-gcd bignum-gcd ; inline
147 [ * ] 2keep fast-gcd /i ; foldable
149 : divisor? ( m n -- ? )
152 ERROR: non-trivial-divisor n ;
154 : mod-inv ( x n -- y )
155 [ nip ] [ gcd 1 = ] 2bi
156 [ dup 0 < [ + ] [ nip ] if ]
157 [ non-trivial-divisor ] if ; foldable
159 : ^mod ( x y n -- z )
161 [ [ [ neg ] dip ^mod ] keep mod-inv ] [ (^mod) ] if ; foldable
163 GENERIC: absq ( x -- y ) foldable
165 M: real absq sq ; inline
167 : ~abs ( x y epsilon -- ? )
170 : ~rel ( x y epsilon -- ? )
171 [ [ - abs ] 2keep [ abs ] bi@ + ] dip * <= ;
173 : ~ ( x y epsilon -- ? )
175 { [ dup zero? ] [ drop number= ] }
176 { [ dup 0 < ] [ neg ~rel ] }
180 : conjugate ( z -- z* ) >rect neg rect> ; inline
182 : arg ( z -- arg ) >float-rect swap fatan2 ; inline
185 dup complex? [ drop f ] [ abs 1 <= ] if ; inline
188 dup complex? [ drop f ] [ 1 >= ] if ; inline
190 GENERIC: frexp ( x -- y exp )
193 dup fp-special? [ dup zero? ] unless* [ 0 ] [
195 [ 0x800f,ffff,ffff,ffff bitand 0.5 double>bits bitor bits>double ]
196 [ -52 shift 0x7ff bitand 1022 - ] bi
201 dup 0 > [ 1 ] [ abs -1 ] if swap dup log2 [
202 52 swap - shift 0x000f,ffff,ffff,ffff bitand
203 0.5 double>bits bitor bits>double
204 ] [ 1 + ] bi [ * ] dip
209 GENERIC# ldexp 1 ( x exp -- y )
212 over fp-special? [ over zero? ] unless* [ drop ] [
213 [ double>bits dup -52 shift 0x7ff bitand 1023 - ] dip +
215 { [ dup -1074 < ] [ drop 0 copysign ] }
216 { [ dup 1023 > ] [ drop 0 < -1/0. 1/0. ? ] }
218 dup -1022 < [ 52 + -52 2^ ] [ 1 ] if
219 [ -0x7ff0,0000,0000,0001 bitand ]
220 [ 1023 + 52 shift bitor bits>double ]
227 2dup [ zero? ] either? [ 2drop 0 ] [ shift ] if ;
229 GENERIC: log ( x -- y )
231 M: float log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ; inline
233 M: real log >float log ; inline
235 M: complex log >polar [ flog ] dip rect> ; inline
239 : most-negative-finite-float ( -- x )
240 -0x1.ffff,ffff,ffff,fp1023 >integer ; inline
241 : most-positive-finite-float ( -- x )
242 0x1.ffff,ffff,ffff,fp1023 >integer ; inline
243 CONSTANT: log-2 0x1.62e42fefa39efp-1
244 CONSTANT: log10-2 0x1.34413509f79ffp-2
246 : (representable-as-float?) ( x -- ? )
247 most-negative-finite-float
248 most-positive-finite-float between? ; inline
250 : (bignum-log) ( n log-quot: ( x -- y ) log-2 -- log )
252 dup (representable-as-float?)
253 [ >float @ ] [ frexp [ @ ] [ _ * ] bi* + ] if
258 M: bignum log [ log ] log-2 (bignum-log) ;
260 GENERIC: log1+ ( x -- y )
262 M: object log1+ 1 + log ; inline
264 M: float log1+ dup -1.0 >= [ flog1+ ] [ 1.0 + 0.0 rect> log ] if ; inline
266 : 10^ ( x -- y ) 10 swap ^ ; inline
268 GENERIC: log10 ( x -- y ) foldable
270 M: real log10 >float flog10 ; inline
272 M: complex log10 log 10 log / ; inline
274 M: bignum log10 [ log10 ] log10-2 (bignum-log) ;
276 GENERIC: cos ( x -- y ) foldable
280 [ [ fcos ] [ fcosh ] bi* * ]
281 [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
283 M: float cos fcos ; inline
285 M: real cos >float cos ; inline
287 : sec ( x -- y ) cos recip ; inline
289 GENERIC: cosh ( x -- y ) foldable
293 [ [ fcosh ] [ fcos ] bi* * ]
294 [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
296 M: float cosh fcosh ; inline
298 M: real cosh >float cosh ; inline
300 : sech ( x -- y ) cosh recip ; inline
302 GENERIC: sin ( x -- y ) foldable
306 [ [ fsin ] [ fcosh ] bi* * ]
307 [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
309 M: float sin fsin ; inline
311 M: real sin >float sin ; inline
313 : cosec ( x -- y ) sin recip ; inline
315 GENERIC: sinh ( x -- y ) foldable
319 [ [ fsinh ] [ fcos ] bi* * ]
320 [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
322 M: float sinh fsinh ; inline
324 M: real sinh >float sinh ; inline
326 : cosech ( x -- y ) sinh recip ; inline
328 GENERIC: tan ( x -- y ) foldable
330 M: complex tan [ sin ] [ cos ] bi / ;
332 M: float tan ftan ; inline
334 M: real tan >float tan ; inline
336 GENERIC: tanh ( x -- y ) foldable
338 M: complex tanh [ sinh ] [ cosh ] bi / ;
340 M: float tanh ftanh ; inline
342 M: real tanh >float tanh ; inline
344 : cot ( x -- y ) tan recip ; inline
346 : coth ( x -- y ) tanh recip ; inline
349 dup sq 1 - sqrt + log ; inline
351 : asech ( x -- y ) recip acosh ; inline
354 dup sq 1 + sqrt + log ; inline
356 : acosech ( x -- y ) recip asinh ; inline
359 [ 1 + ] [ 1 - neg ] bi / log 2 / ; inline
361 : acoth ( x -- y ) recip atanh ; inline
363 : i* ( x -- y ) >rect neg swap rect> ;
365 : -i* ( x -- y ) >rect swap neg rect> ;
368 dup [-1,1]? [ >float fasin ] [ i* asinh -i* ] if ; inline
371 dup [-1,1]? [ >float facos ] [ asin pi 2 / swap - ] if ;
374 GENERIC: atan ( x -- y ) foldable
376 M: complex atan i* atanh i* ; inline
378 M: float atan fatan ; inline
380 M: real atan >float atan ; inline
382 : asec ( x -- y ) recip acos ; inline
384 : acosec ( x -- y ) recip asin ; inline
386 : acot ( x -- y ) recip atan ; inline
388 : truncate ( x -- y ) dup 1 mod - ; inline
390 GENERIC: round ( x -- y )
392 M: integer round ; inline
395 >fraction [ /mod abs 2 * ] keep >= [ dup 0 < -1 1 ? + ] when ;
397 M: float round dup sgn 2 /f + truncate ;
401 [ dup 0 < [ - 1 - ] [ - ] if ] unless-zero ; foldable
403 : ceiling ( x -- y ) neg floor neg ; foldable
405 : floor-to ( x step -- y )
406 [ [ / floor ] [ * ] bi ] unless-zero ;
408 : lerp ( a b t -- a_t ) [ over - ] dip * + ; inline
410 : roots ( x t -- seq )
411 [ [ log ] [ recip ] bi* * e^ ]
412 [ recip 2pi * 0 swap complex boa e^ ]
413 [ iota [ ^ * ] 2with map ] tri ;
415 : sigmoid ( x -- y ) neg e^ 1 + recip ; inline
417 GENERIC: signum ( x -- y )
421 M: complex signum dup abs / ;
423 MATH: copysign ( x y -- x' )
425 M: real copysign >float copysign ;
428 [ double>bits ] [ fp-sign ] bi*
429 [ 63 2^ bitor ] [ 63 2^ bitnot bitand ] if