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
100 [ [ mod ] keep swap (gcd*) ] unless-zero ; inline recursive
106 { [ over zero? ] [ 0^ ] }
107 { [ dup integer? ] [ integer^ ] }
108 { [ 2dup real^? ] [ [ >float ] bi@ fpow ] }
112 : nth-root ( n x -- y ) swap recip ^ ; inline
115 [ 0 1 ] 2dip (gcd) dup 0 < [ neg ] when ; foldable
118 (gcd*) dup 0 < [ neg ] when ; foldable
121 [ * ] 2keep gcd* /i ; foldable
123 : divisor? ( m n -- ? )
126 ERROR: non-trivial-divisor n ;
128 : mod-inv ( x n -- y )
129 [ nip ] [ gcd 1 = ] 2bi
130 [ dup 0 < [ + ] [ nip ] if ]
131 [ non-trivial-divisor ] if ; foldable
133 : ^mod ( x y n -- z )
135 [ [ [ neg ] dip ^mod ] keep mod-inv ] [ (^mod) ] if ; foldable
137 GENERIC: absq ( x -- y ) foldable
139 M: real absq sq ; inline
141 : ~abs ( x y epsilon -- ? )
144 : ~rel ( x y epsilon -- ? )
145 [ [ - abs ] 2keep [ abs ] bi@ + ] dip * <= ;
147 : ~ ( x y epsilon -- ? )
149 { [ dup zero? ] [ drop number= ] }
150 { [ dup 0 < ] [ neg ~rel ] }
154 : conjugate ( z -- z* ) >rect neg rect> ; inline
156 : arg ( z -- arg ) >float-rect swap fatan2 ; inline
159 dup complex? [ drop f ] [ abs 1 <= ] if ; inline
162 dup complex? [ drop f ] [ 1 >= ] if ; inline
164 GENERIC: frexp ( x -- y exp )
167 dup fp-special? [ dup zero? ] unless* [ 0 ] [
169 [ HEX: 800f,ffff,ffff,ffff bitand 0.5 double>bits bitor bits>double ]
170 [ -52 shift HEX: 7ff bitand 1022 - ] bi
175 dup 0 > [ 1 ] [ abs -1 ] if swap dup log2 [
176 52 swap - shift HEX: 000f,ffff,ffff,ffff bitand
177 0.5 double>bits bitor bits>double
178 ] [ 1 + ] bi [ * ] dip
181 GENERIC: log ( x -- y )
183 M: float log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ; inline
185 M: real log >float log ; inline
187 M: complex log >polar [ flog ] dip rect> ; inline
191 : most-negative-finite-float ( -- x )
192 HEX: -1.ffff,ffff,ffff,fp1023 >integer ; inline
193 : most-positive-finite-float ( -- x )
194 HEX: 1.ffff,ffff,ffff,fp1023 >integer ; inline
195 CONSTANT: log-2 HEX: 1.62e42fefa39efp-1
196 CONSTANT: log10-2 HEX: 1.34413509f79ffp-2
198 : (representable-as-float?) ( x -- ? )
199 most-negative-finite-float
200 most-positive-finite-float between? ; inline
202 : (bignum-log) ( n log-quot: ( x -- y ) log-2 -- log )
204 dup (representable-as-float?)
205 [ >float @ ] [ frexp [ @ ] [ _ * ] bi* + ] if
210 M: bignum log [ log ] log-2 (bignum-log) ;
212 GENERIC: log1+ ( x -- y )
214 M: object log1+ 1 + log ; inline
216 M: float log1+ dup -1.0 >= [ flog1+ ] [ 1.0 + 0.0 rect> log ] if ; inline
218 : 10^ ( x -- y ) 10 swap ^ ; inline
220 GENERIC: log10 ( x -- y ) foldable
222 M: real log10 >float flog10 ; inline
224 M: complex log10 log 10 log / ; inline
226 M: bignum log10 [ log10 ] log10-2 (bignum-log) ;
228 GENERIC: cos ( x -- y ) foldable
232 [ [ fcos ] [ fcosh ] bi* * ]
233 [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
235 M: float cos fcos ; inline
237 M: real cos >float cos ; inline
239 : sec ( x -- y ) cos recip ; inline
241 GENERIC: cosh ( x -- y ) foldable
245 [ [ fcosh ] [ fcos ] bi* * ]
246 [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
248 M: float cosh fcosh ; inline
250 M: real cosh >float cosh ; inline
252 : sech ( x -- y ) cosh recip ; inline
254 GENERIC: sin ( x -- y ) foldable
258 [ [ fsin ] [ fcosh ] bi* * ]
259 [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
261 M: float sin fsin ; inline
263 M: real sin >float sin ; inline
265 : cosec ( x -- y ) sin recip ; inline
267 GENERIC: sinh ( x -- y ) foldable
271 [ [ fsinh ] [ fcos ] bi* * ]
272 [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
274 M: float sinh fsinh ; inline
276 M: real sinh >float sinh ; inline
278 : cosech ( x -- y ) sinh recip ; inline
280 GENERIC: tan ( x -- y ) foldable
282 M: complex tan [ sin ] [ cos ] bi / ;
284 M: float tan ftan ; inline
286 M: real tan >float tan ; inline
288 GENERIC: tanh ( x -- y ) foldable
290 M: complex tanh [ sinh ] [ cosh ] bi / ;
292 M: float tanh ftanh ; inline
294 M: real tanh >float tanh ; inline
296 : cot ( x -- y ) tan recip ; inline
298 : coth ( x -- y ) tanh recip ; inline
301 dup sq 1 - sqrt + log ; inline
303 : asech ( x -- y ) recip acosh ; inline
306 dup sq 1 + sqrt + log ; inline
308 : acosech ( x -- y ) recip asinh ; inline
311 [ 1 + ] [ 1 - neg ] bi / log 2 / ; inline
313 : acoth ( x -- y ) recip atanh ; inline
315 : i* ( x -- y ) >rect neg swap rect> ;
317 : -i* ( x -- y ) >rect swap neg rect> ;
320 dup [-1,1]? [ >float fasin ] [ i* asinh -i* ] if ; inline
323 dup [-1,1]? [ >float facos ] [ asin pi 2 / swap - ] if ;
326 GENERIC: atan ( x -- y ) foldable
328 M: complex atan i* atanh i* ; inline
330 M: float atan fatan ; inline
332 M: real atan >float atan ; inline
334 : asec ( x -- y ) recip acos ; inline
336 : acosec ( x -- y ) recip asin ; inline
338 : acot ( x -- y ) recip atan ; inline
340 : truncate ( x -- y ) dup 1 mod - ; inline
342 : round ( x -- y ) dup sgn 2 / + truncate ; inline
346 [ ] [ dup 0 < [ - 1 - ] [ - ] if ] if-zero ; foldable
348 : ceiling ( x -- y ) neg floor neg ; foldable
350 : floor-to ( x step -- y )
351 [ [ / floor ] [ * ] bi ] unless-zero ;
353 : lerp ( a b t -- a_t ) [ over - ] dip * + ; inline