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
5 combinators.short-circuit macros literals ;
8 : >fraction ( a/b -- a b )
9 [ numerator ] [ denominator ] bi ; inline
12 dup 0 = [ drop ] [ complex boa ] if ; inline
14 GENERIC: sqrt ( x -- y ) foldable
18 [ neg fsqrt [ 0.0 ] dip rect> ] [ fsqrt ] if ; inline
20 : factor-2s ( n -- r s )
21 #! factor an integer into 2^r * s
23 [ 0 ] dip [ dup even? ] [ [ 1 + ] [ 2/ ] bi* ] while
28 GENERIC# ^n 1 ( z w -- z^w ) foldable
31 make-bits 1 [ [ over * ] when [ sq ] dip ] reduce nip ; inline
34 [ factor-2s ] dip [ (^n) ] keep rot * shift ;
37 [ >fraction ] dip '[ _ ^n ] bi@ / ;
43 : integer^ ( x y -- z )
44 dup 0 >= [ ^n ] [ [ recip ] dip neg ^n ] if ; inline
49 [ real-part ] [ imaginary-part ] bi ; inline
51 : >float-rect ( z -- x y )
52 >rect [ >float ] bi@ ; inline
54 : >polar ( z -- abs arg )
55 >float-rect [ [ sq ] bi@ + fsqrt ] [ swap fatan2 ] 2bi ; inline
57 : cis ( arg -- z ) >float [ fcos ] [ fsin ] bi rect> ; inline
59 : polar> ( abs arg -- z ) cis * ; inline
61 GENERIC: exp ( x -- y )
63 M: float exp fexp ; inline
65 M: real exp >float exp ; inline
67 M: complex exp >rect [ exp ] dip polar> ; inline
71 : ^mag ( w abs arg -- magnitude )
73 [ >float swap >float fpow ]
77 : ^theta ( w abs arg -- theta )
78 [ >float-rect ] [ flog * swap ] [ * + ] tri* ; inline
80 : ^complex ( x y -- z )
81 swap >polar [ ^mag ] [ ^theta ] 3bi polar> ; inline
84 2dup [ real? ] both? [ drop 0 >= ] [ 2drop f ] if ; inline
87 swap [ 0/0. ] swap '[ 0 < 1/0. _ ? ] if-zero ; inline
89 : (^mod) ( x y n -- z )
90 [ make-bits 1 ] dip dup
91 '[ [ over * _ mod ] when [ sq _ mod ] dip ] reduce nip ; inline
93 : (gcd) ( b a x y -- a d )
97 swap [ /mod [ over * swapd - ] dip ] keep (gcd)
104 { [ over zero? ] [ 0^ ] }
105 { [ dup integer? ] [ integer^ ] }
106 { [ 2dup real^? ] [ [ >float ] bi@ fpow ] }
110 : nth-root ( n x -- y ) swap recip ^ ; inline
113 [ 0 1 ] 2dip (gcd) dup 0 < [ neg ] when ; foldable
116 [ * ] 2keep gcd nip /i ; foldable
118 : divisor? ( m n -- ? )
121 ERROR: non-trivial-divisor n ;
123 : mod-inv ( x n -- y )
124 [ nip ] [ gcd 1 = ] 2bi
125 [ dup 0 < [ + ] [ nip ] if ]
126 [ non-trivial-divisor ] if ; foldable
128 : ^mod ( x y n -- z )
130 [ [ [ neg ] dip ^mod ] keep mod-inv ] [ (^mod) ] if ; foldable
132 GENERIC: absq ( x -- y ) foldable
134 M: real absq sq ; inline
136 : ~abs ( x y epsilon -- ? )
139 : ~rel ( x y epsilon -- ? )
140 [ [ - abs ] 2keep [ abs ] bi@ + ] dip * <= ;
142 : ~ ( x y epsilon -- ? )
144 { [ dup zero? ] [ drop number= ] }
145 { [ dup 0 < ] [ neg ~rel ] }
149 : conjugate ( z -- z* ) >rect neg rect> ; inline
151 : arg ( z -- arg ) >float-rect swap fatan2 ; inline
154 dup complex? [ drop f ] [ abs 1 <= ] if ; inline
157 dup complex? [ drop f ] [ 1 >= ] if ; inline
159 GENERIC: frexp ( x -- y exp )
162 dup { [ fp-special? ] [ zero? ] } 1|| [ 0 ] [
164 [ HEX: 800f,ffff,ffff,ffff bitand 0.5 double>bits bitor bits>double ]
165 [ -52 shift HEX: 7ff bitand 1022 - ] bi
170 dup 0 > [ 1 ] [ abs -1 ] if swap dup log2 [
171 52 swap - shift HEX: 000f,ffff,ffff,ffff bitand
172 0.5 double>bits bitor bits>double
173 ] [ 1 + ] bi [ * ] dip
176 GENERIC: log ( x -- y )
178 M: float log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ; inline
180 M: real log >float log ; inline
182 M: complex log >polar [ flog ] dip rect> ; inline
186 CONSTANT: most-negative-finite-float $[ -1/0. next-float >integer ]
187 CONSTANT: most-positive-finite-float $[ 1/0. prev-float >integer ]
189 MACRO: bignum-log ( quot: ( x -- y ) -- quot )
192 most-negative-finite-float
193 most-positive-finite-float
195 [ >float @ ] [ frexp [ @ ] [ 2 @ * ] bi* + ] if
200 M: bignum log [ log ] bignum-log ;
202 GENERIC: log1+ ( x -- y )
204 M: object log1+ 1 + log ; inline
206 M: float log1+ dup -1.0 >= [ flog1+ ] [ 1.0 + 0.0 rect> log ] if ; inline
208 : 10^ ( x -- y ) 10 swap ^ ; inline
210 GENERIC: log10 ( x -- y ) foldable
212 M: real log10 >float flog10 ; inline
214 M: complex log10 log 10 log / ; inline
216 M: bignum log10 [ log10 ] bignum-log ;
218 GENERIC: cos ( x -- y ) foldable
222 [ [ fcos ] [ fcosh ] bi* * ]
223 [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
225 M: float cos fcos ; inline
227 M: real cos >float cos ; inline
229 : sec ( x -- y ) cos recip ; inline
231 GENERIC: cosh ( x -- y ) foldable
235 [ [ fcosh ] [ fcos ] bi* * ]
236 [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
238 M: float cosh fcosh ; inline
240 M: real cosh >float cosh ; inline
242 : sech ( x -- y ) cosh recip ; inline
244 GENERIC: sin ( x -- y ) foldable
248 [ [ fsin ] [ fcosh ] bi* * ]
249 [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
251 M: float sin fsin ; inline
253 M: real sin >float sin ; inline
255 : cosec ( x -- y ) sin recip ; inline
257 GENERIC: sinh ( x -- y ) foldable
261 [ [ fsinh ] [ fcos ] bi* * ]
262 [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
264 M: float sinh fsinh ; inline
266 M: real sinh >float sinh ; inline
268 : cosech ( x -- y ) sinh recip ; inline
270 GENERIC: tan ( x -- y ) foldable
272 M: complex tan [ sin ] [ cos ] bi / ;
274 M: float tan ftan ; inline
276 M: real tan >float tan ; inline
278 GENERIC: tanh ( x -- y ) foldable
280 M: complex tanh [ sinh ] [ cosh ] bi / ;
282 M: float tanh ftanh ; inline
284 M: real tanh >float tanh ; inline
286 : cot ( x -- y ) tan recip ; inline
288 : coth ( x -- y ) tanh recip ; inline
291 dup sq 1 - sqrt + log ; inline
293 : asech ( x -- y ) recip acosh ; inline
296 dup sq 1 + sqrt + log ; inline
298 : acosech ( x -- y ) recip asinh ; inline
301 [ 1 + ] [ 1 - neg ] bi / log 2 / ; inline
303 : acoth ( x -- y ) recip atanh ; inline
305 : i* ( x -- y ) >rect neg swap rect> ;
307 : -i* ( x -- y ) >rect swap neg rect> ;
310 dup [-1,1]? [ >float fasin ] [ i* asinh -i* ] if ; inline
313 dup [-1,1]? [ >float facos ] [ asin pi 2 / swap - ] if ;
316 GENERIC: atan ( x -- y ) foldable
318 M: complex atan i* atanh i* ; inline
320 M: float atan fatan ; inline
322 M: real atan >float atan ; inline
324 : asec ( x -- y ) recip acos ; inline
326 : acosec ( x -- y ) recip asin ; inline
328 : acot ( x -- y ) recip atan ; inline
330 : truncate ( x -- y ) dup 1 mod - ; inline
332 : round ( x -- y ) dup sgn 2 / + truncate ; inline
336 [ ] [ dup 0 < [ - 1 - ] [ - ] if ] if-zero ; foldable
338 : ceiling ( x -- y ) neg floor neg ; foldable
340 : floor-to ( x step -- y )
341 [ [ / floor ] [ * ] bi ] unless-zero ;
343 : lerp ( a b t -- a_t ) [ over - ] dip * + ; inline