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