]> gitweb.factorcode.org Git - factor.git/blob - basis/math/functions/functions.factor
Merge remote-tracking branch 'ajvondrak/graphviz'
[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 fry 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 <
17     [ neg fsqrt [ 0.0 ] dip rect> ] [ fsqrt ] if ; inline
18
19 : factor-2s ( n -- r s )
20     #! factor an integer into 2^r * s
21     dup 0 = [ 1 ] [
22         [ 0 ] dip [ dup even? ] [ [ 1 + ] [ 2/ ] bi* ] while
23     ] if ; inline
24
25 <PRIVATE
26
27 GENERIC# ^n 1 ( z w -- z^w ) foldable
28
29 : (^n) ( z w -- z^w )
30     make-bits 1 [ [ over * ] when [ sq ] dip ] reduce nip ; inline
31
32 M: integer ^n
33     [ factor-2s ] dip [ (^n) ] keep rot * shift ;
34
35 M: ratio ^n
36     [ >fraction ] dip '[ _ ^n ] bi@ / ;
37
38 M: float ^n (^n) ;
39
40 M: complex ^n (^n) ;
41
42 : integer^ ( x y -- z )
43     dup 0 >= [ ^n ] [ [ recip ] dip neg ^n ] if ; inline
44
45 PRIVATE>
46
47 : >rect ( z -- x y )
48     [ real-part ] [ imaginary-part ] bi ; inline
49
50 : >float-rect ( z -- x y )
51     >rect [ >float ] bi@ ; inline
52
53 : >polar ( z -- abs arg )
54     >float-rect [ [ sq ] bi@ + fsqrt ] [ swap fatan2 ] 2bi ; inline
55
56 : cis ( arg -- z ) >float [ fcos ] [ fsin ] bi rect> ; inline
57
58 : polar> ( abs arg -- z ) cis * ; inline
59
60 GENERIC: exp ( x -- y )
61
62 M: float exp fexp ; inline
63
64 M: real exp >float exp ; inline
65
66 M: complex exp >rect [ exp ] dip polar> ; inline
67
68 <PRIVATE
69
70 : ^mag ( w abs arg -- magnitude )
71     [ >float-rect swap ]
72     [ >float swap >float fpow ]
73     [ rot * exp /f ]
74     tri* ; inline
75
76 : ^theta ( w abs arg -- theta )
77     [ >float-rect ] [ flog * swap ] [ * + ] tri* ; inline
78
79 : ^complex ( x y -- z )
80     swap >polar [ ^mag ] [ ^theta ] 3bi polar> ; inline
81
82 : real^? ( x y -- ? )
83     2dup [ real? ] both? [ drop 0 >= ] [ 2drop f ] if ; inline
84
85 : 0^ ( zero x -- z )
86     swap [ 0/0. ] swap '[ 0 < 1/0. _ ? ] if-zero ; inline
87
88 : (^mod) ( x y n -- z )
89     [ make-bits 1 ] dip dup
90     '[ [ over * _ mod ] when [ sq _ mod ] dip ] reduce nip ; 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 zero? ] [ 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 ] [ (^mod) ] if ; foldable
130
131 GENERIC: absq ( x -- y ) foldable
132
133 M: real absq sq ; inline
134
135 : ~abs ( x y epsilon -- ? )
136     [ - abs ] dip < ;
137
138 : ~rel ( x y epsilon -- ? )
139     [ [ - abs ] 2keep [ abs ] bi@ + ] dip * <= ;
140
141 : ~ ( x y epsilon -- ? )
142     {
143         { [ dup zero? ] [ drop number= ] }
144         { [ dup 0 < ] [ neg ~rel ] }
145         [ ~abs ]
146     } cond ;
147
148 : conjugate ( z -- z* ) >rect neg rect> ; inline
149
150 : arg ( z -- arg ) >float-rect swap fatan2 ; inline
151
152 : [-1,1]? ( x -- ? )
153     dup complex? [ drop f ] [ abs 1 <= ] if ; inline
154
155 : >=1? ( x -- ? )
156     dup complex? [ drop f ] [ 1 >= ] if ; inline
157
158 GENERIC: log ( x -- y )
159
160 M: float log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ; inline
161
162 M: real log >float log ; inline
163
164 M: complex log >polar [ flog ] dip rect> ; inline
165
166 GENERIC: log1+ ( x -- y )
167
168 M: object log1+ 1 + log ; inline
169
170 M: float log1+ dup -1.0 >= [ flog1+ ] [ 1.0 + 0.0 rect> log ] if ; inline
171
172 : 10^ ( x -- y ) 10 swap ^ ; inline
173
174 GENERIC: log10 ( x -- y ) foldable
175
176 M: real log10 >float flog10 ; inline
177
178 M: complex log10 log 10 log / ; inline
179
180 GENERIC: cos ( x -- y ) foldable
181
182 M: complex cos
183     >float-rect
184     [ [ fcos ] [ fcosh ] bi* * ]
185     [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
186
187 M: float cos fcos ; inline
188
189 M: real cos >float cos ; inline
190
191 : sec ( x -- y ) cos recip ; inline
192
193 GENERIC: cosh ( x -- y ) foldable
194
195 M: complex cosh
196     >float-rect
197     [ [ fcosh ] [ fcos ] bi* * ]
198     [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
199
200 M: float cosh fcosh ; inline
201
202 M: real cosh >float cosh ; inline
203
204 : sech ( x -- y ) cosh recip ; inline
205
206 GENERIC: sin ( x -- y ) foldable
207
208 M: complex sin
209     >float-rect
210     [ [ fsin ] [ fcosh ] bi* * ]
211     [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
212
213 M: float sin fsin ; inline
214
215 M: real sin >float sin ; inline
216
217 : cosec ( x -- y ) sin recip ; inline
218
219 GENERIC: sinh ( x -- y ) foldable
220
221 M: complex sinh
222     >float-rect
223     [ [ fsinh ] [ fcos ] bi* * ]
224     [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
225
226 M: float sinh fsinh ; inline
227
228 M: real sinh >float sinh ; inline
229
230 : cosech ( x -- y ) sinh recip ; inline
231
232 GENERIC: tan ( x -- y ) foldable
233
234 M: complex tan [ sin ] [ cos ] bi / ;
235
236 M: float tan ftan ; inline
237
238 M: real tan >float tan ; inline
239
240 GENERIC: tanh ( x -- y ) foldable
241
242 M: complex tanh [ sinh ] [ cosh ] bi / ;
243
244 M: float tanh ftanh ; inline
245
246 M: real tanh >float tanh ; inline
247
248 : cot ( x -- y ) tan recip ; inline
249
250 : coth ( x -- y ) tanh recip ; inline
251
252 : acosh ( x -- y )
253     dup sq 1 - sqrt + log ; inline
254
255 : asech ( x -- y ) recip acosh ; inline
256
257 : asinh ( x -- y )
258     dup sq 1 + sqrt + log ; inline
259
260 : acosech ( x -- y ) recip asinh ; inline
261
262 : atanh ( x -- y )
263     [ 1 + ] [ 1 - neg ] bi / log 2 / ; inline
264
265 : acoth ( x -- y ) recip atanh ; inline
266
267 : i* ( x -- y ) >rect neg swap rect> ;
268
269 : -i* ( x -- y ) >rect swap neg rect> ;
270
271 : asin ( x -- y )
272     dup [-1,1]? [ >float fasin ] [ i* asinh -i* ] if ; inline
273
274 : acos ( x -- y )
275     dup [-1,1]? [ >float facos ] [ asin pi 2 / swap - ] if ;
276     inline
277
278 GENERIC: atan ( x -- y ) foldable
279
280 M: complex atan i* atanh i* ; inline
281
282 M: float atan fatan ; inline
283
284 M: real atan >float atan ; inline
285
286 : asec ( x -- y ) recip acos ; inline
287
288 : acosec ( x -- y ) recip asin ; inline
289
290 : acot ( x -- y ) recip atan ; inline
291
292 : truncate ( x -- y ) dup 1 mod - ; inline
293
294 : round ( x -- y ) dup sgn 2 / + truncate ; inline
295
296 : floor ( x -- y )
297     dup 1 mod
298     [ ] [ dup 0 < [ - 1 - ] [ - ] if ] if-zero ; foldable
299
300 : ceiling ( x -- y ) neg floor neg ; foldable
301
302 : floor-to ( x step -- y )
303     [ [ / floor ] [ * ] bi ] unless-zero ;
304
305 : lerp ( a b t -- a_t ) [ over - ] dip * + ; inline