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 GENERIC: sqrt ( x -- y ) foldable
11 [ neg fsqrt [ 0.0 ] dip rect> ] [ fsqrt ] if ; inline
13 : factor-2s ( n -- r s )
14 #! factor an integer into 2^r * s
16 [ 0 ] dip [ dup even? ] [ [ 1 + ] [ 2/ ] bi* ] while
21 : (^fixnum) ( z w -- z^w )
25 [ [ * ] keep ] [ 1 - ] bi*
26 ] when [ sq ] [ 2/ ] bi*
27 ] until 2drop ; inline
29 : (^bignum) ( z w -- z^w )
30 make-bits 1 [ [ over * ] when [ sq ] dip ] reduce nip ; inline
33 dup fixnum? [ (^fixnum) ] [ (^bignum) ] if ; inline
35 GENERIC# ^n 1 ( z w -- z^w ) foldable
40 [ factor-2s ] dip [ (^n) ] keep rot * shift ;
43 [ >fraction ] dip '[ _ ^n ] bi@ / ;
49 : integer^ ( x y -- z )
50 dup 0 >= [ ^n ] [ [ recip ] dip neg ^n ] if ; inline
54 : >float-rect ( z -- x y )
55 >rect [ >float ] bi@ ; inline
57 : >polar ( z -- abs arg )
58 >float-rect [ [ sq ] bi@ + fsqrt ] [ swap fatan2 ] 2bi ; inline
60 : cis ( arg -- z ) >float [ fcos ] [ fsin ] bi rect> ; inline
62 : polar> ( abs arg -- z ) cis * ; inline
64 GENERIC: e^ ( x -- y )
66 M: float e^ fexp ; inline
68 M: real e^ >float e^ ; inline
70 M: complex e^ >rect [ e^ ] dip polar> ; inline
74 : ^mag ( w abs arg -- magnitude )
76 [ >float swap >float fpow ]
80 : ^theta ( w abs arg -- theta )
81 [ >float-rect ] [ flog * swap ] [ * + ] tri* ; inline
83 : ^complex ( x y -- z )
84 swap >polar [ ^mag ] [ ^theta ] 3bi polar> ; inline
87 2dup [ real? ] both? [ drop 0 >= ] [ 2drop f ] if ; inline
90 swap [ 0/0. ] swap '[ 0 < 1/0. _ ? ] if-zero ; inline
92 : (^mod) ( x y n -- z )
93 [ make-bits 1 ] dip dup
94 '[ [ over * _ mod ] when [ sq _ mod ] dip ] reduce nip ; inline
100 { [ over zero? ] [ 0^ ] }
101 { [ dup integer? ] [ integer^ ] }
102 { [ 2dup real^? ] [ [ >float ] bi@ fpow ] }
106 : nth-root ( n x -- y ) swap recip ^ ; inline
109 [ * ] 2keep fast-gcd /i ; foldable
111 : divisor? ( m n -- ? )
114 ERROR: non-trivial-divisor n ;
116 : mod-inv ( x n -- y )
117 [ nip ] [ gcd 1 = ] 2bi
118 [ dup 0 < [ + ] [ nip ] if ]
119 [ throw-non-trivial-divisor ] if ; foldable
121 : ^mod ( x y n -- z )
123 [ [ [ neg ] dip ^mod ] keep mod-inv ] [ (^mod) ] if ; foldable
125 GENERIC: absq ( x -- y ) foldable
127 M: real absq sq ; inline
129 : ~abs ( x y epsilon -- ? )
132 : ~rel ( x y epsilon -- ? )
133 [ [ - abs ] 2keep [ abs ] bi@ + ] dip * <= ;
135 : ~ ( x y epsilon -- ? )
137 { [ dup zero? ] [ drop number= ] }
138 { [ dup 0 < ] [ neg ~rel ] }
142 : conjugate ( z -- z* ) >rect neg rect> ; inline
144 : arg ( z -- arg ) >float-rect swap fatan2 ; inline
147 dup complex? [ drop f ] [ abs 1 <= ] if ; inline
150 dup complex? [ drop f ] [ 1 >= ] if ; inline
152 GENERIC: frexp ( x -- y exp )
155 dup fp-special? [ dup zero? ] unless* [ 0 ] [
157 [ 0x800f,ffff,ffff,ffff bitand 0.5 double>bits bitor bits>double ]
158 [ -52 shift 0x7ff bitand 1022 - ] bi
163 dup 0 > [ 1 ] [ abs -1 ] if swap dup log2 [
164 52 swap - shift 0x000f,ffff,ffff,ffff bitand
165 0.5 double>bits bitor bits>double
166 ] [ 1 + ] bi [ * ] dip
171 GENERIC# ldexp 1 ( x exp -- y )
174 over fp-special? [ over zero? ] unless* [ drop ] [
175 [ double>bits dup -52 shift 0x7ff bitand 1023 - ] dip +
177 { [ dup -1074 < ] [ drop 0 copysign ] }
178 { [ dup 1023 > ] [ drop 0 < -1/0. 1/0. ? ] }
180 dup -1022 < [ 52 + -52 2^ ] [ 1 ] if
181 [ -0x7ff0,0000,0000,0001 bitand ]
182 [ 1023 + 52 shift bitor bits>double ]
189 2dup [ zero? ] either? [ 2drop 0 ] [ shift ] if ;
191 GENERIC: log ( x -- y )
193 M: float log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ; inline
195 M: real log >float log ; inline
197 M: complex log >polar [ flog ] dip rect> ; inline
201 : most-negative-finite-float ( -- x )
202 -0x1.ffff,ffff,ffff,fp1023 >integer ; inline
204 : most-positive-finite-float ( -- x )
205 0x1.ffff,ffff,ffff,fp1023 >integer ; inline
207 CONSTANT: log-2 0x1.62e42fefa39efp-1
208 CONSTANT: log10-2 0x1.34413509f79ffp-2
210 : representable-as-float? ( x -- ? )
211 most-negative-finite-float
212 most-positive-finite-float between? ; inline
214 : (bignum-log) ( n log-quot: ( x -- y ) log-2 -- log )
216 dup representable-as-float?
217 [ >float @ ] [ frexp [ @ ] [ _ * ] bi* + ] if
222 M: bignum log [ log ] log-2 (bignum-log) ;
224 GENERIC: log1+ ( x -- y )
226 M: object log1+ 1 + log ; inline
228 M: float log1+ dup -1.0 >= [ flog1+ ] [ 1.0 + 0.0 rect> log ] if ; inline
230 : 10^ ( x -- y ) 10 swap ^ ; inline
232 GENERIC: log10 ( x -- y ) foldable
234 M: real log10 >float flog10 ; inline
236 M: complex log10 log 10 log / ; inline
238 M: bignum log10 [ log10 ] log10-2 (bignum-log) ;
240 GENERIC: cos ( x -- y ) foldable
244 [ [ fcos ] [ fcosh ] bi* * ]
245 [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
247 M: float cos fcos ; inline
249 M: real cos >float cos ; inline
251 : sec ( x -- y ) cos recip ; inline
253 GENERIC: cosh ( x -- y ) foldable
257 [ [ fcosh ] [ fcos ] bi* * ]
258 [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
260 M: float cosh fcosh ; inline
262 M: real cosh >float cosh ; inline
264 : sech ( x -- y ) cosh recip ; inline
266 GENERIC: sin ( x -- y ) foldable
270 [ [ fsin ] [ fcosh ] bi* * ]
271 [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
273 M: float sin fsin ; inline
275 M: real sin >float sin ; inline
277 : cosec ( x -- y ) sin recip ; inline
279 GENERIC: sinh ( x -- y ) foldable
283 [ [ fsinh ] [ fcos ] bi* * ]
284 [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
286 M: float sinh fsinh ; inline
288 M: real sinh >float sinh ; inline
290 : cosech ( x -- y ) sinh recip ; inline
292 GENERIC: tan ( x -- y ) foldable
294 M: complex tan [ sin ] [ cos ] bi / ;
296 M: float tan ftan ; inline
298 M: real tan >float tan ; inline
300 GENERIC: tanh ( x -- y ) foldable
302 M: complex tanh [ sinh ] [ cosh ] bi / ;
304 M: float tanh ftanh ; inline
306 M: real tanh >float tanh ; inline
308 : cot ( x -- y ) tan recip ; inline
310 : coth ( x -- y ) tanh recip ; inline
313 dup sq 1 - sqrt + log ; inline
315 : asech ( x -- y ) recip acosh ; inline
318 dup sq 1 + sqrt + log ; inline
320 : acosech ( x -- y ) recip asinh ; inline
323 [ 1 + ] [ 1 - neg ] bi / log 2 / ; inline
325 : acoth ( x -- y ) recip atanh ; inline
327 : i* ( x -- y ) >rect neg swap rect> ;
329 : -i* ( x -- y ) >rect swap neg rect> ;
332 dup [-1,1]? [ >float fasin ] [ i* asinh -i* ] if ; inline
335 dup [-1,1]? [ >float facos ] [ asin pi 2 / swap - ] if ; inline
337 GENERIC: atan ( x -- y ) foldable
339 M: complex atan i* atanh i* ; inline
341 M: float atan fatan ; inline
343 M: real atan >float atan ; inline
345 : asec ( x -- y ) recip acos ; inline
347 : acosec ( x -- y ) recip asin ; inline
349 : acot ( x -- y ) recip atan ; inline
351 : truncate ( x -- y ) dup 1 mod - ; inline
353 GENERIC: round ( x -- y )
355 M: integer round ; inline
358 >fraction [ /mod abs 2 * ] keep >= [ dup 0 < -1 1 ? + ] when ;
360 M: float round dup sgn 2 /f + truncate ;
364 [ dup 0 < [ - 1 - ] [ - ] if ] unless-zero ; foldable
366 : ceiling ( x -- y ) neg floor neg ; foldable
368 : floor-to ( x step -- y )
369 [ [ / floor ] [ * ] bi ] unless-zero ;
371 : lerp ( a b t -- a_t ) [ over - ] dip * + ; inline
373 : roots ( x t -- seq )
374 [ [ log ] [ recip ] bi* * e^ ]
375 [ recip 2pi * 0 swap complex boa e^ ]
376 [ iota [ ^ * ] 2with map ] tri ;
378 : sigmoid ( x -- y ) neg e^ 1 + recip ; inline
380 GENERIC: signum ( x -- y )
384 M: complex signum dup abs / ;
386 MATH: copysign ( x y -- x' )
388 M: real copysign >float copysign ;
391 [ double>bits ] [ fp-sign ] bi*
392 [ 63 2^ bitor ] [ 63 2^ bitnot bitand ] if