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