]> gitweb.factorcode.org Git - factor.git/blob - basis/math/functions/functions.factor
math.functions: faster gcd means faster ratios.
[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 : (gcd*) ( a b -- c )
100     [ [ mod ] keep swap (gcd*) ] unless-zero ; inline recursive
101
102 PRIVATE>
103
104 : ^ ( x y -- z )
105     {
106         { [ over zero? ] [ 0^ ] }
107         { [ dup integer? ] [ integer^ ] }
108         { [ 2dup real^? ] [ [ >float ] bi@ fpow ] }
109         [ ^complex ]
110     } cond ; inline
111
112 : nth-root ( n x -- y ) swap recip ^ ; inline
113
114 : gcd ( x y -- a d )
115     [ 0 1 ] 2dip (gcd) dup 0 < [ neg ] when ; foldable
116
117 : gcd* ( a b -- c )
118     (gcd*) dup 0 < [ neg ] when ; foldable
119
120 : lcm ( a b -- c )
121     [ * ] 2keep gcd* /i ; foldable
122
123 : divisor? ( m n -- ? )
124     mod 0 = ;
125
126 ERROR: non-trivial-divisor n ;
127
128 : mod-inv ( x n -- y )
129     [ nip ] [ gcd 1 = ] 2bi
130     [ dup 0 < [ + ] [ nip ] if ]
131     [ non-trivial-divisor ] if ; foldable
132
133 : ^mod ( x y n -- z )
134     over 0 <
135     [ [ [ neg ] dip ^mod ] keep mod-inv ] [ (^mod) ] if ; foldable
136
137 GENERIC: absq ( x -- y ) foldable
138
139 M: real absq sq ; inline
140
141 : ~abs ( x y epsilon -- ? )
142     [ - abs ] dip < ;
143
144 : ~rel ( x y epsilon -- ? )
145     [ [ - abs ] 2keep [ abs ] bi@ + ] dip * <= ;
146
147 : ~ ( x y epsilon -- ? )
148     {
149         { [ dup zero? ] [ drop number= ] }
150         { [ dup 0 < ] [ neg ~rel ] }
151         [ ~abs ]
152     } cond ;
153
154 : conjugate ( z -- z* ) >rect neg rect> ; inline
155
156 : arg ( z -- arg ) >float-rect swap fatan2 ; inline
157
158 : [-1,1]? ( x -- ? )
159     dup complex? [ drop f ] [ abs 1 <= ] if ; inline
160
161 : >=1? ( x -- ? )
162     dup complex? [ drop f ] [ 1 >= ] if ; inline
163
164 GENERIC: frexp ( x -- y exp )
165
166 M: float frexp
167     dup fp-special? [ dup zero? ] unless* [ 0 ] [
168         double>bits
169         [ HEX: 800f,ffff,ffff,ffff bitand 0.5 double>bits bitor bits>double ]
170         [ -52 shift HEX: 7ff bitand 1022 - ] bi
171     ] if ; inline
172
173 M: integer frexp
174     [ 0.0 0 ] [
175         dup 0 > [ 1 ] [ abs -1 ] if swap dup log2 [
176             52 swap - shift HEX: 000f,ffff,ffff,ffff bitand
177             0.5 double>bits bitor bits>double
178         ] [ 1 + ] bi [ * ] dip
179     ] if-zero ; inline
180
181 GENERIC: log ( x -- y )
182
183 M: float log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ; inline
184
185 M: real log >float log ; inline
186
187 M: complex log >polar [ flog ] dip rect> ; inline
188
189 <PRIVATE
190
191 : most-negative-finite-float ( -- x )
192     HEX: -1.ffff,ffff,ffff,fp1023 >integer ; inline
193 : most-positive-finite-float ( -- x )
194     HEX:  1.ffff,ffff,ffff,fp1023 >integer ; inline
195 CONSTANT: log-2   HEX: 1.62e42fefa39efp-1
196 CONSTANT: log10-2 HEX: 1.34413509f79ffp-2
197
198 : (representable-as-float?) ( x -- ? )
199     most-negative-finite-float
200     most-positive-finite-float between? ; inline
201
202 : (bignum-log) ( n log-quot: ( x -- y ) log-2 -- log )
203     [ dup ] dip '[
204         dup (representable-as-float?)
205         [ >float @ ] [ frexp [ @ ] [ _ * ] bi* + ] if
206     ] call ; inline
207
208 PRIVATE>
209
210 M: bignum log [ log ] log-2 (bignum-log) ;
211
212 GENERIC: log1+ ( x -- y )
213
214 M: object log1+ 1 + log ; inline
215
216 M: float log1+ dup -1.0 >= [ flog1+ ] [ 1.0 + 0.0 rect> log ] if ; inline
217
218 : 10^ ( x -- y ) 10 swap ^ ; inline
219
220 GENERIC: log10 ( x -- y ) foldable
221
222 M: real log10 >float flog10 ; inline
223
224 M: complex log10 log 10 log / ; inline
225
226 M: bignum log10 [ log10 ] log10-2 (bignum-log) ;
227
228 GENERIC: cos ( x -- y ) foldable
229
230 M: complex cos
231     >float-rect
232     [ [ fcos ] [ fcosh ] bi* * ]
233     [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
234
235 M: float cos fcos ; inline
236
237 M: real cos >float cos ; inline
238
239 : sec ( x -- y ) cos recip ; inline
240
241 GENERIC: cosh ( x -- y ) foldable
242
243 M: complex cosh
244     >float-rect
245     [ [ fcosh ] [ fcos ] bi* * ]
246     [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
247
248 M: float cosh fcosh ; inline
249
250 M: real cosh >float cosh ; inline
251
252 : sech ( x -- y ) cosh recip ; inline
253
254 GENERIC: sin ( x -- y ) foldable
255
256 M: complex sin
257     >float-rect
258     [ [ fsin ] [ fcosh ] bi* * ]
259     [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
260
261 M: float sin fsin ; inline
262
263 M: real sin >float sin ; inline
264
265 : cosec ( x -- y ) sin recip ; inline
266
267 GENERIC: sinh ( x -- y ) foldable
268
269 M: complex sinh
270     >float-rect
271     [ [ fsinh ] [ fcos ] bi* * ]
272     [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
273
274 M: float sinh fsinh ; inline
275
276 M: real sinh >float sinh ; inline
277
278 : cosech ( x -- y ) sinh recip ; inline
279
280 GENERIC: tan ( x -- y ) foldable
281
282 M: complex tan [ sin ] [ cos ] bi / ;
283
284 M: float tan ftan ; inline
285
286 M: real tan >float tan ; inline
287
288 GENERIC: tanh ( x -- y ) foldable
289
290 M: complex tanh [ sinh ] [ cosh ] bi / ;
291
292 M: float tanh ftanh ; inline
293
294 M: real tanh >float tanh ; inline
295
296 : cot ( x -- y ) tan recip ; inline
297
298 : coth ( x -- y ) tanh recip ; inline
299
300 : acosh ( x -- y )
301     dup sq 1 - sqrt + log ; inline
302
303 : asech ( x -- y ) recip acosh ; inline
304
305 : asinh ( x -- y )
306     dup sq 1 + sqrt + log ; inline
307
308 : acosech ( x -- y ) recip asinh ; inline
309
310 : atanh ( x -- y )
311     [ 1 + ] [ 1 - neg ] bi / log 2 / ; inline
312
313 : acoth ( x -- y ) recip atanh ; inline
314
315 : i* ( x -- y ) >rect neg swap rect> ;
316
317 : -i* ( x -- y ) >rect swap neg rect> ;
318
319 : asin ( x -- y )
320     dup [-1,1]? [ >float fasin ] [ i* asinh -i* ] if ; inline
321
322 : acos ( x -- y )
323     dup [-1,1]? [ >float facos ] [ asin pi 2 / swap - ] if ;
324     inline
325
326 GENERIC: atan ( x -- y ) foldable
327
328 M: complex atan i* atanh i* ; inline
329
330 M: float atan fatan ; inline
331
332 M: real atan >float atan ; inline
333
334 : asec ( x -- y ) recip acos ; inline
335
336 : acosec ( x -- y ) recip asin ; inline
337
338 : acot ( x -- y ) recip atan ; inline
339
340 : truncate ( x -- y ) dup 1 mod - ; inline
341
342 : round ( x -- y ) dup sgn 2 / + truncate ; inline
343
344 : floor ( x -- y )
345     dup 1 mod
346     [ ] [ dup 0 < [ - 1 - ] [ - ] if ] if-zero ; foldable
347
348 : ceiling ( x -- y ) neg floor neg ; foldable
349
350 : floor-to ( x step -- y )
351     [ [ / floor ] [ * ] bi ] unless-zero ;
352
353 : lerp ( a b t -- a_t ) [ over - ] dip * + ; inline