]> gitweb.factorcode.org Git - factor.git/blob - basis/math/functions/functions.factor
Merge branch 'master' of git://factorcode.org/git/factor
[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         { [ 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: log ( x -- y )
160
161 M: float log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ; inline
162
163 M: real log >float log ; inline
164
165 M: complex log >polar [ flog ] dip rect> ; inline
166
167 GENERIC: log1+ ( x -- y )
168
169 M: object log1+ 1 + log ; inline
170
171 M: float log1+ dup -1.0 >= [ flog1+ ] [ 1.0 + 0.0 rect> log ] if ; inline
172
173 : 10^ ( x -- y ) 10 swap ^ ; inline
174
175 GENERIC: log10 ( x -- y ) foldable
176
177 M: real log10 >float flog10 ; inline
178
179 M: complex log10 log 10 log / ; inline
180
181 GENERIC: cos ( x -- y ) foldable
182
183 M: complex cos
184     >float-rect
185     [ [ fcos ] [ fcosh ] bi* * ]
186     [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
187
188 M: float cos fcos ; inline
189
190 M: real cos >float cos ; inline
191
192 : sec ( x -- y ) cos recip ; inline
193
194 GENERIC: cosh ( x -- y ) foldable
195
196 M: complex cosh
197     >float-rect
198     [ [ fcosh ] [ fcos ] bi* * ]
199     [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
200
201 M: float cosh fcosh ; inline
202
203 M: real cosh >float cosh ; inline
204
205 : sech ( x -- y ) cosh recip ; inline
206
207 GENERIC: sin ( x -- y ) foldable
208
209 M: complex sin
210     >float-rect
211     [ [ fsin ] [ fcosh ] bi* * ]
212     [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
213
214 M: float sin fsin ; inline
215
216 M: real sin >float sin ; inline
217
218 : cosec ( x -- y ) sin recip ; inline
219
220 GENERIC: sinh ( x -- y ) foldable
221
222 M: complex sinh
223     >float-rect
224     [ [ fsinh ] [ fcos ] bi* * ]
225     [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
226
227 M: float sinh fsinh ; inline
228
229 M: real sinh >float sinh ; inline
230
231 : cosech ( x -- y ) sinh recip ; inline
232
233 GENERIC: tan ( x -- y ) foldable
234
235 M: complex tan [ sin ] [ cos ] bi / ;
236
237 M: float tan ftan ; inline
238
239 M: real tan >float tan ; inline
240
241 GENERIC: tanh ( x -- y ) foldable
242
243 M: complex tanh [ sinh ] [ cosh ] bi / ;
244
245 M: float tanh ftanh ; inline
246
247 M: real tanh >float tanh ; inline
248
249 : cot ( x -- y ) tan recip ; inline
250
251 : coth ( x -- y ) tanh recip ; inline
252
253 : acosh ( x -- y )
254     dup sq 1 - sqrt + log ; inline
255
256 : asech ( x -- y ) recip acosh ; inline
257
258 : asinh ( x -- y )
259     dup sq 1 + sqrt + log ; inline
260
261 : acosech ( x -- y ) recip asinh ; inline
262
263 : atanh ( x -- y )
264     [ 1 + ] [ 1 - neg ] bi / log 2 / ; inline
265
266 : acoth ( x -- y ) recip atanh ; inline
267
268 : i* ( x -- y ) >rect neg swap rect> ;
269
270 : -i* ( x -- y ) >rect swap neg rect> ;
271
272 : asin ( x -- y )
273     dup [-1,1]? [ >float fasin ] [ i* asinh -i* ] if ; inline
274
275 : acos ( x -- y )
276     dup [-1,1]? [ >float facos ] [ asin pi 2 / swap - ] if ;
277     inline
278
279 GENERIC: atan ( x -- y ) foldable
280
281 M: complex atan i* atanh i* ; inline
282
283 M: float atan fatan ; inline
284
285 M: real atan >float atan ; inline
286
287 : asec ( x -- y ) recip acos ; inline
288
289 : acosec ( x -- y ) recip asin ; inline
290
291 : acot ( x -- y ) recip atan ; inline
292
293 : truncate ( x -- y ) dup 1 mod - ; inline
294
295 : round ( x -- y ) dup sgn 2 / + truncate ; inline
296
297 : floor ( x -- y )
298     dup 1 mod
299     [ ] [ dup 0 < [ - 1 - ] [ - ] if ] if-zero ; foldable
300
301 : ceiling ( x -- y ) neg floor neg ; foldable
302
303 : floor-to ( x step -- y )
304     [ [ / floor ] [ * ] bi ] unless-zero ;
305
306 : lerp ( a b t -- a_t ) [ over - ] dip * + ; inline
307