]> 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
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 <PRIVATE
11
12 : (rect>) ( x y -- z )
13     dup 0 = [ drop ] [ <complex> ] if ; inline
14
15 PRIVATE>
16
17 : rect> ( x y -- z )
18     2dup [ real? ] both? [
19         (rect>)
20     ] [
21         "Complex number must have real components" throw
22     ] if ; inline
23
24 GENERIC: sqrt ( x -- y ) foldable
25
26 M: real sqrt
27     >float dup 0.0 < [ neg fsqrt 0.0 swap rect> ] [ fsqrt ] if ;
28
29 : each-bit ( n quot: ( ? -- ) -- )
30     over [ 0 = ] [ -1 = ] bi or [
31         2drop
32     ] [
33         2dup { [ odd? ] [ call ] [ 2/ ] [ each-bit ] } spread
34     ] if ; inline recursive
35
36 : map-bits ( n quot: ( ? -- obj ) -- seq )
37     accumulator [ each-bit ] dip ; inline
38
39 : factor-2s ( n -- r s )
40     #! factor an integer into 2^r * s
41     dup 0 = [ 1 ] [
42         0 swap [ dup even? ] [ [ 1+ ] [ 2/ ] bi* ] [ ] while
43     ] if ; inline
44
45 <PRIVATE
46
47 GENERIC# ^n 1 ( z w -- z^w )
48
49 : (^n) ( z w -- z^w )
50     1 swap [ [ dupd * ] when [ sq ] dip ] each-bit nip ; inline
51
52 M: integer ^n
53     [ factor-2s ] dip [ (^n) ] keep rot * shift ;
54
55 M: ratio ^n
56     [ >fraction ] dip tuck [ ^n ] 2bi@ / ;
57
58 M: float ^n
59     (^n) ;
60
61 : integer^ ( x y -- z )
62     dup 0 > [ ^n ] [ neg ^n recip ] if ; inline
63
64 PRIVATE>
65
66 : >rect ( z -- x y )
67     [ real-part ] [ imaginary-part ] bi ; inline
68
69 : >float-rect ( z -- x y )
70     >rect [ >float ] bi@ ; inline
71
72 : >polar ( z -- abs arg )
73     >float-rect [ [ sq ] bi@ + fsqrt ] [ swap fatan2 ] 2bi ; inline
74
75 : cis ( arg -- z ) dup fcos swap fsin rect> ; inline
76
77 : polar> ( abs arg -- z ) cis * ; inline
78
79 <PRIVATE
80
81 : ^mag ( w abs arg -- magnitude )
82     [ >float-rect swap ] [ swap fpow ] [ rot * fexp /f ] tri* ; inline
83
84 : ^theta ( w abs arg -- theta )
85     [ >float-rect ] [ flog * swap ] [ * + ] tri* ; inline
86
87 : ^complex ( x y -- z )
88     swap >polar [ ^mag ] [ ^theta ] 3bi polar> ; inline
89
90 : real^? ( x y -- ? )
91     2dup [ real? ] both? [ drop 0 >= ] [ 2drop f ] if ; inline
92
93 : 0^ ( x -- z )
94     dup zero? [ drop 0./0. ] [ 0 < 1./0. 0 ? ] if ; inline
95
96 : (^mod) ( n x y -- z )
97     1 swap [
98         [ dupd * pick mod ] when [ sq over mod ] dip
99     ] each-bit 2nip ; inline
100
101 : (gcd) ( b a x y -- a d )
102     over zero? [
103         2nip
104     ] [
105         swap [ /mod [ over * swapd - ] dip ] keep (gcd)
106     ] if ;
107
108 PRIVATE>
109
110 : ^ ( x y -- z )
111     {
112         { [ over zero? ] [ nip 0^ ] }
113         { [ dup integer? ] [ integer^ ] }
114         { [ 2dup real^? ] [ fpow ] }
115         [ ^complex ]
116     } cond ; inline
117
118 : gcd ( x y -- a d )
119     [ 0 1 ] 2dip (gcd) dup 0 < [ neg ] when ; foldable
120
121 : lcm ( a b -- c )
122     [ * ] 2keep gcd nip /i ; foldable
123
124 : mod-inv ( x n -- y )
125     tuck gcd 1 = [
126         dup 0 < [ + ] [ nip ] if
127     ] [
128         "Non-trivial divisor found" throw
129     ] if ; foldable
130
131 : ^mod ( x y n -- z )
132     over 0 < [
133         [ [ neg ] dip ^mod ] keep mod-inv
134     ] [
135         -rot (^mod)
136     ] if ; foldable
137
138 GENERIC: absq ( x -- y ) foldable
139
140 M: real absq sq ;
141
142 : ~abs ( x y epsilon -- ? )
143     [ - abs ] dip < ;
144
145 : ~rel ( x y epsilon -- ? )
146     [ [ - abs ] 2keep [ abs ] bi@ + ] dip * < ;
147
148 : ~ ( x y epsilon -- ? )
149     {
150         { [ 2over [ fp-nan? ] either? ] [ 3drop f ] }
151         { [ dup zero? ] [ drop number= ] }
152         { [ dup 0 < ] [ ~rel ] }
153         [ ~abs ]
154     } cond ;
155
156 : conjugate ( z -- z* ) >rect neg rect> ; inline
157
158 : arg ( z -- arg ) >float-rect swap fatan2 ; inline
159
160 : [-1,1]? ( x -- ? )
161     dup complex? [ drop f ] [ abs 1 <= ] if ; inline
162
163 : >=1? ( x -- ? )
164     dup complex? [ drop f ] [ 1 >= ] if ; inline
165
166 GENERIC: exp ( x -- y )
167
168 M: real exp fexp ;
169
170 M: complex exp >rect swap fexp swap polar> ;
171
172 GENERIC: log ( x -- y )
173
174 M: real log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ;
175
176 M: complex log >polar swap flog swap rect> ;
177
178 GENERIC: cos ( x -- y ) foldable
179
180 M: complex cos
181     >float-rect
182     [ [ fcos ] [ fcosh ] bi* * ]
183     [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
184
185 M: real cos fcos ;
186
187 : sec ( x -- y ) cos recip ; inline
188
189 GENERIC: cosh ( x -- y ) foldable
190
191 M: complex cosh
192     >float-rect
193     [ [ fcosh ] [ fcos ] bi* * ]
194     [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
195
196 M: real cosh fcosh ;
197
198 : sech ( x -- y ) cosh recip ; inline
199
200 GENERIC: sin ( x -- y ) foldable
201
202 M: complex sin
203     >float-rect
204     [ [ fsin ] [ fcosh ] bi* * ]
205     [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
206
207 M: real sin fsin ;
208
209 : cosec ( x -- y ) sin recip ; inline
210
211 GENERIC: sinh ( x -- y ) foldable
212
213 M: complex sinh 
214     >float-rect
215     [ [ fsinh ] [ fcos ] bi* * ]
216     [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
217
218 M: real sinh fsinh ;
219
220 : cosech ( x -- y ) sinh recip ; inline
221
222 GENERIC: tan ( x -- y ) foldable
223
224 M: complex tan [ sin ] [ cos ] bi / ;
225
226 M: real tan ftan ;
227
228 GENERIC: tanh ( x -- y ) foldable
229
230 M: complex tanh [ sinh ] [ cosh ] bi / ;
231
232 M: real tanh ftanh ;
233
234 : cot ( x -- y ) tan recip ; inline
235
236 : coth ( x -- y ) tanh recip ; inline
237
238 : acosh ( x -- y )
239     dup sq 1- sqrt + log ; inline
240
241 : asech ( x -- y ) recip acosh ; inline
242
243 : asinh ( x -- y )
244     dup sq 1+ sqrt + log ; inline
245
246 : acosech ( x -- y ) recip asinh ; inline
247
248 : atanh ( x -- y )
249     [ 1+ ] [ 1- neg ] bi / log 2 / ; inline
250
251 : acoth ( x -- y ) recip atanh ; inline
252
253 : i* ( x -- y ) >rect neg swap rect> ;
254
255 : -i* ( x -- y ) >rect swap neg rect> ;
256
257 : asin ( x -- y )
258     dup [-1,1]? [ fasin ] [ i* asinh -i* ] if ; inline
259
260 : acos ( x -- y )
261     dup [-1,1]? [ facos ] [ asin pi 2 / swap - ] if ;
262     inline
263
264 GENERIC: atan ( x -- y ) foldable
265
266 M: complex atan i* atanh i* ;
267
268 M: real atan fatan ;
269
270 : asec ( x -- y ) recip acos ; inline
271
272 : acosec ( x -- y ) recip asin ; inline
273
274 : acot ( x -- y ) recip atan ; inline
275
276 : truncate ( x -- y ) dup 1 mod - ; inline
277
278 : round ( x -- y ) dup sgn 2 / + truncate ; inline
279
280 : floor ( x -- y )
281     dup 1 mod dup zero?
282     [ drop ] [ dup 0 < [ - 1- ] [ - ] if ] if ; foldable
283
284 : ceiling ( x -- y ) neg floor neg ; foldable