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 ;
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
17 [ neg fsqrt [ 0.0 ] dip rect> ] [ fsqrt ] if ; inline
19 : factor-2s ( n -- r s )
20 #! factor an integer into 2^r * s
22 [ 0 ] dip [ dup even? ] [ [ 1 + ] [ 2/ ] bi* ] while
27 GENERIC# ^n 1 ( z w -- z^w ) foldable
30 make-bits 1 [ [ over * ] when [ sq ] dip ] reduce nip ; inline
33 [ factor-2s ] dip [ (^n) ] keep rot * shift ;
36 [ >fraction ] dip '[ _ ^n ] bi@ / ;
42 : integer^ ( x y -- z )
43 dup 0 >= [ ^n ] [ [ recip ] dip neg ^n ] if ; inline
48 [ real-part ] [ imaginary-part ] bi ; inline
50 : >float-rect ( z -- x y )
51 >rect [ >float ] bi@ ; inline
53 : >polar ( z -- abs arg )
54 >float-rect [ [ sq ] bi@ + fsqrt ] [ swap fatan2 ] 2bi ; inline
56 : cis ( arg -- z ) >float [ fcos ] [ fsin ] bi rect> ; inline
58 : polar> ( abs arg -- z ) cis * ; inline
60 GENERIC: exp ( x -- y )
62 M: float exp fexp ; inline
64 M: real exp >float exp ; inline
66 M: complex exp >rect [ exp ] dip polar> ; inline
70 : ^mag ( w abs arg -- magnitude )
72 [ >float swap >float fpow ]
76 : ^theta ( w abs arg -- theta )
77 [ >float-rect ] [ flog * swap ] [ * + ] tri* ; inline
79 : ^complex ( x y -- z )
80 swap >polar [ ^mag ] [ ^theta ] 3bi polar> ; inline
83 2dup [ real? ] both? [ drop 0 >= ] [ 2drop f ] if ; inline
86 swap [ 0/0. ] swap '[ 0 < 1/0. _ ? ] if-zero ; inline
88 : (^mod) ( x y n -- z )
89 [ make-bits 1 ] dip dup
90 '[ [ over * _ mod ] when [ sq _ mod ] dip ] reduce nip ; inline
92 : (gcd) ( b a x y -- a d )
96 swap [ /mod [ over * swapd - ] dip ] keep (gcd)
97 ] if ; inline recursive
103 { [ over zero? ] [ 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 ; inline
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 ] [ (^mod) ] if ; foldable
131 GENERIC: absq ( x -- y ) foldable
133 M: real absq sq ; inline
135 : ~abs ( x y epsilon -- ? )
138 : ~rel ( x y epsilon -- ? )
139 [ [ - abs ] 2keep [ abs ] bi@ + ] dip * <= ;
141 : ~ ( x y epsilon -- ? )
143 { [ dup zero? ] [ drop number= ] }
144 { [ dup 0 < ] [ neg ~rel ] }
148 : conjugate ( z -- z* ) >rect neg rect> ; inline
150 : arg ( z -- arg ) >float-rect swap fatan2 ; inline
153 dup complex? [ drop f ] [ abs 1 <= ] if ; inline
156 dup complex? [ drop f ] [ 1 >= ] if ; inline
158 GENERIC: frexp ( x -- y exp )
161 dup fp-special? [ dup zero? ] unless* [ 0 ] [
163 [ 0x800f,ffff,ffff,ffff bitand 0.5 double>bits bitor bits>double ]
164 [ -52 shift 0x7ff bitand 1022 - ] bi
169 dup 0 > [ 1 ] [ abs -1 ] if swap dup log2 [
170 52 swap - shift 0x000f,ffff,ffff,ffff bitand
171 0.5 double>bits bitor bits>double
172 ] [ 1 + ] bi [ * ] dip
175 GENERIC: log ( x -- y )
177 M: float log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ; inline
179 M: real log >float log ; inline
181 M: complex log >polar [ flog ] dip rect> ; inline
185 : most-negative-finite-float ( -- x )
186 -0x1.ffff,ffff,ffff,fp1023 >integer ; inline
187 : most-positive-finite-float ( -- x )
188 0x1.ffff,ffff,ffff,fp1023 >integer ; inline
189 CONSTANT: log-2 0x1.62e42fefa39efp-1
190 CONSTANT: log10-2 0x1.34413509f79ffp-2
192 : (representable-as-float?) ( x -- ? )
193 most-negative-finite-float
194 most-positive-finite-float between? ; inline
196 : (bignum-log) ( n log-quot: ( x -- y ) log-2 -- log )
198 dup (representable-as-float?)
199 [ >float @ ] [ frexp [ @ ] [ _ * ] bi* + ] if
204 M: bignum log [ log ] log-2 (bignum-log) ;
206 GENERIC: log1+ ( x -- y )
208 M: object log1+ 1 + log ; inline
210 M: float log1+ dup -1.0 >= [ flog1+ ] [ 1.0 + 0.0 rect> log ] if ; inline
212 : 10^ ( x -- y ) 10 swap ^ ; inline
214 GENERIC: log10 ( x -- y ) foldable
216 M: real log10 >float flog10 ; inline
218 M: complex log10 log 10 log / ; inline
220 M: bignum log10 [ log10 ] log10-2 (bignum-log) ;
222 GENERIC: cos ( x -- y ) foldable
226 [ [ fcos ] [ fcosh ] bi* * ]
227 [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
229 M: float cos fcos ; inline
231 M: real cos >float cos ; inline
233 : sec ( x -- y ) cos recip ; inline
235 GENERIC: cosh ( x -- y ) foldable
239 [ [ fcosh ] [ fcos ] bi* * ]
240 [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
242 M: float cosh fcosh ; inline
244 M: real cosh >float cosh ; inline
246 : sech ( x -- y ) cosh recip ; inline
248 GENERIC: sin ( x -- y ) foldable
252 [ [ fsin ] [ fcosh ] bi* * ]
253 [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
255 M: float sin fsin ; inline
257 M: real sin >float sin ; inline
259 : cosec ( x -- y ) sin recip ; inline
261 GENERIC: sinh ( x -- y ) foldable
265 [ [ fsinh ] [ fcos ] bi* * ]
266 [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
268 M: float sinh fsinh ; inline
270 M: real sinh >float sinh ; inline
272 : cosech ( x -- y ) sinh recip ; inline
274 GENERIC: tan ( x -- y ) foldable
276 M: complex tan [ sin ] [ cos ] bi / ;
278 M: float tan ftan ; inline
280 M: real tan >float tan ; inline
282 GENERIC: tanh ( x -- y ) foldable
284 M: complex tanh [ sinh ] [ cosh ] bi / ;
286 M: float tanh ftanh ; inline
288 M: real tanh >float tanh ; inline
290 : cot ( x -- y ) tan recip ; inline
292 : coth ( x -- y ) tanh recip ; inline
295 dup sq 1 - sqrt + log ; inline
297 : asech ( x -- y ) recip acosh ; inline
300 dup sq 1 + sqrt + log ; inline
302 : acosech ( x -- y ) recip asinh ; inline
305 [ 1 + ] [ 1 - neg ] bi / log 2 / ; inline
307 : acoth ( x -- y ) recip atanh ; inline
309 : i* ( x -- y ) >rect neg swap rect> ;
311 : -i* ( x -- y ) >rect swap neg rect> ;
314 dup [-1,1]? [ >float fasin ] [ i* asinh -i* ] if ; inline
317 dup [-1,1]? [ >float facos ] [ asin pi 2 / swap - ] if ;
320 GENERIC: atan ( x -- y ) foldable
322 M: complex atan i* atanh i* ; inline
324 M: float atan fatan ; inline
326 M: real atan >float atan ; inline
328 : asec ( x -- y ) recip acos ; inline
330 : acosec ( x -- y ) recip asin ; inline
332 : acot ( x -- y ) recip atan ; inline
334 : truncate ( x -- y ) dup 1 mod - ; inline
336 : round ( x -- y ) dup sgn 2 / + truncate ; inline
340 [ ] [ dup 0 < [ - 1 - ] [ - ] if ] if-zero ; foldable
342 : ceiling ( x -- y ) neg floor neg ; foldable
344 : floor-to ( x step -- y )
345 [ [ / floor ] [ * ] bi ] unless-zero ;
347 : lerp ( a b t -- a_t ) [ over - ] dip * + ; inline