]> gitweb.factorcode.org Git - factor.git/blob - basis/math/functions/functions.factor
math.functions: duplicate on-bits to avoid circular dependency on math.bitwise.
[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 macros literals ;
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 <PRIVATE
160 ! to avoid circular dependency on math.bitwise
161 : on-bits ( m -- n ) 2^ 1 - ; inline
162 PRIVATE>
163
164 GENERIC: frexp ( x -- y exp )
165
166 M: float frexp
167     dup { [ fp-special? ] [ zero? ] } 1|| [ 0 ] [
168         double>bits
169         [ HEX: 800f,ffff,ffff,ffff bitand 0.5 double>bits bitor bits>double ]
170         [ -52 shift 11 on-bits 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 52 on-bits 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 CONSTANT: most-negative-finite-float $[ -1/0. next-float >integer ]
192 CONSTANT: most-positive-finite-float $[ 1/0. prev-float >integer ]
193
194 MACRO: bignum-loghelper ( quot: ( x -- y ) -- quot )
195     dup 2 over call( x -- y ) '[
196         dup
197         most-negative-finite-float
198         most-positive-finite-float
199         between?
200         [ >float @ ] [ frexp [ @ ] [ _ * ] bi* + ] if
201     ] ;
202
203 PRIVATE>
204
205 M: bignum log [ log ] bignum-loghelper ;
206
207 GENERIC: log1+ ( x -- y )
208
209 M: object log1+ 1 + log ; inline
210
211 M: float log1+ dup -1.0 >= [ flog1+ ] [ 1.0 + 0.0 rect> log ] if ; inline
212
213 : 10^ ( x -- y ) 10 swap ^ ; inline
214
215 GENERIC: log10 ( x -- y ) foldable
216
217 M: real log10 >float flog10 ; inline
218
219 M: complex log10 log 10 log / ; inline
220
221 M: bignum log10 [ log10 ] bignum-loghelper ;
222
223 GENERIC: cos ( x -- y ) foldable
224
225 M: complex cos
226     >float-rect
227     [ [ fcos ] [ fcosh ] bi* * ]
228     [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
229
230 M: float cos fcos ; inline
231
232 M: real cos >float cos ; inline
233
234 : sec ( x -- y ) cos recip ; inline
235
236 GENERIC: cosh ( x -- y ) foldable
237
238 M: complex cosh
239     >float-rect
240     [ [ fcosh ] [ fcos ] bi* * ]
241     [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
242
243 M: float cosh fcosh ; inline
244
245 M: real cosh >float cosh ; inline
246
247 : sech ( x -- y ) cosh recip ; inline
248
249 GENERIC: sin ( x -- y ) foldable
250
251 M: complex sin
252     >float-rect
253     [ [ fsin ] [ fcosh ] bi* * ]
254     [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
255
256 M: float sin fsin ; inline
257
258 M: real sin >float sin ; inline
259
260 : cosec ( x -- y ) sin recip ; inline
261
262 GENERIC: sinh ( x -- y ) foldable
263
264 M: complex sinh
265     >float-rect
266     [ [ fsinh ] [ fcos ] bi* * ]
267     [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
268
269 M: float sinh fsinh ; inline
270
271 M: real sinh >float sinh ; inline
272
273 : cosech ( x -- y ) sinh recip ; inline
274
275 GENERIC: tan ( x -- y ) foldable
276
277 M: complex tan [ sin ] [ cos ] bi / ;
278
279 M: float tan ftan ; inline
280
281 M: real tan >float tan ; inline
282
283 GENERIC: tanh ( x -- y ) foldable
284
285 M: complex tanh [ sinh ] [ cosh ] bi / ;
286
287 M: float tanh ftanh ; inline
288
289 M: real tanh >float tanh ; inline
290
291 : cot ( x -- y ) tan recip ; inline
292
293 : coth ( x -- y ) tanh recip ; inline
294
295 : acosh ( x -- y )
296     dup sq 1 - sqrt + log ; inline
297
298 : asech ( x -- y ) recip acosh ; inline
299
300 : asinh ( x -- y )
301     dup sq 1 + sqrt + log ; inline
302
303 : acosech ( x -- y ) recip asinh ; inline
304
305 : atanh ( x -- y )
306     [ 1 + ] [ 1 - neg ] bi / log 2 / ; inline
307
308 : acoth ( x -- y ) recip atanh ; inline
309
310 : i* ( x -- y ) >rect neg swap rect> ;
311
312 : -i* ( x -- y ) >rect swap neg rect> ;
313
314 : asin ( x -- y )
315     dup [-1,1]? [ >float fasin ] [ i* asinh -i* ] if ; inline
316
317 : acos ( x -- y )
318     dup [-1,1]? [ >float facos ] [ asin pi 2 / swap - ] if ;
319     inline
320
321 GENERIC: atan ( x -- y ) foldable
322
323 M: complex atan i* atanh i* ; inline
324
325 M: float atan fatan ; inline
326
327 M: real atan >float atan ; inline
328
329 : asec ( x -- y ) recip acos ; inline
330
331 : acosec ( x -- y ) recip asin ; inline
332
333 : acot ( x -- y ) recip atan ; inline
334
335 : truncate ( x -- y ) dup 1 mod - ; inline
336
337 : round ( x -- y ) dup sgn 2 / + truncate ; inline
338
339 : floor ( x -- y )
340     dup 1 mod
341     [ ] [ dup 0 < [ - 1 - ] [ - ] if ] if-zero ; foldable
342
343 : ceiling ( x -- y ) neg floor neg ; foldable
344
345 : floor-to ( x step -- y )
346     [ [ / floor ] [ * ] bi ] unless-zero ;
347
348 : lerp ( a b t -- a_t ) [ over - ] dip * + ; inline