]> gitweb.factorcode.org Git - factor.git/blob - basis/math/functions/functions.factor
Merge branch 'master' into integer-simd
[factor.git] / basis / math / functions / functions.factor
1 ! Copyright (C) 2004, 2008 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 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 < [ neg fsqrt 0.0 swap rect> ] [ fsqrt ] if ; inline
17
18 : factor-2s ( n -- r s )
19     #! factor an integer into 2^r * s
20     dup 0 = [ 1 ] [
21         0 swap [ dup even? ] [ [ 1 + ] [ 2/ ] bi* ] while
22     ] if ; inline
23
24 <PRIVATE
25
26 GENERIC# ^n 1 ( z w -- z^w ) foldable
27
28 : (^n) ( z w -- z^w )
29     make-bits 1 [ [ dupd * ] when [ sq ] dip ] reduce nip ; inline
30
31 M: integer ^n
32     [ factor-2s ] dip [ (^n) ] keep rot * shift ;
33
34 M: ratio ^n
35     [ >fraction ] dip [ ^n ] curry bi@ / ;
36
37 M: float ^n (^n) ;
38
39 M: complex ^n (^n) ;
40
41 : integer^ ( x y -- z )
42     dup 0 > [ ^n ] [ neg ^n recip ] if ; inline
43
44 PRIVATE>
45
46 : >rect ( z -- x y )
47     [ real-part ] [ imaginary-part ] bi ; inline
48
49 : >float-rect ( z -- x y )
50     >rect [ >float ] bi@ ; inline
51
52 : >polar ( z -- abs arg )
53     >float-rect [ [ sq ] bi@ + fsqrt ] [ swap fatan2 ] 2bi ; inline
54
55 : cis ( arg -- z ) >float [ fcos ] [ fsin ] bi rect> ; inline
56
57 : polar> ( abs arg -- z ) cis * ; inline
58
59 GENERIC: exp ( x -- y )
60
61 M: float exp fexp ; inline
62
63 M: real exp >float exp ; inline
64
65 M: complex exp >rect swap fexp swap polar> ; inline
66
67 <PRIVATE
68
69 : ^mag ( w abs arg -- magnitude )
70     [ >float-rect swap ]
71     [ >float swap >float fpow ]
72     [ rot * exp /f ]
73     tri* ; inline
74
75 : ^theta ( w abs arg -- theta )
76     [ >float-rect ] [ flog * swap ] [ * + ] tri* ; inline
77
78 : ^complex ( x y -- z )
79     swap >polar [ ^mag ] [ ^theta ] 3bi polar> ; inline
80
81 : real^? ( x y -- ? )
82     2dup [ real? ] both? [ drop 0 >= ] [ 2drop f ] if ; inline
83
84 : 0^ ( x -- z )
85     [ 0/0. ] [ 0 < 1/0. 0 ? ] if-zero ; inline
86
87 : (^mod) ( n x y -- z )
88     make-bits 1 [
89         [ dupd * pick mod ] when [ sq over mod ] dip
90     ] reduce 2nip ; 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 ;
98
99 PRIVATE>
100
101 : ^ ( x y -- z )
102     {
103         { [ over 0 = ] [ nip 0^ ] }
104         { [ dup integer? ] [ integer^ ] }
105         { [ 2dup real^? ] [ [ >float ] bi@ fpow ] }
106         [ ^complex ]
107     } cond ; inline
108
109 : gcd ( x y -- a d )
110     [ 0 1 ] 2dip (gcd) dup 0 < [ neg ] when ; foldable
111
112 : lcm ( a b -- c )
113     [ * ] 2keep gcd nip /i ; foldable
114
115 : divisor? ( m n -- ? )
116     mod 0 = ;
117
118 ERROR: non-trivial-divisor n ;
119
120 : mod-inv ( x n -- y )
121     [ nip ] [ gcd 1 = ] 2bi
122     [ dup 0 < [ + ] [ nip ] if ]
123     [ non-trivial-divisor ] if ; foldable
124
125 : ^mod ( x y n -- z )
126     over 0 < [
127         [ [ neg ] dip ^mod ] keep mod-inv
128     ] [
129         -rot (^mod)
130     ] 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         { [ 2over [ fp-nan? ] either? ] [ 3drop f ] }
145         { [ dup zero? ] [ drop number= ] }
146         { [ dup 0 < ] [ neg ~rel ] }
147         [ ~abs ]
148     } cond ;
149
150 : conjugate ( z -- z* ) >rect neg rect> ; inline
151
152 : arg ( z -- arg ) >float-rect swap fatan2 ; inline
153
154 : [-1,1]? ( x -- ? )
155     dup complex? [ drop f ] [ abs 1 <= ] if ; inline
156
157 : >=1? ( x -- ? )
158     dup complex? [ drop f ] [ 1 >= ] if ; inline
159
160 GENERIC: log ( x -- y )
161
162 M: float log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ; inline
163
164 M: real log >float log ; inline
165
166 M: complex log >polar [ flog ] dip rect> ; inline
167
168 GENERIC: log1+ ( x -- y )
169
170 M: object log1+ 1 + log ; inline
171
172 M: float log1+ dup -1.0 >= [ flog1+ ] [ 1.0 + 0.0 rect> log ] if ; inline
173
174 : 10^ ( x -- y ) 10 swap ^ ; inline
175
176 GENERIC: log10 ( x -- y ) foldable
177
178 M: real log10 >float flog10 ; inline
179
180 M: complex log10 log 10 log / ; inline
181
182 GENERIC: cos ( x -- y ) foldable
183
184 M: complex cos
185     >float-rect
186     [ [ fcos ] [ fcosh ] bi* * ]
187     [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
188
189 M: float cos fcos ; inline
190
191 M: real cos >float cos ; inline
192
193 : sec ( x -- y ) cos recip ; inline
194
195 GENERIC: cosh ( x -- y ) foldable
196
197 M: complex cosh
198     >float-rect
199     [ [ fcosh ] [ fcos ] bi* * ]
200     [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
201
202 M: float cosh fcosh ; inline
203
204 M: real cosh >float cosh ; inline
205
206 : sech ( x -- y ) cosh recip ; inline
207
208 GENERIC: sin ( x -- y ) foldable
209
210 M: complex sin
211     >float-rect
212     [ [ fsin ] [ fcosh ] bi* * ]
213     [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
214
215 M: float sin fsin ; inline
216
217 M: real sin >float sin ; inline
218
219 : cosec ( x -- y ) sin recip ; inline
220
221 GENERIC: sinh ( x -- y ) foldable
222
223 M: complex sinh
224     >float-rect
225     [ [ fsinh ] [ fcos ] bi* * ]
226     [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
227
228 M: float sinh fsinh ; inline
229
230 M: real sinh >float sinh ; inline
231
232 : cosech ( x -- y ) sinh recip ; inline
233
234 GENERIC: tan ( x -- y ) foldable
235
236 M: complex tan [ sin ] [ cos ] bi / ;
237
238 M: float tan ftan ; inline
239
240 M: real tan >float tan ; inline
241
242 GENERIC: tanh ( x -- y ) foldable
243
244 M: complex tanh [ sinh ] [ cosh ] bi / ;
245
246 M: float tanh ftanh ; inline
247
248 M: real tanh >float tanh ; inline
249
250 : cot ( x -- y ) tan recip ; inline
251
252 : coth ( x -- y ) tanh recip ; inline
253
254 : acosh ( x -- y )
255     dup sq 1 - sqrt + log ; inline
256
257 : asech ( x -- y ) recip acosh ; inline
258
259 : asinh ( x -- y )
260     dup sq 1 + sqrt + log ; inline
261
262 : acosech ( x -- y ) recip asinh ; inline
263
264 : atanh ( x -- y )
265     [ 1 + ] [ 1 - neg ] bi / log 2 / ; inline
266
267 : acoth ( x -- y ) recip atanh ; inline
268
269 : i* ( x -- y ) >rect neg swap rect> ;
270
271 : -i* ( x -- y ) >rect swap neg rect> ;
272
273 : asin ( x -- y )
274     dup [-1,1]? [ >float fasin ] [ i* asinh -i* ] if ; inline
275
276 : acos ( x -- y )
277     dup [-1,1]? [ >float facos ] [ asin pi 2 / swap - ] if ;
278     inline
279
280 GENERIC: atan ( x -- y ) foldable
281
282 M: complex atan i* atanh i* ; inline
283
284 M: float atan fatan ; inline
285
286 M: real atan >float atan ; inline
287
288 : asec ( x -- y ) recip acos ; inline
289
290 : acosec ( x -- y ) recip asin ; inline
291
292 : acot ( x -- y ) recip atan ; inline
293
294 : truncate ( x -- y ) dup 1 mod - ; inline
295
296 : round ( x -- y ) dup sgn 2 / + truncate ; inline
297
298 : floor ( x -- y )
299     dup 1 mod
300     [ ] [ dup 0 < [ - 1 - ] [ - ] if ] if-zero ; foldable
301
302 : ceiling ( x -- y ) neg floor neg ; foldable
303
304 : floor-to ( x step -- y )
305     [ [ / floor ] [ * ] bi ] unless-zero ;
306
307 : lerp ( a b t -- a_t ) [ over - ] dip * + ; inline
308