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