]> gitweb.factorcode.org Git - factor.git/blob - basis/math/functions/functions.factor
mason: show git SHA1 and timestamp of last completed build
[factor.git] / basis / math / functions / functions.factor
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
5 sequences.private ;
6 IN: math.functions
7
8 GENERIC: sqrt ( x -- y ) foldable
9
10 M: real sqrt
11     >float dup 0.0 <
12     [ neg fsqrt [ 0.0 ] dip rect> ] [ fsqrt ] if ; inline
13
14 : factor-2s ( n -- r s )
15     ! factor an integer into 2^r * s
16     dup 0 = [ 1 ] [
17         [ 0 ] dip [ dup even? ] [ [ 1 + ] [ 2/ ] bi* ] while
18     ] if ; inline
19
20 <PRIVATE
21
22 : (^fixnum) ( z w -- z^w )
23     [ 1 ] 2dip
24     [ dup zero? ] [
25         dup odd? [
26             [ [ * ] keep ] [ 1 - ] bi*
27         ] when [ sq ] [ 2/ ] bi*
28     ] until 2drop ; inline
29
30 : (^bignum) ( z w -- z^w )
31     make-bits 1 [ [ over * ] when [ sq ] dip ] reduce nip ; inline
32
33 : (^n) ( z w -- z^w )
34     dup fixnum? [ (^fixnum) ] [ (^bignum) ] if ; inline
35
36 GENERIC#: ^n 1 ( z w -- z^w ) foldable
37
38 M: fixnum ^n (^n) ;
39
40 M: bignum ^n
41     [ factor-2s ] dip [ (^n) ] keep rot * shift ;
42
43 M: ratio ^n
44     [ >fraction ] dip '[ _ ^n ] bi@ / ;
45
46 M: float ^n [ >float fpow ] unless-zero ;
47
48 M: complex ^n (^n) ;
49
50 : ^integer ( x y -- z )
51     dup 0 >= [ ^n ] [ [ recip ] dip neg ^n ] if ; inline
52
53 PRIVATE>
54
55 : >float-rect ( z -- x y )
56     >rect [ >float ] bi@ ; inline
57
58 : >polar ( z -- abs arg )
59     >float-rect [ [ sq ] bi@ + fsqrt ] [ swap fatan2 ] 2bi ; inline
60
61 : cis ( arg -- z ) >float [ fcos ] [ fsin ] bi rect> ; inline
62
63 : polar> ( abs arg -- z ) cis * ; inline
64
65 GENERIC: e^ ( x -- e^x )
66
67 M: float e^ fexp ; inline
68
69 M: real e^ >float e^ ; inline
70
71 M: complex e^ >rect [ e^ ] dip polar> ; inline
72
73 <PRIVATE
74
75 : ^mag ( w abs arg -- magnitude )
76     [ >float-rect swap ]
77     [ >float swap >float fpow ]
78     [ rot * e^ /f ]
79     tri* ; inline
80
81 : ^theta ( w abs arg -- theta )
82     [ >float-rect ] [ flog * swap ] [ * + ] tri* ; inline
83
84 : ^complex ( x y -- z )
85     swap >polar [ ^mag ] [ ^theta ] 3bi polar> ; inline
86
87 : real^? ( x y -- ? )
88     2dup [ real? ] both? [ drop 0 >= ] [ 2drop f ] if ; inline
89
90 : 0^ ( zero x -- z )
91     swap [ 0/0. ] swap '[ 0 < 1/0. _ ? ] if-zero ; inline
92
93 : (^mod) ( x y n -- z )
94     [ make-bits 1 ] dip dup
95     '[ [ over * _ mod ] when [ sq _ mod ] dip ] reduce nip ; inline
96
97 PRIVATE>
98
99 : ^ ( x y -- x^y )
100     {
101         { [ over zero? ] [ 0^ ] }
102         { [ dup integer? ] [ ^integer ] }
103         { [ 2dup real^? ] [ [ >float ] bi@ fpow ] }
104         [ ^complex ]
105     } cond ; inline
106
107 : nth-root ( n x -- y ) swap recip ^ ; inline
108
109 : divisor? ( m n -- ? ) mod zero? ; inline
110
111 ERROR: non-trivial-divisor n ;
112
113 : mod-inv ( x n -- y )
114     [ nip ] [ gcd 1 = ] 2bi
115     [ dup 0 < [ + ] [ nip ] if ]
116     [ non-trivial-divisor ] if ; foldable
117
118 : ^mod ( x y n -- z )
119     over 0 <
120     [ [ [ neg ] dip ^mod ] keep mod-inv ] [ (^mod) ] if ; foldable
121
122 GENERIC: absq ( x -- y ) foldable
123
124 M: real absq sq ; inline
125
126 : ~abs ( x y epsilon -- ? )
127     [ - abs ] dip < ;
128
129 : ~rel ( x y epsilon -- ? )
130     [ [ - abs ] 2keep [ abs ] bi@ + ] dip * <= ;
131
132 : ~ ( x y epsilon -- ? )
133     {
134         { [ dup zero? ] [ drop number= ] }
135         { [ dup 0 < ] [ neg ~rel ] }
136         [ ~abs ]
137     } cond ;
138
139 : conjugate ( z -- z* ) >rect neg rect> ; inline
140
141 : arg ( z -- arg ) >float-rect swap fatan2 ; inline
142
143 : [-1,1]? ( x -- ? )
144     dup complex? [ drop f ] [ abs 1 <= ] if ; inline
145
146 : >=1? ( x -- ? )
147     dup complex? [ drop f ] [ 1 >= ] if ; inline
148
149 GENERIC: frexp ( x -- y exp )
150
151 M: float frexp
152     dup fp-special? [ dup zero? ] unless* [ 0 ] [
153         double>bits
154         [ 0x800f,ffff,ffff,ffff bitand 0.5 double>bits bitor bits>double ]
155         [ -52 shift 0x7ff bitand 1022 - ] bi
156     ] if ; inline
157
158 M: integer frexp
159     [ 0.0 0 ] [
160         dup 0 > [ 1 ] [ abs -1 ] if swap dup log2 [
161             52 swap - shift 0x000f,ffff,ffff,ffff bitand
162             0.5 double>bits bitor bits>double
163         ] [ 1 + ] bi [ * ] dip
164     ] if-zero ; inline
165
166 DEFER: copysign
167
168 GENERIC#: ldexp 1 ( x exp -- y )
169
170 M: float ldexp
171     over fp-special? [ over zero? ] unless* [ drop ] [
172         [ double>bits dup -52 shift 0x7ff bitand 1023 - ] dip +
173         {
174             { [ dup -1074 < ] [ drop 0 copysign ] }
175             { [ dup 1023 > ] [ drop 0 < -1/0. 1/0. ? ] }
176             [
177                 dup -1022 < [ 52 + -52 2^ ] [ 1 ] if
178                 [ -0x7ff0,0000,0000,0001 bitand ]
179                 [ 1023 + 52 shift bitor bits>double ]
180                 [ * ] tri*
181             ]
182         } cond
183     ] if ;
184
185 M: integer ldexp
186     2dup [ zero? ] either? [ 2drop 0 ] [ shift ] if ;
187
188 GENERIC: log ( x -- y )
189
190 M: float log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ; inline
191
192 M: real log >float log ; inline
193
194 M: complex log >polar [ flog ] dip rect> ; inline
195
196 : logn ( x n -- y ) [ log ] bi@ / ;
197
198 GENERIC: lgamma ( x -- y )
199
200 M: float lgamma flgamma ;
201
202 M: real lgamma >float lgamma ;
203
204 <PRIVATE
205
206 : most-negative-finite-float ( -- x )
207     -0x1.ffff,ffff,ffff,fp1023 >integer ; inline
208
209 : most-positive-finite-float ( -- x )
210     0x1.ffff,ffff,ffff,fp1023 >integer ; inline
211
212 CONSTANT: log-2   0x1.62e42fefa39efp-1
213 CONSTANT: log10-2 0x1.34413509f79ffp-2
214
215 : representable-as-float? ( x -- ? )
216     most-negative-finite-float
217     most-positive-finite-float between? ; inline
218
219 : (bignum-log) ( n log-quot: ( x -- y ) log-2 -- log )
220     dupd '[
221         dup representable-as-float?
222         [ >float @ ] [ frexp _ [ _ * ] bi* + ] if
223     ] call ; inline
224
225 PRIVATE>
226
227 M: bignum log [ log ] log-2 (bignum-log) ;
228
229 GENERIC: log1+ ( x -- y )
230
231 M: object log1+ 1 + log ; inline
232
233 M: float log1+ dup -1.0 >= [ flog1+ ] [ 1.0 + 0.0 rect> log ] if ; inline
234
235 : 10^ ( x -- 10^x ) 10 swap ^ ; inline
236
237 GENERIC: log10 ( x -- y ) foldable
238
239 M: real log10 >float flog10 ; inline
240
241 M: complex log10 log 10 log / ; inline
242
243 M: bignum log10 [ log10 ] log10-2 (bignum-log) ;
244
245 GENERIC: e^-1 ( x -- e^x-1 )
246
247 M: float e^-1
248     dup abs 0.7 < [
249         dup e^ dup 1.0 = [
250             drop
251         ] [
252             [ 1.0 - * ] [ log / ] bi
253         ] if
254     ] [ e^ 1.0 - ] if ; inline
255
256 M: real e^-1 >float e^-1 ; inline
257
258 GENERIC: cos ( x -- y ) foldable
259
260 M: complex cos
261     >float-rect
262     [ [ fcos ] [ fcosh ] bi* * ]
263     [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
264
265 M: float cos fcos ; inline
266
267 M: real cos >float cos ; inline
268
269 : sec ( x -- y ) cos recip ; inline
270
271 GENERIC: cosh ( x -- y ) foldable
272
273 M: complex cosh
274     >float-rect
275     [ [ fcosh ] [ fcos ] bi* * ]
276     [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
277
278 M: float cosh fcosh ; inline
279
280 M: real cosh >float cosh ; inline
281
282 : sech ( x -- y ) cosh recip ; inline
283
284 GENERIC: sin ( x -- y ) foldable
285
286 M: complex sin
287     >float-rect
288     [ [ fsin ] [ fcosh ] bi* * ]
289     [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
290
291 M: float sin fsin ; inline
292
293 M: real sin >float sin ; inline
294
295 : cosec ( x -- y ) sin recip ; inline
296
297 GENERIC: sinh ( x -- y ) foldable
298
299 M: complex sinh
300     >float-rect
301     [ [ fsinh ] [ fcos ] bi* * ]
302     [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
303
304 M: float sinh fsinh ; inline
305
306 M: real sinh >float sinh ; inline
307
308 : cosech ( x -- y ) sinh recip ; inline
309
310 GENERIC: tan ( x -- y ) foldable
311
312 M: complex tan [ sin ] [ cos ] bi / ;
313
314 M: float tan ftan ; inline
315
316 M: real tan >float tan ; inline
317
318 GENERIC: tanh ( x -- y ) foldable
319
320 M: complex tanh [ sinh ] [ cosh ] bi / ;
321
322 M: float tanh ftanh ; inline
323
324 M: real tanh >float tanh ; inline
325
326 : cot ( x -- y ) tan recip ; inline
327
328 : coth ( x -- y ) tanh recip ; inline
329
330 : acosh ( x -- y )
331     dup sq 1 - sqrt + log ; inline
332
333 : asech ( x -- y ) recip acosh ; inline
334
335 : asinh ( x -- y )
336     dup sq 1 + sqrt + log ; inline
337
338 : acosech ( x -- y ) recip asinh ; inline
339
340 : atanh ( x -- y )
341     [ 1 + ] [ 1 - neg ] bi / log 2 / ; inline
342
343 : acoth ( x -- y ) recip atanh ; inline
344
345 : i* ( x -- y ) >rect neg swap rect> ;
346
347 : -i* ( x -- y ) >rect swap neg rect> ;
348
349 : asin ( x -- y )
350     dup [-1,1]? [ >float fasin ] [ i* asinh -i* ] if ; inline
351
352 : acos ( x -- y )
353     dup [-1,1]? [ >float facos ] [ asin pi 2 / swap - ] if ; inline
354
355 GENERIC: atan ( x -- y ) foldable
356
357 M: complex atan i* atanh i* ; inline
358
359 M: float atan fatan ; inline
360
361 M: real atan >float atan ; inline
362
363 : asec ( x -- y ) recip acos ; inline
364
365 : acosec ( x -- y ) recip asin ; inline
366
367 : acot ( x -- y ) recip atan ; inline
368
369 : deg>rad ( x -- y ) pi * 180 / ; inline
370
371 : rad>deg ( x -- y ) 180 * pi / ; inline
372
373 GENERIC: truncate ( x -- y )
374
375 M: real truncate dup 1 mod - ;
376
377 M: float truncate
378     dup double>bits
379     dup -52 shift 0x7ff bitand 0x3ff -
380     ! check for floats without fractional part (>= 2^52)
381     dup 52 < [
382         nipd
383         dup 0 < [
384             ! the float is between -1.0 and 1.0,
385             ! the result could be +/-0.0, but we will
386             ! return 0.0 instead similar to other
387             ! languages
388             2drop 0.0 ! -63 shift zero? 0.0 -0.0 ?
389         ] [
390             ! Put zeroes in the correct part of the mantissa
391             0x000fffffffffffff swap neg shift bitnot bitand
392             bits>double
393         ] if
394     ] [
395         ! check for nans and infinities and do an operation on them
396         ! to trigger fp exceptions if necessary
397         nip 0x400 = [ dup + ] when
398     ] if ; inline
399
400 GENERIC: round ( x -- y )
401
402 GENERIC: round-to-even ( x -- y )
403
404 GENERIC: round-to-odd ( x -- y )
405
406 M: integer round ; inline
407
408 M: integer round-to-even ; inline
409
410 M: integer round-to-odd ; inline
411
412 : (round-tiebreak?) ( quotient rem denom tiebreak-quot -- q ? )
413     [ [ > ] ] dip [ 2dip = and ] curry 3bi or ; inline
414
415 : (round-to-even?) ( quotient rem denom -- quotient ? )
416     [ >integer odd? ] (round-tiebreak?) ; inline
417
418 : (round-to-odd?) ( quotient rem denom -- quotient ? )
419     [ >integer even? ] (round-tiebreak?) ; inline
420
421 : (ratio-round) ( x round-quot -- y )
422     [ >fraction [ /mod dup swapd abs 2 * ] keep ] [ call ] bi*
423     [ swap 0 < -1 1 ? + ] [ nip ] if ; inline
424
425 : (float-round) ( x round-quot -- y )
426     [ dup 1 mod [ - ] keep dup swapd abs 0.5 ] [ call ] bi*
427     [ swap 0.0 < -1.0 1.0 ? + ] [ nip ] if ; inline
428
429 M: ratio round [ >= ] (ratio-round) ;
430
431 M: ratio round-to-even [ (round-to-even?) ] (ratio-round) ;
432
433 M: ratio round-to-odd [ (round-to-odd?) ] (ratio-round) ;
434
435 M: float round dup sgn 2 /f + truncate ;
436
437 M: float round-to-even [ (round-to-even?) ] (float-round) ;
438
439 M: float round-to-odd [ (round-to-odd?) ] (float-round) ;
440
441 : floor ( x -- y )
442     dup 1 mod
443     [ dup 0 < [ - 1 - ] [ - ] if ] unless-zero ; foldable
444
445 : ceiling ( x -- y ) neg floor neg ; foldable
446
447 : floor-to ( x step -- y )
448     [ [ / floor ] [ * ] bi ] unless-zero ;
449
450 : lerp ( a b t -- a_t ) [ over - ] dip * + ; inline
451
452 : roots ( x t -- seq )
453     [ [ log ] [ recip ] bi* * e^ ]
454     [ recip 2pi * 0 swap complex boa e^ ]
455     [ <iota> [ ^ * ] 2with map ] tri ;
456
457 ! expit
458 : sigmoid ( x -- y ) neg e^ 1 + recip ; inline
459
460 : logit ( x -- y ) [ ] [ 1 swap - ] bi /f log ; inline
461
462
463 GENERIC: signum ( x -- y )
464
465 M: real signum sgn ;
466
467 M: complex signum dup abs / ;
468
469 MATH: copysign ( x y -- x' )
470
471 M: real copysign >float copysign ;
472
473 M: float copysign
474     [ double>bits ] [ fp-sign ] bi*
475     [ 63 2^ bitor ] [ 63 2^ bitnot bitand ] if
476     bits>double ;
477
478 :: integer-sqrt ( x -- n )
479     x [ 0 ] [
480         assert-non-negative
481         bit-length 1 - 2 /i :> c
482         1 :> a!
483         0 :> d!
484         c bit-length <iota> <reversed> [| s |
485             d :> e
486             c s neg shift d!
487             a d e - 1 - shift
488             x 2 c * e - d - 1 + neg shift a /i + a!
489         ] each
490         a a sq x > [ 1 - ] when
491     ] if-zero ;
492
493 <PRIVATE
494
495 GENERIC: (integer-log10) ( x -- n ) foldable
496
497 ! For 32 bits systems, we could reduce
498 ! this to the first 27 elements..
499 CONSTANT: log10-guesses {
500     0 0 0 0 1 1 1 2 2 2 3 3 3 3
501     4 4 4 5 5 5 6 6 6 6 7 7 7 8
502     8 8 9 9 9 9 10 10 10 11 11 11
503     12 12 12 12 13 13 13 14 14 14
504     15 15 15 15 16 16 16 17 17
505 }
506
507 ! This table will hold a few unused bignums on 32 bits systems...
508 ! It could be reduced to the first 8 elements
509 ! Note that even though the 64 bits most-positive-fixnum
510 ! is hardcoded here this table also works (by chance) for 32bit systems.
511 ! This is because there is only one power of 2 greater than the
512 ! greatest power of 10 for 27 bit unsigned integers so we don't
513 ! need to hardcode the 32 bits most-positive-fixnum. See the
514 ! table below for powers of 2 and powers of 10 around the
515 ! most-positive-fixnum.
516 !
517 ! 67108864  2^26    | 72057594037927936   2^56
518 ! 99999999  10^8    | 99999999999999999  10^17
519 ! 134217727 2^27-1  | 144115188075855872  2^57
520 !                   | 288230376151711744  2^58
521 !                   | 576460752303423487  2^59-1
522 CONSTANT: log10-thresholds {
523     9 99 999 9999 99999 999999
524     9999999 99999999 999999999
525     9999999999 99999999999
526     999999999999 9999999999999
527     99999999999999 999999999999999
528     9999999999999999 99999999999999999
529     576460752303423487
530 }
531
532 : fixnum-integer-log10 ( n -- x )
533     dup (log2) { array-capacity } declare
534     log10-guesses nth-unsafe { array-capacity } declare
535     dup log10-thresholds nth-unsafe { fixnum } declare
536     rot < [ 1 + ] when ; inline
537
538 ! bignum-integer-log10-find-down and bignum-integer-log10-find-up
539 ! work with very bad guesses, but in practice they will never loop
540 ! more than once.
541 : bignum-integer-log10-find-down ( guess 10^guess n -- log10 )
542     [ 2dup > ] [ [ [ 1 - ] [ 10 / ] bi* ] dip ] do while 2drop ;
543
544 : bignum-integer-log10-find-up ( guess 10^guess n -- log10 )
545     [ 10 * ] dip
546     [ 2dup <= ] [ [ [ 1 + ] [ 10 * ] bi* ] dip ] while 2drop ;
547
548 : bignum-integer-log10-guess ( n -- guess 10^guess )
549     (log2) >integer log10-2 * >integer dup 10^ ;
550
551 : bignum-integer-log10 ( n -- x )
552     [ bignum-integer-log10-guess ] keep 2dup >
553     [ bignum-integer-log10-find-down ]
554     [ bignum-integer-log10-find-up ] if ; inline
555
556 M: fixnum (integer-log10) fixnum-integer-log10 { fixnum } declare ; inline
557
558 M: bignum (integer-log10) bignum-integer-log10 ; inline
559
560 PRIVATE>
561
562 <PRIVATE
563
564 GENERIC: (integer-log2) ( x -- n ) foldable
565
566 M: integer (integer-log2) (log2) ; inline
567
568 : ((ratio-integer-log)) ( ratio quot -- log )
569     [ >integer ] dip call ; inline
570
571 : (ratio-integer-log) ( ratio quot base -- log )
572     pick 1 >=
573     [ drop ((ratio-integer-log)) ] [
574         [ recip ] 2dip
575         [ drop ((ratio-integer-log)) ] [ nip pick ^ = ] 3bi
576         [ 1 + ] unless neg
577     ] if ; inline
578
579 M: ratio (integer-log2) [ (integer-log2) ] 2 (ratio-integer-log) ;
580
581 M: ratio (integer-log10) [ (integer-log10) ] 10 (ratio-integer-log) ;
582
583 PRIVATE>
584
585 : integer-log10 ( x -- n )
586     assert-positive (integer-log10) ; inline
587
588 : integer-log2 ( x -- n )
589     assert-positive (integer-log2) ; inline