]> gitweb.factorcode.org Git - factor.git/blob - basis/math/functions/functions.factor
ab19faf078115ea87ad868545c89f0fad65ea075
[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 <PRIVATE
200
201 : most-negative-finite-float ( -- x )
202     -0x1.ffff,ffff,ffff,fp1023 >integer ; inline
203
204 : most-positive-finite-float ( -- x )
205     0x1.ffff,ffff,ffff,fp1023 >integer ; inline
206
207 CONSTANT: log-2   0x1.62e42fefa39efp-1
208 CONSTANT: log10-2 0x1.34413509f79ffp-2
209
210 : representable-as-float? ( x -- ? )
211     most-negative-finite-float
212     most-positive-finite-float between? ; inline
213
214 : (bignum-log) ( n log-quot: ( x -- y ) log-2 -- log )
215     [ dup ] dip '[
216         dup representable-as-float?
217         [ >float @ ] [ frexp [ @ ] [ _ * ] bi* + ] if
218     ] call ; inline
219
220 PRIVATE>
221
222 M: bignum log [ log ] log-2 (bignum-log) ;
223
224 GENERIC: log1+ ( x -- y )
225
226 M: object log1+ 1 + log ; inline
227
228 M: float log1+ dup -1.0 >= [ flog1+ ] [ 1.0 + 0.0 rect> log ] if ; inline
229
230 : 10^ ( x -- 10^x ) 10 swap ^ ; inline
231
232 GENERIC: log10 ( x -- y ) foldable
233
234 M: real log10 >float flog10 ; inline
235
236 M: complex log10 log 10 log / ; inline
237
238 M: bignum log10 [ log10 ] log10-2 (bignum-log) ;
239
240 GENERIC: cos ( x -- y ) foldable
241
242 M: complex cos
243     >float-rect
244     [ [ fcos ] [ fcosh ] bi* * ]
245     [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
246
247 M: float cos fcos ; inline
248
249 M: real cos >float cos ; inline
250
251 : sec ( x -- y ) cos recip ; inline
252
253 GENERIC: cosh ( x -- y ) foldable
254
255 M: complex cosh
256     >float-rect
257     [ [ fcosh ] [ fcos ] bi* * ]
258     [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
259
260 M: float cosh fcosh ; inline
261
262 M: real cosh >float cosh ; inline
263
264 : sech ( x -- y ) cosh recip ; inline
265
266 GENERIC: sin ( x -- y ) foldable
267
268 M: complex sin
269     >float-rect
270     [ [ fsin ] [ fcosh ] bi* * ]
271     [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
272
273 M: float sin fsin ; inline
274
275 M: real sin >float sin ; inline
276
277 : cosec ( x -- y ) sin recip ; inline
278
279 GENERIC: sinh ( x -- y ) foldable
280
281 M: complex sinh
282     >float-rect
283     [ [ fsinh ] [ fcos ] bi* * ]
284     [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
285
286 M: float sinh fsinh ; inline
287
288 M: real sinh >float sinh ; inline
289
290 : cosech ( x -- y ) sinh recip ; inline
291
292 GENERIC: tan ( x -- y ) foldable
293
294 M: complex tan [ sin ] [ cos ] bi / ;
295
296 M: float tan ftan ; inline
297
298 M: real tan >float tan ; inline
299
300 GENERIC: tanh ( x -- y ) foldable
301
302 M: complex tanh [ sinh ] [ cosh ] bi / ;
303
304 M: float tanh ftanh ; inline
305
306 M: real tanh >float tanh ; inline
307
308 : cot ( x -- y ) tan recip ; inline
309
310 : coth ( x -- y ) tanh recip ; inline
311
312 : acosh ( x -- y )
313     dup sq 1 - sqrt + log ; inline
314
315 : asech ( x -- y ) recip acosh ; inline
316
317 : asinh ( x -- y )
318     dup sq 1 + sqrt + log ; inline
319
320 : acosech ( x -- y ) recip asinh ; inline
321
322 : atanh ( x -- y )
323     [ 1 + ] [ 1 - neg ] bi / log 2 / ; inline
324
325 : acoth ( x -- y ) recip atanh ; inline
326
327 : i* ( x -- y ) >rect neg swap rect> ;
328
329 : -i* ( x -- y ) >rect swap neg rect> ;
330
331 : asin ( x -- y )
332     dup [-1,1]? [ >float fasin ] [ i* asinh -i* ] if ; inline
333
334 : acos ( x -- y )
335     dup [-1,1]? [ >float facos ] [ asin pi 2 / swap - ] if ; inline
336
337 GENERIC: atan ( x -- y ) foldable
338
339 M: complex atan i* atanh i* ; inline
340
341 M: float atan fatan ; inline
342
343 M: real atan >float atan ; inline
344
345 : asec ( x -- y ) recip acos ; inline
346
347 : acosec ( x -- y ) recip asin ; inline
348
349 : acot ( x -- y ) recip atan ; inline
350
351 GENERIC: truncate ( x -- y )
352
353 M: real truncate dup 1 mod - ;
354
355 M: float truncate
356     dup double>bits
357     dup -52 shift 0x7ff bitand 0x3ff -
358     ! check for floats without fractional part (>= 2^52)
359     dup 52 < [
360         [ drop ] 2dip
361         dup 0 < [
362             ! the float is between -1.0 and 1.0,
363             ! the result could be +/-0.0, but we will
364             ! return 0.0 instead similar to other
365             ! languages
366             2drop 0.0 ! -63 shift zero? 0.0 -0.0 ?
367         ] [
368             ! Put zeroes in the correct part of the mantissa
369             0x000fffffffffffff swap neg shift bitnot bitand
370             bits>double
371         ] if
372     ] [
373         ! check for nans and infinities and do an operation on them
374         ! to trigger fp exceptions if necessary
375         nip 0x400 = [ dup + ] when
376     ] if ; inline
377
378 GENERIC: round ( x -- y )
379
380 GENERIC: round-to-even ( x -- y )
381
382 GENERIC: round-to-odd ( x -- y )
383
384 M: integer round ; inline
385
386 M: integer round-to-even ; inline
387
388 M: integer round-to-odd ; inline
389
390 : (round-tiebreak?) ( quotient rem denom tiebreak-quot -- q ? )
391     [ [ > ] ] dip [ 2dip = and ] curry 3bi or ; inline
392
393 : (round-to-even?) ( quotient rem denom -- quotient ? )
394     [ >integer odd? ] (round-tiebreak?) ; inline
395
396 : (round-to-odd?) ( quotient rem denom -- quotient ? )
397     [ >integer even? ] (round-tiebreak?) ; inline
398
399 : (ratio-round) ( x round-quot -- y )
400     [ >fraction [ /mod dup swapd abs 2 * ] keep ] [ call ] bi*
401     [ swap 0 < -1 1 ? + ] [ nip ] if ; inline
402
403 : (float-round) ( x round-quot -- y )
404     [ dup 1 mod [ - ] keep dup swapd abs 0.5 ] [ call ] bi*
405     [ swap 0.0 < -1.0 1.0 ? + ] [ nip ] if ; inline
406
407 M: ratio round [ >= ] (ratio-round) ;
408
409 M: ratio round-to-even [ (round-to-even?) ] (ratio-round) ;
410
411 M: ratio round-to-odd [ (round-to-odd?) ] (ratio-round) ;
412
413 M: float round dup sgn 2 /f + truncate ;
414
415 M: float round-to-even [ (round-to-even?) ] (float-round) ;
416
417 M: float round-to-odd [ (round-to-odd?) ] (float-round) ;
418
419 : floor ( x -- y )
420     dup 1 mod
421     [ dup 0 < [ - 1 - ] [ - ] if ] unless-zero ; foldable
422
423 : ceiling ( x -- y ) neg floor neg ; foldable
424
425 : floor-to ( x step -- y )
426     [ [ / floor ] [ * ] bi ] unless-zero ;
427
428 : lerp ( a b t -- a_t ) [ over - ] dip * + ; inline
429
430 : roots ( x t -- seq )
431     [ [ log ] [ recip ] bi* * e^ ]
432     [ recip 2pi * 0 swap complex boa e^ ]
433     [ <iota> [ ^ * ] 2with map ] tri ;
434
435 : sigmoid ( x -- y ) neg e^ 1 + recip ; inline
436
437 GENERIC: signum ( x -- y )
438
439 M: real signum sgn ;
440
441 M: complex signum dup abs / ;
442
443 MATH: copysign ( x y -- x' )
444
445 M: real copysign >float copysign ;
446
447 M: float copysign
448     [ double>bits ] [ fp-sign ] bi*
449     [ 63 2^ bitor ] [ 63 2^ bitnot bitand ] if
450     bits>double ;