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 ;
7 : >fraction ( a/b -- a b )
8 [ numerator ] [ denominator ] bi ; inline
11 dup 0 = [ drop ] [ complex boa ] if ; inline
13 GENERIC: sqrt ( x -- y ) foldable
16 >float dup 0.0 < [ neg fsqrt 0.0 swap rect> ] [ fsqrt ] if ; inline
18 : factor-2s ( n -- r s )
19 #! factor an integer into 2^r * s
21 0 swap [ dup even? ] [ [ 1 + ] [ 2/ ] bi* ] while
26 GENERIC# ^n 1 ( z w -- z^w ) foldable
29 make-bits 1 [ [ dupd * ] when [ sq ] dip ] reduce nip ; inline
32 [ factor-2s ] dip [ (^n) ] keep rot * shift ;
35 [ >fraction ] dip [ ^n ] curry bi@ / ;
41 : integer^ ( x y -- z )
42 dup 0 > [ ^n ] [ neg ^n recip ] if ; inline
47 [ real-part ] [ imaginary-part ] bi ; inline
49 : >float-rect ( z -- x y )
50 >rect [ >float ] bi@ ; inline
52 : >polar ( z -- abs arg )
53 >float-rect [ [ sq ] bi@ + fsqrt ] [ swap fatan2 ] 2bi ; inline
55 : cis ( arg -- z ) >float [ fcos ] [ fsin ] bi rect> ; inline
57 : polar> ( abs arg -- z ) cis * ; inline
59 GENERIC: exp ( x -- y )
61 M: float exp fexp ; inline
63 M: real exp >float exp ; inline
65 M: complex exp >rect swap fexp swap polar> ; inline
69 : ^mag ( w abs arg -- magnitude )
71 [ >float swap >float fpow ]
75 : ^theta ( w abs arg -- theta )
76 [ >float-rect ] [ flog * swap ] [ * + ] tri* ; inline
78 : ^complex ( x y -- z )
79 swap >polar [ ^mag ] [ ^theta ] 3bi polar> ; inline
82 2dup [ real? ] both? [ drop 0 >= ] [ 2drop f ] if ; inline
85 [ 0/0. ] [ 0 < 1/0. 0 ? ] if-zero ; inline
87 : (^mod) ( n x y -- z )
89 [ dupd * pick mod ] when [ sq over mod ] dip
90 ] reduce 2nip ; inline
92 : (gcd) ( b a x y -- a d )
96 swap [ /mod [ over * swapd - ] dip ] keep (gcd)
103 { [ over 0 = ] [ nip 0^ ] }
104 { [ dup integer? ] [ integer^ ] }
105 { [ 2dup real^? ] [ [ >float ] bi@ fpow ] }
109 : nth-root ( n x -- y ) swap recip ^ ; inline
112 [ 0 1 ] 2dip (gcd) dup 0 < [ neg ] when ; foldable
115 [ * ] 2keep gcd nip /i ; foldable
117 : divisor? ( m n -- ? )
120 ERROR: non-trivial-divisor n ;
122 : mod-inv ( x n -- y )
123 [ nip ] [ gcd 1 = ] 2bi
124 [ dup 0 < [ + ] [ nip ] if ]
125 [ non-trivial-divisor ] if ; foldable
127 : ^mod ( x y n -- z )
129 [ [ neg ] dip ^mod ] keep mod-inv
134 GENERIC: absq ( x -- y ) foldable
136 M: real absq sq ; inline
138 : ~abs ( x y epsilon -- ? )
141 : ~rel ( x y epsilon -- ? )
142 [ [ - abs ] 2keep [ abs ] bi@ + ] dip * <= ;
144 : ~ ( x y epsilon -- ? )
146 { [ dup zero? ] [ drop number= ] }
147 { [ dup 0 < ] [ neg ~rel ] }
151 : conjugate ( z -- z* ) >rect neg rect> ; inline
153 : arg ( z -- arg ) >float-rect swap fatan2 ; inline
156 dup complex? [ drop f ] [ abs 1 <= ] if ; inline
159 dup complex? [ drop f ] [ 1 >= ] if ; inline
161 GENERIC: log ( x -- y )
163 M: float log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ; inline
165 M: real log >float log ; inline
167 M: complex log >polar [ flog ] dip rect> ; inline
169 GENERIC: log1+ ( x -- y )
171 M: object log1+ 1 + log ; inline
173 M: float log1+ dup -1.0 >= [ flog1+ ] [ 1.0 + 0.0 rect> log ] if ; inline
175 : 10^ ( x -- y ) 10 swap ^ ; inline
177 GENERIC: log10 ( x -- y ) foldable
179 M: real log10 >float flog10 ; inline
181 M: complex log10 log 10 log / ; inline
183 GENERIC: cos ( x -- y ) foldable
187 [ [ fcos ] [ fcosh ] bi* * ]
188 [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
190 M: float cos fcos ; inline
192 M: real cos >float cos ; inline
194 : sec ( x -- y ) cos recip ; inline
196 GENERIC: cosh ( x -- y ) foldable
200 [ [ fcosh ] [ fcos ] bi* * ]
201 [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
203 M: float cosh fcosh ; inline
205 M: real cosh >float cosh ; inline
207 : sech ( x -- y ) cosh recip ; inline
209 GENERIC: sin ( x -- y ) foldable
213 [ [ fsin ] [ fcosh ] bi* * ]
214 [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
216 M: float sin fsin ; inline
218 M: real sin >float sin ; inline
220 : cosec ( x -- y ) sin recip ; inline
222 GENERIC: sinh ( x -- y ) foldable
226 [ [ fsinh ] [ fcos ] bi* * ]
227 [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
229 M: float sinh fsinh ; inline
231 M: real sinh >float sinh ; inline
233 : cosech ( x -- y ) sinh recip ; inline
235 GENERIC: tan ( x -- y ) foldable
237 M: complex tan [ sin ] [ cos ] bi / ;
239 M: float tan ftan ; inline
241 M: real tan >float tan ; inline
243 GENERIC: tanh ( x -- y ) foldable
245 M: complex tanh [ sinh ] [ cosh ] bi / ;
247 M: float tanh ftanh ; inline
249 M: real tanh >float tanh ; inline
251 : cot ( x -- y ) tan recip ; inline
253 : coth ( x -- y ) tanh recip ; inline
256 dup sq 1 - sqrt + log ; inline
258 : asech ( x -- y ) recip acosh ; inline
261 dup sq 1 + sqrt + log ; inline
263 : acosech ( x -- y ) recip asinh ; inline
266 [ 1 + ] [ 1 - neg ] bi / log 2 / ; inline
268 : acoth ( x -- y ) recip atanh ; inline
270 : i* ( x -- y ) >rect neg swap rect> ;
272 : -i* ( x -- y ) >rect swap neg rect> ;
275 dup [-1,1]? [ >float fasin ] [ i* asinh -i* ] if ; inline
278 dup [-1,1]? [ >float facos ] [ asin pi 2 / swap - ] if ;
281 GENERIC: atan ( x -- y ) foldable
283 M: complex atan i* atanh i* ; inline
285 M: float atan fatan ; inline
287 M: real atan >float atan ; inline
289 : asec ( x -- y ) recip acos ; inline
291 : acosec ( x -- y ) recip asin ; inline
293 : acot ( x -- y ) recip atan ; inline
295 : truncate ( x -- y ) dup 1 mod - ; inline
297 : round ( x -- y ) dup sgn 2 / + truncate ; inline
301 [ ] [ dup 0 < [ - 1 - ] [ - ] if ] if-zero ; foldable
303 : ceiling ( x -- y ) neg floor neg ; foldable
305 : floor-to ( x step -- y )
306 [ [ / floor ] [ * ] bi ] unless-zero ;
308 : lerp ( a b t -- a_t ) [ over - ] dip * + ; inline