]> gitweb.factorcode.org Git - factor.git/blob - basis/math/functions/functions.factor
c4b713ad16f6362eef9e413e3b16fc73b98b213d
[factor.git] / basis / math / functions / functions.factor
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 ;
5 IN: math.functions
6
7 GENERIC: sqrt ( x -- y ) foldable
8
9 M: real sqrt
10     >float dup 0.0 <
11     [ neg fsqrt [ 0.0 ] dip rect> ] [ fsqrt ] if ; inline
12
13 : factor-2s ( n -- r s )
14     ! factor an integer into 2^r * s
15     dup 0 = [ 1 ] [
16         [ 0 ] dip [ dup even? ] [ [ 1 + ] [ 2/ ] bi* ] while
17     ] if ; inline
18
19 <PRIVATE
20
21 : (^fixnum) ( z w -- z^w )
22     [ 1 ] 2dip
23     [ dup zero? ] [
24         dup odd? [
25             [ [ * ] keep ] [ 1 - ] bi*
26         ] when [ sq ] [ 2/ ] bi*
27     ] until 2drop ; inline
28
29 : (^bignum) ( z w -- z^w )
30     make-bits 1 [ [ over * ] when [ sq ] dip ] reduce nip ; inline
31
32 : (^n) ( z w -- z^w )
33     dup fixnum? [ (^fixnum) ] [ (^bignum) ] if ; inline
34
35 GENERIC#: ^n 1 ( z w -- z^w ) foldable
36
37 M: fixnum ^n (^n) ;
38
39 M: bignum ^n
40     [ factor-2s ] dip [ (^n) ] keep rot * shift ;
41
42 M: ratio ^n
43     [ >fraction ] dip '[ _ ^n ] bi@ / ;
44
45 M: float ^n (^n) ;
46
47 M: complex ^n (^n) ;
48
49 : integer^ ( x y -- z )
50     dup 0 >= [ ^n ] [ [ recip ] dip neg ^n ] if ; inline
51
52 PRIVATE>
53
54 : >float-rect ( z -- x y )
55     >rect [ >float ] bi@ ; inline
56
57 : >polar ( z -- abs arg )
58     >float-rect [ [ sq ] bi@ + fsqrt ] [ swap fatan2 ] 2bi ; inline
59
60 : cis ( arg -- z ) >float [ fcos ] [ fsin ] bi rect> ; inline
61
62 : polar> ( abs arg -- z ) cis * ; inline
63
64 GENERIC: e^ ( x -- e^x )
65
66 M: float e^ fexp ; inline
67
68 M: real e^ >float e^ ; inline
69
70 M: complex e^ >rect [ e^ ] dip polar> ; inline
71
72 <PRIVATE
73
74 : ^mag ( w abs arg -- magnitude )
75     [ >float-rect swap ]
76     [ >float swap >float fpow ]
77     [ rot * e^ /f ]
78     tri* ; inline
79
80 : ^theta ( w abs arg -- theta )
81     [ >float-rect ] [ flog * swap ] [ * + ] tri* ; inline
82
83 : ^complex ( x y -- z )
84     swap >polar [ ^mag ] [ ^theta ] 3bi polar> ; inline
85
86 : real^? ( x y -- ? )
87     2dup [ real? ] both? [ drop 0 >= ] [ 2drop f ] if ; inline
88
89 : 0^ ( zero x -- z )
90     swap [ 0/0. ] swap '[ 0 < 1/0. _ ? ] if-zero ; inline
91
92 : (^mod) ( x y n -- z )
93     [ make-bits 1 ] dip dup
94     '[ [ over * _ mod ] when [ sq _ mod ] dip ] reduce nip ; inline
95
96 PRIVATE>
97
98 : ^ ( x y -- x^y )
99     {
100         { [ over zero? ] [ 0^ ] }
101         { [ dup integer? ] [ integer^ ] }
102         { [ 2dup real^? ] [ [ >float ] bi@ fpow ] }
103         [ ^complex ]
104     } cond ; inline
105
106 : nth-root ( n x -- y ) swap recip ^ ; inline
107
108 : lcm ( a b -- c )
109     [ * ] 2keep simple-gcd /i ; foldable
110
111 : divisor? ( m n -- ? )
112     mod 0 = ; inline
113
114 ERROR: non-trivial-divisor n ;
115
116 : mod-inv ( x n -- y )
117     [ nip ] [ gcd 1 = ] 2bi
118     [ dup 0 < [ + ] [ nip ] if ]
119     [ non-trivial-divisor ] if ; foldable
120
121 : ^mod ( x y n -- z )
122     over 0 <
123     [ [ [ neg ] dip ^mod ] keep mod-inv ] [ (^mod) ] if ; foldable
124
125 GENERIC: absq ( x -- y ) foldable
126
127 M: real absq sq ; inline
128
129 : ~abs ( x y epsilon -- ? )
130     [ - abs ] dip < ;
131
132 : ~rel ( x y epsilon -- ? )
133     [ [ - abs ] 2keep [ abs ] bi@ + ] dip * <= ;
134
135 : ~ ( x y epsilon -- ? )
136     {
137         { [ dup zero? ] [ drop number= ] }
138         { [ dup 0 < ] [ neg ~rel ] }
139         [ ~abs ]
140     } cond ;
141
142 : conjugate ( z -- z* ) >rect neg rect> ; inline
143
144 : arg ( z -- arg ) >float-rect swap fatan2 ; inline
145
146 : [-1,1]? ( x -- ? )
147     dup complex? [ drop f ] [ abs 1 <= ] if ; inline
148
149 : >=1? ( x -- ? )
150     dup complex? [ drop f ] [ 1 >= ] if ; inline
151
152 GENERIC: frexp ( x -- y exp )
153
154 M: float frexp
155     dup fp-special? [ dup zero? ] unless* [ 0 ] [
156         double>bits
157         [ 0x800f,ffff,ffff,ffff bitand 0.5 double>bits bitor bits>double ]
158         [ -52 shift 0x7ff bitand 1022 - ] bi
159     ] if ; inline
160
161 M: integer frexp
162     [ 0.0 0 ] [
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
167     ] if-zero ; inline
168
169 DEFER: copysign
170
171 GENERIC#: ldexp 1 ( x exp -- y )
172
173 M: float ldexp
174     over fp-special? [ over zero? ] unless* [ drop ] [
175         [ double>bits dup -52 shift 0x7ff bitand 1023 - ] dip +
176         {
177             { [ dup -1074 < ] [ drop 0 copysign ] }
178             { [ dup 1023 > ] [ drop 0 < -1/0. 1/0. ? ] }
179             [
180                 dup -1022 < [ 52 + -52 2^ ] [ 1 ] if
181                 [ -0x7ff0,0000,0000,0001 bitand ]
182                 [ 1023 + 52 shift bitor bits>double ]
183                 [ * ] tri*
184             ]
185         } cond
186     ] if ;
187
188 M: integer ldexp
189     2dup [ zero? ] either? [ 2drop 0 ] [ shift ] if ;
190
191 GENERIC: log ( x -- y )
192
193 M: float log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ; inline
194
195 M: real log >float log ; inline
196
197 M: complex log >polar [ flog ] dip rect> ; inline
198
199 : logn ( x n -- y ) [ log ] bi@ / ;
200
201 <PRIVATE
202
203 : most-negative-finite-float ( -- x )
204     -0x1.ffff,ffff,ffff,fp1023 >integer ; inline
205
206 : most-positive-finite-float ( -- x )
207     0x1.ffff,ffff,ffff,fp1023 >integer ; inline
208
209 CONSTANT: log-2   0x1.62e42fefa39efp-1
210 CONSTANT: log10-2 0x1.34413509f79ffp-2
211
212 : representable-as-float? ( x -- ? )
213     most-negative-finite-float
214     most-positive-finite-float between? ; inline
215
216 : (bignum-log) ( n log-quot: ( x -- y ) log-2 -- log )
217     [ dup ] dip '[
218         dup representable-as-float?
219         [ >float @ ] [ frexp _ [ _ * ] bi* + ] if
220     ] call ; inline
221
222 PRIVATE>
223
224 M: bignum log [ log ] log-2 (bignum-log) ;
225
226 GENERIC: log1+ ( x -- y )
227
228 M: object log1+ 1 + log ; inline
229
230 M: float log1+ dup -1.0 >= [ flog1+ ] [ 1.0 + 0.0 rect> log ] if ; inline
231
232 : 10^ ( x -- 10^x ) 10 swap ^ ; inline
233
234 GENERIC: log10 ( x -- y ) foldable
235
236 M: real log10 >float flog10 ; inline
237
238 M: complex log10 log 10 log / ; inline
239
240 M: bignum log10 [ log10 ] log10-2 (bignum-log) ;
241
242 GENERIC: cos ( x -- y ) foldable
243
244 M: complex cos
245     >float-rect
246     [ [ fcos ] [ fcosh ] bi* * ]
247     [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
248
249 M: float cos fcos ; inline
250
251 M: real cos >float cos ; inline
252
253 : sec ( x -- y ) cos recip ; inline
254
255 GENERIC: cosh ( x -- y ) foldable
256
257 M: complex cosh
258     >float-rect
259     [ [ fcosh ] [ fcos ] bi* * ]
260     [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
261
262 M: float cosh fcosh ; inline
263
264 M: real cosh >float cosh ; inline
265
266 : sech ( x -- y ) cosh recip ; inline
267
268 GENERIC: sin ( x -- y ) foldable
269
270 M: complex sin
271     >float-rect
272     [ [ fsin ] [ fcosh ] bi* * ]
273     [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
274
275 M: float sin fsin ; inline
276
277 M: real sin >float sin ; inline
278
279 : cosec ( x -- y ) sin recip ; inline
280
281 GENERIC: sinh ( x -- y ) foldable
282
283 M: complex sinh
284     >float-rect
285     [ [ fsinh ] [ fcos ] bi* * ]
286     [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
287
288 M: float sinh fsinh ; inline
289
290 M: real sinh >float sinh ; inline
291
292 : cosech ( x -- y ) sinh recip ; inline
293
294 GENERIC: tan ( x -- y ) foldable
295
296 M: complex tan [ sin ] [ cos ] bi / ;
297
298 M: float tan ftan ; inline
299
300 M: real tan >float tan ; inline
301
302 GENERIC: tanh ( x -- y ) foldable
303
304 M: complex tanh [ sinh ] [ cosh ] bi / ;
305
306 M: float tanh ftanh ; inline
307
308 M: real tanh >float tanh ; inline
309
310 : cot ( x -- y ) tan recip ; inline
311
312 : coth ( x -- y ) tanh recip ; inline
313
314 : acosh ( x -- y )
315     dup sq 1 - sqrt + log ; inline
316
317 : asech ( x -- y ) recip acosh ; inline
318
319 : asinh ( x -- y )
320     dup sq 1 + sqrt + log ; inline
321
322 : acosech ( x -- y ) recip asinh ; inline
323
324 : atanh ( x -- y )
325     [ 1 + ] [ 1 - neg ] bi / log 2 / ; inline
326
327 : acoth ( x -- y ) recip atanh ; inline
328
329 : i* ( x -- y ) >rect neg swap rect> ;
330
331 : -i* ( x -- y ) >rect swap neg rect> ;
332
333 : asin ( x -- y )
334     dup [-1,1]? [ >float fasin ] [ i* asinh -i* ] if ; inline
335
336 : acos ( x -- y )
337     dup [-1,1]? [ >float facos ] [ asin pi 2 / swap - ] if ; inline
338
339 GENERIC: atan ( x -- y ) foldable
340
341 M: complex atan i* atanh i* ; inline
342
343 M: float atan fatan ; inline
344
345 M: real atan >float atan ; inline
346
347 : asec ( x -- y ) recip acos ; inline
348
349 : acosec ( x -- y ) recip asin ; inline
350
351 : acot ( x -- y ) recip atan ; inline
352
353 GENERIC: truncate ( x -- y )
354
355 M: real truncate dup 1 mod - ;
356
357 M: float truncate
358     dup double>bits
359     dup -52 shift 0x7ff bitand 0x3ff -
360     ! check for floats without fractional part (>= 2^52)
361     dup 52 < [
362         [ drop ] 2dip
363         dup 0 < [
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
367             ! languages
368             2drop 0.0 ! -63 shift zero? 0.0 -0.0 ?
369         ] [
370             ! Put zeroes in the correct part of the mantissa
371             0x000fffffffffffff swap neg shift bitnot bitand
372             bits>double
373         ] if
374     ] [
375         ! check for nans and infinities and do an operation on them
376         ! to trigger fp exceptions if necessary
377         nip 0x400 = [ dup + ] when
378     ] if ; inline
379
380 GENERIC: round ( x -- y )
381
382 GENERIC: round-to-even ( x -- y )
383
384 GENERIC: round-to-odd ( x -- y )
385
386 M: integer round ; inline
387
388 M: integer round-to-even ; inline
389
390 M: integer round-to-odd ; inline
391
392 : (round-tiebreak?) ( quotient rem denom tiebreak-quot -- q ? )
393     [ [ > ] ] dip [ 2dip = and ] curry 3bi or ; inline
394
395 : (round-to-even?) ( quotient rem denom -- quotient ? )
396     [ >integer odd? ] (round-tiebreak?) ; inline
397
398 : (round-to-odd?) ( quotient rem denom -- quotient ? )
399     [ >integer even? ] (round-tiebreak?) ; inline
400
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
404
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
408
409 M: ratio round [ >= ] (ratio-round) ;
410
411 M: ratio round-to-even [ (round-to-even?) ] (ratio-round) ;
412
413 M: ratio round-to-odd [ (round-to-odd?) ] (ratio-round) ;
414
415 M: float round dup sgn 2 /f + truncate ;
416
417 M: float round-to-even [ (round-to-even?) ] (float-round) ;
418
419 M: float round-to-odd [ (round-to-odd?) ] (float-round) ;
420
421 : floor ( x -- y )
422     dup 1 mod
423     [ dup 0 < [ - 1 - ] [ - ] if ] unless-zero ; foldable
424
425 : ceiling ( x -- y ) neg floor neg ; foldable
426
427 : floor-to ( x step -- y )
428     [ [ / floor ] [ * ] bi ] unless-zero ;
429
430 : lerp ( a b t -- a_t ) [ over - ] dip * + ; inline
431
432 : roots ( x t -- seq )
433     [ [ log ] [ recip ] bi* * e^ ]
434     [ recip 2pi * 0 swap complex boa e^ ]
435     [ <iota> [ ^ * ] 2with map ] tri ;
436
437 : sigmoid ( x -- y ) neg e^ 1 + recip ; inline
438
439 GENERIC: signum ( x -- y )
440
441 M: real signum sgn ;
442
443 M: complex signum dup abs / ;
444
445 MATH: copysign ( x y -- x' )
446
447 M: real copysign >float copysign ;
448
449 M: float copysign
450     [ double>bits ] [ fp-sign ] bi*
451     [ 63 2^ bitor ] [ 63 2^ bitnot bitand ] if
452     bits>double ;