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