1 ! Copyright (C) 2004, 2010 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: combinators kernel math math.bits math.constants
4 math.libm math.order 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: e^-1 ( x -- e^x-1 )
249 [ 1.0 - * ] [ log / ] bi
251 ] [ e^ 1.0 - ] if ; inline
253 M: real e^-1 >float e^-1 ; inline
255 GENERIC: cos ( x -- y ) foldable
259 [ [ fcos ] [ fcosh ] bi* * ]
260 [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
262 M: float cos fcos ; inline
264 M: real cos >float cos ; inline
266 : sec ( x -- y ) cos recip ; inline
268 GENERIC: cosh ( x -- y ) foldable
272 [ [ fcosh ] [ fcos ] bi* * ]
273 [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
275 M: float cosh fcosh ; inline
277 M: real cosh >float cosh ; inline
279 : sech ( x -- y ) cosh recip ; inline
281 GENERIC: sin ( x -- y ) foldable
285 [ [ fsin ] [ fcosh ] bi* * ]
286 [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
288 M: float sin fsin ; inline
290 M: real sin >float sin ; inline
292 : cosec ( x -- y ) sin recip ; inline
294 GENERIC: sinh ( x -- y ) foldable
298 [ [ fsinh ] [ fcos ] bi* * ]
299 [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
301 M: float sinh fsinh ; inline
303 M: real sinh >float sinh ; inline
305 : cosech ( x -- y ) sinh recip ; inline
307 GENERIC: tan ( x -- y ) foldable
309 M: complex tan [ sin ] [ cos ] bi / ;
311 M: float tan ftan ; inline
313 M: real tan >float tan ; inline
315 GENERIC: tanh ( x -- y ) foldable
317 M: complex tanh [ sinh ] [ cosh ] bi / ;
319 M: float tanh ftanh ; inline
321 M: real tanh >float tanh ; inline
323 : cot ( x -- y ) tan recip ; inline
325 : coth ( x -- y ) tanh recip ; inline
328 dup sq 1 - sqrt + log ; inline
330 : asech ( x -- y ) recip acosh ; inline
333 dup sq 1 + sqrt + log ; inline
335 : acosech ( x -- y ) recip asinh ; inline
338 [ 1 + ] [ 1 - neg ] bi / log 2 / ; inline
340 : acoth ( x -- y ) recip atanh ; inline
342 : i* ( x -- y ) >rect neg swap rect> ;
344 : -i* ( x -- y ) >rect swap neg rect> ;
347 dup [-1,1]? [ >float fasin ] [ i* asinh -i* ] if ; inline
350 dup [-1,1]? [ >float facos ] [ asin pi 2 / swap - ] if ; inline
352 GENERIC: atan ( x -- y ) foldable
354 M: complex atan i* atanh i* ; inline
356 M: float atan fatan ; inline
358 M: real atan >float atan ; inline
360 : asec ( x -- y ) recip acos ; inline
362 : acosec ( x -- y ) recip asin ; inline
364 : acot ( x -- y ) recip atan ; inline
366 GENERIC: truncate ( x -- y )
368 M: real truncate dup 1 mod - ;
372 dup -52 shift 0x7ff bitand 0x3ff -
373 ! check for floats without fractional part (>= 2^52)
377 ! the float is between -1.0 and 1.0,
378 ! the result could be +/-0.0, but we will
379 ! return 0.0 instead similar to other
381 2drop 0.0 ! -63 shift zero? 0.0 -0.0 ?
383 ! Put zeroes in the correct part of the mantissa
384 0x000fffffffffffff swap neg shift bitnot bitand
388 ! check for nans and infinities and do an operation on them
389 ! to trigger fp exceptions if necessary
390 nip 0x400 = [ dup + ] when
393 GENERIC: round ( x -- y )
395 GENERIC: round-to-even ( x -- y )
397 GENERIC: round-to-odd ( x -- y )
399 M: integer round ; inline
401 M: integer round-to-even ; inline
403 M: integer round-to-odd ; inline
405 : (round-tiebreak?) ( quotient rem denom tiebreak-quot -- q ? )
406 [ [ > ] ] dip [ 2dip = and ] curry 3bi or ; inline
408 : (round-to-even?) ( quotient rem denom -- quotient ? )
409 [ >integer odd? ] (round-tiebreak?) ; inline
411 : (round-to-odd?) ( quotient rem denom -- quotient ? )
412 [ >integer even? ] (round-tiebreak?) ; inline
414 : (ratio-round) ( x round-quot -- y )
415 [ >fraction [ /mod dup swapd abs 2 * ] keep ] [ call ] bi*
416 [ swap 0 < -1 1 ? + ] [ nip ] if ; inline
418 : (float-round) ( x round-quot -- y )
419 [ dup 1 mod [ - ] keep dup swapd abs 0.5 ] [ call ] bi*
420 [ swap 0.0 < -1.0 1.0 ? + ] [ nip ] if ; inline
422 M: ratio round [ >= ] (ratio-round) ;
424 M: ratio round-to-even [ (round-to-even?) ] (ratio-round) ;
426 M: ratio round-to-odd [ (round-to-odd?) ] (ratio-round) ;
428 M: float round dup sgn 2 /f + truncate ;
430 M: float round-to-even [ (round-to-even?) ] (float-round) ;
432 M: float round-to-odd [ (round-to-odd?) ] (float-round) ;
436 [ dup 0 < [ - 1 - ] [ - ] if ] unless-zero ; foldable
438 : ceiling ( x -- y ) neg floor neg ; foldable
440 : floor-to ( x step -- y )
441 [ [ / floor ] [ * ] bi ] unless-zero ;
443 : lerp ( a b t -- a_t ) [ over - ] dip * + ; inline
445 : roots ( x t -- seq )
446 [ [ log ] [ recip ] bi* * e^ ]
447 [ recip 2pi * 0 swap complex boa e^ ]
448 [ <iota> [ ^ * ] 2with map ] tri ;
451 : sigmoid ( x -- y ) neg e^ 1 + recip ; inline
453 : logit ( x -- y ) [ ] [ 1 swap - ] bi /f log ; inline
456 GENERIC: signum ( x -- y )
460 M: complex signum dup abs / ;
462 MATH: copysign ( x y -- x' )
464 M: real copysign >float copysign ;
467 [ double>bits ] [ fp-sign ] bi*
468 [ 63 2^ bitor ] [ 63 2^ bitnot bitand ] if