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