1 ! Copyright (C) 2004, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: combinators fry kernel math math.bits math.constants
4 math.libm math.order math.private sequences ;
7 GENERIC: sqrt ( x -- y ) foldable
11 [ neg fsqrt [ 0.0 ] dip rect> ] [ fsqrt ] if ; inline
13 : factor-2s ( n -- r s )
14 ! factor an integer into 2^r * s
16 [ 0 ] dip [ dup even? ] [ [ 1 + ] [ 2/ ] bi* ] while
21 : (^fixnum) ( z w -- z^w )
25 [ [ * ] keep ] [ 1 - ] bi*
26 ] when [ sq ] [ 2/ ] bi*
27 ] until 2drop ; inline
29 : (^bignum) ( z w -- z^w )
30 make-bits 1 [ [ over * ] when [ sq ] dip ] reduce nip ; inline
33 dup fixnum? [ (^fixnum) ] [ (^bignum) ] if ; inline
35 GENERIC#: ^n 1 ( z w -- z^w ) foldable
40 [ factor-2s ] dip [ (^n) ] keep rot * shift ;
43 [ >fraction ] dip '[ _ ^n ] bi@ / ;
49 : integer^ ( x y -- z )
50 dup 0 >= [ ^n ] [ [ recip ] dip neg ^n ] if ; inline
54 : >float-rect ( z -- x y )
55 >rect [ >float ] bi@ ; inline
57 : >polar ( z -- abs arg )
58 >float-rect [ [ sq ] bi@ + fsqrt ] [ swap fatan2 ] 2bi ; inline
60 : cis ( arg -- z ) >float [ fcos ] [ fsin ] bi rect> ; inline
62 : polar> ( abs arg -- z ) cis * ; inline
64 GENERIC: e^ ( x -- e^x )
66 M: float e^ fexp ; inline
68 M: real e^ >float e^ ; inline
70 M: complex e^ >rect [ e^ ] dip polar> ; inline
74 : ^mag ( w abs arg -- magnitude )
76 [ >float swap >float fpow ]
80 : ^theta ( w abs arg -- theta )
81 [ >float-rect ] [ flog * swap ] [ * + ] tri* ; inline
83 : ^complex ( x y -- z )
84 swap >polar [ ^mag ] [ ^theta ] 3bi polar> ; inline
87 2dup [ real? ] both? [ drop 0 >= ] [ 2drop f ] if ; inline
90 swap [ 0/0. ] swap '[ 0 < 1/0. _ ? ] if-zero ; inline
92 : (^mod) ( x y n -- z )
93 [ make-bits 1 ] dip dup
94 '[ [ over * _ mod ] when [ sq _ mod ] dip ] reduce nip ; inline
100 { [ over zero? ] [ 0^ ] }
101 { [ dup integer? ] [ integer^ ] }
102 { [ 2dup real^? ] [ [ >float ] bi@ fpow ] }
106 : nth-root ( n x -- y ) swap recip ^ ; inline
109 [ * ] 2keep simple-gcd /i ; foldable
111 : divisor? ( m n -- ? )
114 ERROR: non-trivial-divisor n ;
116 : mod-inv ( x n -- y )
117 [ nip ] [ gcd 1 = ] 2bi
118 [ dup 0 < [ + ] [ nip ] if ]
119 [ non-trivial-divisor ] if ; foldable
121 : ^mod ( x y n -- z )
123 [ [ [ neg ] dip ^mod ] keep mod-inv ] [ (^mod) ] if ; foldable
125 GENERIC: absq ( x -- y ) foldable
127 M: real absq sq ; inline
129 : ~abs ( x y epsilon -- ? )
132 : ~rel ( x y epsilon -- ? )
133 [ [ - abs ] 2keep [ abs ] bi@ + ] dip * <= ;
135 : ~ ( x y epsilon -- ? )
137 { [ dup zero? ] [ drop number= ] }
138 { [ dup 0 < ] [ neg ~rel ] }
142 : conjugate ( z -- z* ) >rect neg rect> ; inline
144 : arg ( z -- arg ) >float-rect swap fatan2 ; inline
147 dup complex? [ drop f ] [ abs 1 <= ] if ; inline
150 dup complex? [ drop f ] [ 1 >= ] if ; inline
152 GENERIC: frexp ( x -- y exp )
155 dup fp-special? [ dup zero? ] unless* [ 0 ] [
157 [ 0x800f,ffff,ffff,ffff bitand 0.5 double>bits bitor bits>double ]
158 [ -52 shift 0x7ff bitand 1022 - ] bi
163 dup 0 > [ 1 ] [ abs -1 ] if swap dup log2 [
164 52 swap - shift 0x000f,ffff,ffff,ffff bitand
165 0.5 double>bits bitor bits>double
166 ] [ 1 + ] bi [ * ] dip
171 GENERIC#: ldexp 1 ( x exp -- y )
174 over fp-special? [ over zero? ] unless* [ drop ] [
175 [ double>bits dup -52 shift 0x7ff bitand 1023 - ] dip +
177 { [ dup -1074 < ] [ drop 0 copysign ] }
178 { [ dup 1023 > ] [ drop 0 < -1/0. 1/0. ? ] }
180 dup -1022 < [ 52 + -52 2^ ] [ 1 ] if
181 [ -0x7ff0,0000,0000,0001 bitand ]
182 [ 1023 + 52 shift bitor bits>double ]
189 2dup [ zero? ] either? [ 2drop 0 ] [ shift ] if ;
191 GENERIC: log ( x -- y )
193 M: float log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ; inline
195 M: real log >float log ; inline
197 M: complex log >polar [ flog ] dip rect> ; inline
199 : logn ( x n -- y ) [ log ] bi@ / ;
203 : most-negative-finite-float ( -- x )
204 -0x1.ffff,ffff,ffff,fp1023 >integer ; inline
206 : most-positive-finite-float ( -- x )
207 0x1.ffff,ffff,ffff,fp1023 >integer ; inline
209 CONSTANT: log-2 0x1.62e42fefa39efp-1
210 CONSTANT: log10-2 0x1.34413509f79ffp-2
212 : representable-as-float? ( x -- ? )
213 most-negative-finite-float
214 most-positive-finite-float between? ; inline
216 : (bignum-log) ( n log-quot: ( x -- y ) log-2 -- log )
218 dup representable-as-float?
219 [ >float @ ] [ frexp _ [ _ * ] bi* + ] if
224 M: bignum log [ log ] log-2 (bignum-log) ;
226 GENERIC: log1+ ( x -- y )
228 M: object log1+ 1 + log ; inline
230 M: float log1+ dup -1.0 >= [ flog1+ ] [ 1.0 + 0.0 rect> log ] if ; inline
232 : 10^ ( x -- 10^x ) 10 swap ^ ; inline
234 GENERIC: log10 ( x -- y ) foldable
236 M: real log10 >float flog10 ; inline
238 M: complex log10 log 10 log / ; inline
240 M: bignum log10 [ log10 ] log10-2 (bignum-log) ;
242 GENERIC: cos ( x -- y ) foldable
246 [ [ fcos ] [ fcosh ] bi* * ]
247 [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
249 M: float cos fcos ; inline
251 M: real cos >float cos ; inline
253 : sec ( x -- y ) cos recip ; inline
255 GENERIC: cosh ( x -- y ) foldable
259 [ [ fcosh ] [ fcos ] bi* * ]
260 [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
262 M: float cosh fcosh ; inline
264 M: real cosh >float cosh ; inline
266 : sech ( x -- y ) cosh recip ; inline
268 GENERIC: sin ( x -- y ) foldable
272 [ [ fsin ] [ fcosh ] bi* * ]
273 [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
275 M: float sin fsin ; inline
277 M: real sin >float sin ; inline
279 : cosec ( x -- y ) sin recip ; inline
281 GENERIC: sinh ( x -- y ) foldable
285 [ [ fsinh ] [ fcos ] bi* * ]
286 [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
288 M: float sinh fsinh ; inline
290 M: real sinh >float sinh ; inline
292 : cosech ( x -- y ) sinh recip ; inline
294 GENERIC: tan ( x -- y ) foldable
296 M: complex tan [ sin ] [ cos ] bi / ;
298 M: float tan ftan ; inline
300 M: real tan >float tan ; inline
302 GENERIC: tanh ( x -- y ) foldable
304 M: complex tanh [ sinh ] [ cosh ] bi / ;
306 M: float tanh ftanh ; inline
308 M: real tanh >float tanh ; inline
310 : cot ( x -- y ) tan recip ; inline
312 : coth ( x -- y ) tanh recip ; inline
315 dup sq 1 - sqrt + log ; inline
317 : asech ( x -- y ) recip acosh ; inline
320 dup sq 1 + sqrt + log ; inline
322 : acosech ( x -- y ) recip asinh ; inline
325 [ 1 + ] [ 1 - neg ] bi / log 2 / ; inline
327 : acoth ( x -- y ) recip atanh ; inline
329 : i* ( x -- y ) >rect neg swap rect> ;
331 : -i* ( x -- y ) >rect swap neg rect> ;
334 dup [-1,1]? [ >float fasin ] [ i* asinh -i* ] if ; inline
337 dup [-1,1]? [ >float facos ] [ asin pi 2 / swap - ] if ; inline
339 GENERIC: atan ( x -- y ) foldable
341 M: complex atan i* atanh i* ; inline
343 M: float atan fatan ; inline
345 M: real atan >float atan ; inline
347 : asec ( x -- y ) recip acos ; inline
349 : acosec ( x -- y ) recip asin ; inline
351 : acot ( x -- y ) recip atan ; inline
353 GENERIC: truncate ( x -- y )
355 M: real truncate dup 1 mod - ;
359 dup -52 shift 0x7ff bitand 0x3ff -
360 ! check for floats without fractional part (>= 2^52)
364 ! the float is between -1.0 and 1.0,
365 ! the result could be +/-0.0, but we will
366 ! return 0.0 instead similar to other
368 2drop 0.0 ! -63 shift zero? 0.0 -0.0 ?
370 ! Put zeroes in the correct part of the mantissa
371 0x000fffffffffffff swap neg shift bitnot bitand
375 ! check for nans and infinities and do an operation on them
376 ! to trigger fp exceptions if necessary
377 nip 0x400 = [ dup + ] when
380 GENERIC: round ( x -- y )
382 GENERIC: round-to-even ( x -- y )
384 GENERIC: round-to-odd ( x -- y )
386 M: integer round ; inline
388 M: integer round-to-even ; inline
390 M: integer round-to-odd ; inline
392 : (round-tiebreak?) ( quotient rem denom tiebreak-quot -- q ? )
393 [ [ > ] ] dip [ 2dip = and ] curry 3bi or ; inline
395 : (round-to-even?) ( quotient rem denom -- quotient ? )
396 [ >integer odd? ] (round-tiebreak?) ; inline
398 : (round-to-odd?) ( quotient rem denom -- quotient ? )
399 [ >integer even? ] (round-tiebreak?) ; inline
401 : (ratio-round) ( x round-quot -- y )
402 [ >fraction [ /mod dup swapd abs 2 * ] keep ] [ call ] bi*
403 [ swap 0 < -1 1 ? + ] [ nip ] if ; inline
405 : (float-round) ( x round-quot -- y )
406 [ dup 1 mod [ - ] keep dup swapd abs 0.5 ] [ call ] bi*
407 [ swap 0.0 < -1.0 1.0 ? + ] [ nip ] if ; inline
409 M: ratio round [ >= ] (ratio-round) ;
411 M: ratio round-to-even [ (round-to-even?) ] (ratio-round) ;
413 M: ratio round-to-odd [ (round-to-odd?) ] (ratio-round) ;
415 M: float round dup sgn 2 /f + truncate ;
417 M: float round-to-even [ (round-to-even?) ] (float-round) ;
419 M: float round-to-odd [ (round-to-odd?) ] (float-round) ;
423 [ dup 0 < [ - 1 - ] [ - ] if ] unless-zero ; foldable
425 : ceiling ( x -- y ) neg floor neg ; foldable
427 : floor-to ( x step -- y )
428 [ [ / floor ] [ * ] bi ] unless-zero ;
430 : lerp ( a b t -- a_t ) [ over - ] dip * + ; inline
432 : roots ( x t -- seq )
433 [ [ log ] [ recip ] bi* * e^ ]
434 [ recip 2pi * 0 swap complex boa e^ ]
435 [ <iota> [ ^ * ] 2with map ] tri ;
437 : sigmoid ( x -- y ) neg e^ 1 + recip ; inline
439 GENERIC: signum ( x -- y )
443 M: complex signum dup abs / ;
445 MATH: copysign ( x y -- x' )
447 M: real copysign >float copysign ;
450 [ double>bits ] [ fp-sign ] bi*
451 [ 63 2^ bitor ] [ 63 2^ bitnot bitand ] if