]> 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, 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 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 ] [ [ recip ] dip neg ^n ] 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 exp 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 : nth-root ( n x -- y ) swap recip ^ ; inline
110
111 : gcd ( x y -- a d )
112     [ 0 1 ] 2dip (gcd) dup 0 < [ neg ] when ; foldable
113
114 : lcm ( a b -- c )
115     [ * ] 2keep gcd nip /i ; foldable
116
117 : divisor? ( m n -- ? )
118     mod 0 = ;
119
120 ERROR: non-trivial-divisor n ;
121
122 : mod-inv ( x n -- y )
123     [ nip ] [ gcd 1 = ] 2bi
124     [ dup 0 < [ + ] [ nip ] if ]
125     [ non-trivial-divisor ] if ; foldable
126
127 : ^mod ( x y n -- z )
128     over 0 < [
129         [ [ neg ] dip ^mod ] keep mod-inv
130     ] [
131         -rot (^mod)
132     ] if ; foldable
133
134 GENERIC: absq ( x -- y ) foldable
135
136 M: real absq sq ; inline
137
138 : ~abs ( x y epsilon -- ? )
139     [ - abs ] dip < ;
140
141 : ~rel ( x y epsilon -- ? )
142     [ [ - abs ] 2keep [ abs ] bi@ + ] dip * <= ;
143
144 : ~ ( x y epsilon -- ? )
145     {
146         { [ dup zero? ] [ drop number= ] }
147         { [ dup 0 < ] [ neg ~rel ] }
148         [ ~abs ]
149     } cond ;
150
151 : conjugate ( z -- z* ) >rect neg rect> ; inline
152
153 : arg ( z -- arg ) >float-rect swap fatan2 ; inline
154
155 : [-1,1]? ( x -- ? )
156     dup complex? [ drop f ] [ abs 1 <= ] if ; inline
157
158 : >=1? ( x -- ? )
159     dup complex? [ drop f ] [ 1 >= ] if ; inline
160
161 GENERIC: log ( x -- y )
162
163 M: float log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ; inline
164
165 M: real log >float log ; inline
166
167 M: complex log >polar [ flog ] dip rect> ; inline
168
169 GENERIC: log1+ ( x -- y )
170
171 M: object log1+ 1 + log ; inline
172
173 M: float log1+ dup -1.0 >= [ flog1+ ] [ 1.0 + 0.0 rect> log ] if ; inline
174
175 : 10^ ( x -- y ) 10 swap ^ ; inline
176
177 GENERIC: log10 ( x -- y ) foldable
178
179 M: real log10 >float flog10 ; inline
180
181 M: complex log10 log 10 log / ; inline
182
183 GENERIC: cos ( x -- y ) foldable
184
185 M: complex cos
186     >float-rect
187     [ [ fcos ] [ fcosh ] bi* * ]
188     [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
189
190 M: float cos fcos ; inline
191
192 M: real cos >float cos ; inline
193
194 : sec ( x -- y ) cos recip ; inline
195
196 GENERIC: cosh ( x -- y ) foldable
197
198 M: complex cosh
199     >float-rect
200     [ [ fcosh ] [ fcos ] bi* * ]
201     [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
202
203 M: float cosh fcosh ; inline
204
205 M: real cosh >float cosh ; inline
206
207 : sech ( x -- y ) cosh recip ; inline
208
209 GENERIC: sin ( x -- y ) foldable
210
211 M: complex sin
212     >float-rect
213     [ [ fsin ] [ fcosh ] bi* * ]
214     [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
215
216 M: float sin fsin ; inline
217
218 M: real sin >float sin ; inline
219
220 : cosec ( x -- y ) sin recip ; inline
221
222 GENERIC: sinh ( x -- y ) foldable
223
224 M: complex sinh
225     >float-rect
226     [ [ fsinh ] [ fcos ] bi* * ]
227     [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
228
229 M: float sinh fsinh ; inline
230
231 M: real sinh >float sinh ; inline
232
233 : cosech ( x -- y ) sinh recip ; inline
234
235 GENERIC: tan ( x -- y ) foldable
236
237 M: complex tan [ sin ] [ cos ] bi / ;
238
239 M: float tan ftan ; inline
240
241 M: real tan >float tan ; inline
242
243 GENERIC: tanh ( x -- y ) foldable
244
245 M: complex tanh [ sinh ] [ cosh ] bi / ;
246
247 M: float tanh ftanh ; inline
248
249 M: real tanh >float tanh ; inline
250
251 : cot ( x -- y ) tan recip ; inline
252
253 : coth ( x -- y ) tanh recip ; inline
254
255 : acosh ( x -- y )
256     dup sq 1 - sqrt + log ; inline
257
258 : asech ( x -- y ) recip acosh ; inline
259
260 : asinh ( x -- y )
261     dup sq 1 + sqrt + log ; inline
262
263 : acosech ( x -- y ) recip asinh ; inline
264
265 : atanh ( x -- y )
266     [ 1 + ] [ 1 - neg ] bi / log 2 / ; inline
267
268 : acoth ( x -- y ) recip atanh ; inline
269
270 : i* ( x -- y ) >rect neg swap rect> ;
271
272 : -i* ( x -- y ) >rect swap neg rect> ;
273
274 : asin ( x -- y )
275     dup [-1,1]? [ >float fasin ] [ i* asinh -i* ] if ; inline
276
277 : acos ( x -- y )
278     dup [-1,1]? [ >float facos ] [ asin pi 2 / swap - ] if ;
279     inline
280
281 GENERIC: atan ( x -- y ) foldable
282
283 M: complex atan i* atanh i* ; inline
284
285 M: float atan fatan ; inline
286
287 M: real atan >float atan ; inline
288
289 : asec ( x -- y ) recip acos ; inline
290
291 : acosec ( x -- y ) recip asin ; inline
292
293 : acot ( x -- y ) recip atan ; inline
294
295 : truncate ( x -- y ) dup 1 mod - ; inline
296
297 : round ( x -- y ) dup sgn 2 / + truncate ; inline
298
299 : floor ( x -- y )
300     dup 1 mod
301     [ ] [ dup 0 < [ - 1 - ] [ - ] if ] if-zero ; foldable
302
303 : ceiling ( x -- y ) neg floor neg ; foldable
304
305 : floor-to ( x step -- y )
306     [ [ / floor ] [ * ] bi ] unless-zero ;
307
308 : lerp ( a b t -- a_t ) [ over - ] dip * + ; inline