1 ! Copyright (C) 2004, 2010 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: combinators kernel kernel.private math math.bits
4 math.constants math.libm math.order math.private sequences
8 GENERIC: sqrt ( x -- y ) foldable
12 [ neg fsqrt [ 0.0 ] dip rect> ] [ fsqrt ] if ; inline
14 : factor-2s ( n -- r s )
15 ! factor an integer into 2^r * s
17 [ 0 ] dip [ dup even? ] [ [ 1 + ] [ 2/ ] bi* ] while
22 : (^fixnum) ( z w -- z^w )
26 [ [ * ] keep ] [ 1 - ] bi*
27 ] when [ sq ] [ 2/ ] bi*
28 ] until 2drop ; inline
30 : (^bignum) ( z w -- z^w )
31 make-bits 1 [ [ over * ] when [ sq ] dip ] reduce nip ; inline
34 dup fixnum? [ (^fixnum) ] [ (^bignum) ] if ; inline
36 GENERIC#: ^n 1 ( z w -- z^w ) foldable
41 [ factor-2s ] dip [ (^n) ] keep rot * shift ;
44 [ >fraction ] dip '[ _ ^n ] bi@ / ;
50 : integer^ ( x y -- z )
51 dup 0 >= [ ^n ] [ [ recip ] dip neg ^n ] if ; inline
55 : >float-rect ( z -- x y )
56 >rect [ >float ] bi@ ; inline
58 : >polar ( z -- abs arg )
59 >float-rect [ [ sq ] bi@ + fsqrt ] [ swap fatan2 ] 2bi ; inline
61 : cis ( arg -- z ) >float [ fcos ] [ fsin ] bi rect> ; inline
63 : polar> ( abs arg -- z ) cis * ; inline
65 GENERIC: e^ ( x -- e^x )
67 M: float e^ fexp ; inline
69 M: real e^ >float e^ ; inline
71 M: complex e^ >rect [ e^ ] dip polar> ; inline
75 : ^mag ( w abs arg -- magnitude )
77 [ >float swap >float fpow ]
81 : ^theta ( w abs arg -- theta )
82 [ >float-rect ] [ flog * swap ] [ * + ] tri* ; inline
84 : ^complex ( x y -- z )
85 swap >polar [ ^mag ] [ ^theta ] 3bi polar> ; inline
88 2dup [ real? ] both? [ drop 0 >= ] [ 2drop f ] if ; inline
91 swap [ 0/0. ] swap '[ 0 < 1/0. _ ? ] if-zero ; inline
93 : (^mod) ( x y n -- z )
94 [ make-bits 1 ] dip dup
95 '[ [ over * _ mod ] when [ sq _ mod ] dip ] reduce nip ; inline
101 { [ over zero? ] [ 0^ ] }
102 { [ dup integer? ] [ integer^ ] }
103 { [ 2dup real^? ] [ [ >float ] bi@ fpow ] }
107 : nth-root ( n x -- y ) swap recip ^ ; inline
109 : divisor? ( m n -- ? )
112 ERROR: non-trivial-divisor n ;
114 : mod-inv ( x n -- y )
115 [ nip ] [ gcd 1 = ] 2bi
116 [ dup 0 < [ + ] [ nip ] if ]
117 [ non-trivial-divisor ] if ; foldable
119 : ^mod ( x y n -- z )
121 [ [ [ neg ] dip ^mod ] keep mod-inv ] [ (^mod) ] if ; foldable
123 GENERIC: absq ( x -- y ) foldable
125 M: real absq sq ; inline
127 : ~abs ( x y epsilon -- ? )
130 : ~rel ( x y epsilon -- ? )
131 [ [ - abs ] 2keep [ abs ] bi@ + ] dip * <= ;
133 : ~ ( x y epsilon -- ? )
135 { [ dup zero? ] [ drop number= ] }
136 { [ dup 0 < ] [ neg ~rel ] }
140 : conjugate ( z -- z* ) >rect neg rect> ; inline
142 : arg ( z -- arg ) >float-rect swap fatan2 ; inline
145 dup complex? [ drop f ] [ abs 1 <= ] if ; inline
148 dup complex? [ drop f ] [ 1 >= ] if ; inline
150 GENERIC: frexp ( x -- y exp )
153 dup fp-special? [ dup zero? ] unless* [ 0 ] [
155 [ 0x800f,ffff,ffff,ffff bitand 0.5 double>bits bitor bits>double ]
156 [ -52 shift 0x7ff bitand 1022 - ] bi
161 dup 0 > [ 1 ] [ abs -1 ] if swap dup log2 [
162 52 swap - shift 0x000f,ffff,ffff,ffff bitand
163 0.5 double>bits bitor bits>double
164 ] [ 1 + ] bi [ * ] dip
169 GENERIC#: ldexp 1 ( x exp -- y )
172 over fp-special? [ over zero? ] unless* [ drop ] [
173 [ double>bits dup -52 shift 0x7ff bitand 1023 - ] dip +
175 { [ dup -1074 < ] [ drop 0 copysign ] }
176 { [ dup 1023 > ] [ drop 0 < -1/0. 1/0. ? ] }
178 dup -1022 < [ 52 + -52 2^ ] [ 1 ] if
179 [ -0x7ff0,0000,0000,0001 bitand ]
180 [ 1023 + 52 shift bitor bits>double ]
187 2dup [ zero? ] either? [ 2drop 0 ] [ shift ] if ;
189 GENERIC: log ( x -- y )
191 M: float log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ; inline
193 M: real log >float log ; inline
195 M: complex log >polar [ flog ] dip rect> ; inline
197 : logn ( x n -- y ) [ log ] bi@ / ;
199 GENERIC: lgamma ( x -- y )
201 M: float lgamma flgamma ;
203 M: real lgamma >float lgamma ;
207 : most-negative-finite-float ( -- x )
208 -0x1.ffff,ffff,ffff,fp1023 >integer ; inline
210 : most-positive-finite-float ( -- x )
211 0x1.ffff,ffff,ffff,fp1023 >integer ; inline
213 CONSTANT: log-2 0x1.62e42fefa39efp-1
214 CONSTANT: log10-2 0x1.34413509f79ffp-2
216 : representable-as-float? ( x -- ? )
217 most-negative-finite-float
218 most-positive-finite-float between? ; inline
220 : (bignum-log) ( n log-quot: ( x -- y ) log-2 -- log )
222 dup representable-as-float?
223 [ >float @ ] [ frexp _ [ _ * ] bi* + ] if
228 M: bignum log [ log ] log-2 (bignum-log) ;
230 GENERIC: log1+ ( x -- y )
232 M: object log1+ 1 + log ; inline
234 M: float log1+ dup -1.0 >= [ flog1+ ] [ 1.0 + 0.0 rect> log ] if ; inline
236 : 10^ ( x -- 10^x ) 10 swap ^ ; inline
238 GENERIC: log10 ( x -- y ) foldable
240 M: real log10 >float flog10 ; inline
242 M: complex log10 log 10 log / ; inline
244 M: bignum log10 [ log10 ] log10-2 (bignum-log) ;
246 GENERIC: e^-1 ( x -- e^x-1 )
253 [ 1.0 - * ] [ log / ] bi
255 ] [ e^ 1.0 - ] if ; inline
257 M: real e^-1 >float e^-1 ; inline
259 GENERIC: cos ( x -- y ) foldable
263 [ [ fcos ] [ fcosh ] bi* * ]
264 [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
266 M: float cos fcos ; inline
268 M: real cos >float cos ; inline
270 : sec ( x -- y ) cos recip ; inline
272 GENERIC: cosh ( x -- y ) foldable
276 [ [ fcosh ] [ fcos ] bi* * ]
277 [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
279 M: float cosh fcosh ; inline
281 M: real cosh >float cosh ; inline
283 : sech ( x -- y ) cosh recip ; inline
285 GENERIC: sin ( x -- y ) foldable
289 [ [ fsin ] [ fcosh ] bi* * ]
290 [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
292 M: float sin fsin ; inline
294 M: real sin >float sin ; inline
296 : cosec ( x -- y ) sin recip ; inline
298 GENERIC: sinh ( x -- y ) foldable
302 [ [ fsinh ] [ fcos ] bi* * ]
303 [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
305 M: float sinh fsinh ; inline
307 M: real sinh >float sinh ; inline
309 : cosech ( x -- y ) sinh recip ; inline
311 GENERIC: tan ( x -- y ) foldable
313 M: complex tan [ sin ] [ cos ] bi / ;
315 M: float tan ftan ; inline
317 M: real tan >float tan ; inline
319 GENERIC: tanh ( x -- y ) foldable
321 M: complex tanh [ sinh ] [ cosh ] bi / ;
323 M: float tanh ftanh ; inline
325 M: real tanh >float tanh ; inline
327 : cot ( x -- y ) tan recip ; inline
329 : coth ( x -- y ) tanh recip ; inline
332 dup sq 1 - sqrt + log ; inline
334 : asech ( x -- y ) recip acosh ; inline
337 dup sq 1 + sqrt + log ; inline
339 : acosech ( x -- y ) recip asinh ; inline
342 [ 1 + ] [ 1 - neg ] bi / log 2 / ; inline
344 : acoth ( x -- y ) recip atanh ; inline
346 : i* ( x -- y ) >rect neg swap rect> ;
348 : -i* ( x -- y ) >rect swap neg rect> ;
351 dup [-1,1]? [ >float fasin ] [ i* asinh -i* ] if ; inline
354 dup [-1,1]? [ >float facos ] [ asin pi 2 / swap - ] if ; inline
356 GENERIC: atan ( x -- y ) foldable
358 M: complex atan i* atanh i* ; inline
360 M: float atan fatan ; inline
362 M: real atan >float atan ; inline
364 : asec ( x -- y ) recip acos ; inline
366 : acosec ( x -- y ) recip asin ; inline
368 : acot ( x -- y ) recip atan ; inline
370 : deg>rad ( x -- y ) pi * 180 / ; inline
372 : rad>deg ( x -- y ) 180 * pi / ; inline
374 GENERIC: truncate ( x -- y )
376 M: real truncate dup 1 mod - ;
380 dup -52 shift 0x7ff bitand 0x3ff -
381 ! check for floats without fractional part (>= 2^52)
385 ! the float is between -1.0 and 1.0,
386 ! the result could be +/-0.0, but we will
387 ! return 0.0 instead similar to other
389 2drop 0.0 ! -63 shift zero? 0.0 -0.0 ?
391 ! Put zeroes in the correct part of the mantissa
392 0x000fffffffffffff swap neg shift bitnot bitand
396 ! check for nans and infinities and do an operation on them
397 ! to trigger fp exceptions if necessary
398 nip 0x400 = [ dup + ] when
401 GENERIC: round ( x -- y )
403 GENERIC: round-to-even ( x -- y )
405 GENERIC: round-to-odd ( x -- y )
407 M: integer round ; inline
409 M: integer round-to-even ; inline
411 M: integer round-to-odd ; inline
413 : (round-tiebreak?) ( quotient rem denom tiebreak-quot -- q ? )
414 [ [ > ] ] dip [ 2dip = and ] curry 3bi or ; inline
416 : (round-to-even?) ( quotient rem denom -- quotient ? )
417 [ >integer odd? ] (round-tiebreak?) ; inline
419 : (round-to-odd?) ( quotient rem denom -- quotient ? )
420 [ >integer even? ] (round-tiebreak?) ; inline
422 : (ratio-round) ( x round-quot -- y )
423 [ >fraction [ /mod dup swapd abs 2 * ] keep ] [ call ] bi*
424 [ swap 0 < -1 1 ? + ] [ nip ] if ; inline
426 : (float-round) ( x round-quot -- y )
427 [ dup 1 mod [ - ] keep dup swapd abs 0.5 ] [ call ] bi*
428 [ swap 0.0 < -1.0 1.0 ? + ] [ nip ] if ; inline
430 M: ratio round [ >= ] (ratio-round) ;
432 M: ratio round-to-even [ (round-to-even?) ] (ratio-round) ;
434 M: ratio round-to-odd [ (round-to-odd?) ] (ratio-round) ;
436 M: float round dup sgn 2 /f + truncate ;
438 M: float round-to-even [ (round-to-even?) ] (float-round) ;
440 M: float round-to-odd [ (round-to-odd?) ] (float-round) ;
444 [ dup 0 < [ - 1 - ] [ - ] if ] unless-zero ; foldable
446 : ceiling ( x -- y ) neg floor neg ; foldable
448 : floor-to ( x step -- y )
449 [ [ / floor ] [ * ] bi ] unless-zero ;
451 : lerp ( a b t -- a_t ) [ over - ] dip * + ; inline
453 : roots ( x t -- seq )
454 [ [ log ] [ recip ] bi* * e^ ]
455 [ recip 2pi * 0 swap complex boa e^ ]
456 [ <iota> [ ^ * ] 2with map ] tri ;
459 : sigmoid ( x -- y ) neg e^ 1 + recip ; inline
461 : logit ( x -- y ) [ ] [ 1 swap - ] bi /f log ; inline
464 GENERIC: signum ( x -- y )
468 M: complex signum dup abs / ;
470 MATH: copysign ( x y -- x' )
472 M: real copysign >float copysign ;
475 [ double>bits ] [ fp-sign ] bi*
476 [ 63 2^ bitor ] [ 63 2^ bitnot bitand ] if
479 :: integer-sqrt ( x -- n )
482 bit-length 1 - 2 /i :> c
485 c bit-length <iota> <reversed> [| s |
489 x 2 c * e - d - 1 + neg shift a /i + a!
491 a a sq x > [ 1 - ] when
496 GENERIC: (integer-log10) ( x -- n ) foldable
498 ! For 32 bits systems, we could reduce
499 ! this to the first 27 elements..
500 CONSTANT: log10-guesses {
501 0 0 0 0 1 1 1 2 2 2 3 3 3 3
502 4 4 4 5 5 5 6 6 6 6 7 7 7 8
503 8 8 9 9 9 9 10 10 10 11 11 11
504 12 12 12 12 13 13 13 14 14 14
505 15 15 15 15 16 16 16 17 17
508 ! This table will hold a few unused bignums on 32 bits systems...
509 ! It could be reduced to the first 8 elements
510 ! Note that even though the 64 bits most-positive-fixnum
511 ! is hardcoded here this table also works (by chance) for 32bit systems.
512 ! This is because there is only one power of 2 greater than the
513 ! greatest power of 10 for 27 bit unsigned integers so we don't
514 ! need to hardcode the 32 bits most-positive-fixnum. See the
515 ! table below for powers of 2 and powers of 10 around the
516 ! most-positive-fixnum.
518 ! 67108864 2^26 | 72057594037927936 2^56
519 ! 99999999 10^8 | 99999999999999999 10^17
520 ! 134217727 2^27-1 | 144115188075855872 2^57
521 ! | 288230376151711744 2^58
522 ! | 576460752303423487 2^59-1
523 CONSTANT: log10-thresholds {
524 9 99 999 9999 99999 999999
525 9999999 99999999 999999999
526 9999999999 99999999999
527 999999999999 9999999999999
528 99999999999999 999999999999999
529 9999999999999999 99999999999999999
533 : fixnum-integer-log10 ( n -- x )
534 dup (log2) { array-capacity } declare
535 log10-guesses nth-unsafe { array-capacity } declare
536 dup log10-thresholds nth-unsafe { fixnum } declare
537 rot < [ 1 + ] when ; inline
539 ! bignum-integer-log10-find-down and bignum-integer-log10-find-up
540 ! work with very bad guesses, but in practice they will never loop
542 : bignum-integer-log10-find-down ( guess 10^guess n -- log10 )
543 [ 2dup > ] [ [ [ 1 - ] [ 10 / ] bi* ] dip ] do while 2drop ;
545 : bignum-integer-log10-find-up ( guess 10^guess n -- log10 )
547 [ 2dup <= ] [ [ [ 1 + ] [ 10 * ] bi* ] dip ] while 2drop ;
549 : bignum-integer-log10-guess ( n -- guess 10^guess )
550 (log2) >integer log10-2 * >integer dup 10^ ;
552 : bignum-integer-log10 ( n -- x )
553 [ bignum-integer-log10-guess ] keep 2dup >
554 [ bignum-integer-log10-find-down ]
555 [ bignum-integer-log10-find-up ] if ; inline
557 M: fixnum (integer-log10) fixnum-integer-log10 { fixnum } declare ; inline
559 M: bignum (integer-log10) bignum-integer-log10 ; inline
565 GENERIC: (integer-log2) ( x -- n ) foldable
567 M: integer (integer-log2) (log2) ; inline
569 : ((ratio-integer-log)) ( ratio quot -- log )
570 [ >integer ] dip call ; inline
572 : (ratio-integer-log) ( ratio quot base -- log )
574 [ drop ((ratio-integer-log)) ] [
576 [ drop ((ratio-integer-log)) ] [ nip pick ^ = ] 3bi
580 M: ratio (integer-log2) [ (integer-log2) ] 2 (ratio-integer-log) ;
582 M: ratio (integer-log10) [ (integer-log10) ] 10 (ratio-integer-log) ;
586 : integer-log10 ( x -- n )
587 assert-positive (integer-log10) ; inline
589 : integer-log2 ( x -- n )
590 assert-positive (integer-log2) ; inline