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