]> gitweb.factorcode.org Git - factor.git/blob - basis/math/functions/functions.factor
Merge branch 'master' into new_ui
[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 [ ^n ] curry bi@ / ;
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     [ nip ] [ gcd 1 = ] 2bi
126     [ dup 0 < [ + ] [ nip ] if ]
127     [ "Non-trivial divisor found" throw ] if ; foldable
128
129 : ^mod ( x y n -- z )
130     over 0 < [
131         [ [ neg ] dip ^mod ] keep mod-inv
132     ] [
133         -rot (^mod)
134     ] if ; foldable
135
136 GENERIC: absq ( x -- y ) foldable
137
138 M: real absq sq ;
139
140 : ~abs ( x y epsilon -- ? )
141     [ - abs ] dip < ;
142
143 : ~rel ( x y epsilon -- ? )
144     [ [ - abs ] 2keep [ abs ] bi@ + ] dip * < ;
145
146 : ~ ( x y epsilon -- ? )
147     {
148         { [ 2over [ fp-nan? ] either? ] [ 3drop f ] }
149         { [ dup zero? ] [ drop number= ] }
150         { [ dup 0 < ] [ ~rel ] }
151         [ ~abs ]
152     } cond ;
153
154 : conjugate ( z -- z* ) >rect neg rect> ; inline
155
156 : arg ( z -- arg ) >float-rect swap fatan2 ; inline
157
158 : [-1,1]? ( x -- ? )
159     dup complex? [ drop f ] [ abs 1 <= ] if ; inline
160
161 : >=1? ( x -- ? )
162     dup complex? [ drop f ] [ 1 >= ] if ; inline
163
164 GENERIC: exp ( x -- y )
165
166 M: real exp fexp ;
167
168 M: complex exp >rect swap fexp swap polar> ;
169
170 GENERIC: log ( x -- y )
171
172 M: real log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ;
173
174 M: complex log >polar swap flog swap rect> ;
175
176 GENERIC: cos ( x -- y ) foldable
177
178 M: complex cos
179     >float-rect
180     [ [ fcos ] [ fcosh ] bi* * ]
181     [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
182
183 M: real cos fcos ;
184
185 : sec ( x -- y ) cos recip ; inline
186
187 GENERIC: cosh ( x -- y ) foldable
188
189 M: complex cosh
190     >float-rect
191     [ [ fcosh ] [ fcos ] bi* * ]
192     [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
193
194 M: real cosh fcosh ;
195
196 : sech ( x -- y ) cosh recip ; inline
197
198 GENERIC: sin ( x -- y ) foldable
199
200 M: complex sin
201     >float-rect
202     [ [ fsin ] [ fcosh ] bi* * ]
203     [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
204
205 M: real sin fsin ;
206
207 : cosec ( x -- y ) sin recip ; inline
208
209 GENERIC: sinh ( x -- y ) foldable
210
211 M: complex sinh 
212     >float-rect
213     [ [ fsinh ] [ fcos ] bi* * ]
214     [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
215
216 M: real sinh fsinh ;
217
218 : cosech ( x -- y ) sinh recip ; inline
219
220 GENERIC: tan ( x -- y ) foldable
221
222 M: complex tan [ sin ] [ cos ] bi / ;
223
224 M: real tan ftan ;
225
226 GENERIC: tanh ( x -- y ) foldable
227
228 M: complex tanh [ sinh ] [ cosh ] bi / ;
229
230 M: real tanh ftanh ;
231
232 : cot ( x -- y ) tan recip ; inline
233
234 : coth ( x -- y ) tanh recip ; inline
235
236 : acosh ( x -- y )
237     dup sq 1- sqrt + log ; inline
238
239 : asech ( x -- y ) recip acosh ; inline
240
241 : asinh ( x -- y )
242     dup sq 1+ sqrt + log ; inline
243
244 : acosech ( x -- y ) recip asinh ; inline
245
246 : atanh ( x -- y )
247     [ 1+ ] [ 1- neg ] bi / log 2 / ; inline
248
249 : acoth ( x -- y ) recip atanh ; inline
250
251 : i* ( x -- y ) >rect neg swap rect> ;
252
253 : -i* ( x -- y ) >rect swap neg rect> ;
254
255 : asin ( x -- y )
256     dup [-1,1]? [ fasin ] [ i* asinh -i* ] if ; inline
257
258 : acos ( x -- y )
259     dup [-1,1]? [ facos ] [ asin pi 2 / swap - ] if ;
260     inline
261
262 GENERIC: atan ( x -- y ) foldable
263
264 M: complex atan i* atanh i* ;
265
266 M: real atan fatan ;
267
268 : asec ( x -- y ) recip acos ; inline
269
270 : acosec ( x -- y ) recip asin ; inline
271
272 : acot ( x -- y ) recip atan ; inline
273
274 : truncate ( x -- y ) dup 1 mod - ; inline
275
276 : round ( x -- y ) dup sgn 2 / + truncate ; inline
277
278 : floor ( x -- y )
279     dup 1 mod dup zero?
280     [ drop ] [ dup 0 < [ - 1- ] [ - ] if ] if ; foldable
281
282 : ceiling ( x -- y ) neg floor neg ; foldable