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