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
160 ! to avoid circular dependency on math.bitwise
161 : on-bits ( m -- n ) 2^ 1 - ; inline
164 GENERIC: frexp ( x -- y exp )
167 dup { [ fp-special? ] [ zero? ] } 1|| [ 0 ] [
169 [ HEX: 800f,ffff,ffff,ffff bitand 0.5 double>bits bitor bits>double ]
170 [ -52 shift 11 on-bits bitand 1022 - ] bi
175 dup 0 > [ 1 ] [ abs -1 ] if swap dup log2 [
176 52 swap - shift 52 on-bits 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 CONSTANT: most-negative-finite-float $[ -1/0. next-float >integer ]
192 CONSTANT: most-positive-finite-float $[ 1/0. prev-float >integer ]
194 MACRO: bignum-loghelper ( quot: ( x -- y ) -- quot )
195 dup 2 over call( x -- y ) '[
197 most-negative-finite-float
198 most-positive-finite-float
200 [ >float @ ] [ frexp [ @ ] [ _ * ] bi* + ] if
205 M: bignum log [ log ] bignum-loghelper ;
207 GENERIC: log1+ ( x -- y )
209 M: object log1+ 1 + log ; inline
211 M: float log1+ dup -1.0 >= [ flog1+ ] [ 1.0 + 0.0 rect> log ] if ; inline
213 : 10^ ( x -- y ) 10 swap ^ ; inline
215 GENERIC: log10 ( x -- y ) foldable
217 M: real log10 >float flog10 ; inline
219 M: complex log10 log 10 log / ; inline
221 M: bignum log10 [ log10 ] bignum-loghelper ;
223 GENERIC: cos ( x -- y ) foldable
227 [ [ fcos ] [ fcosh ] bi* * ]
228 [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
230 M: float cos fcos ; inline
232 M: real cos >float cos ; inline
234 : sec ( x -- y ) cos recip ; inline
236 GENERIC: cosh ( x -- y ) foldable
240 [ [ fcosh ] [ fcos ] bi* * ]
241 [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
243 M: float cosh fcosh ; inline
245 M: real cosh >float cosh ; inline
247 : sech ( x -- y ) cosh recip ; inline
249 GENERIC: sin ( x -- y ) foldable
253 [ [ fsin ] [ fcosh ] bi* * ]
254 [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
256 M: float sin fsin ; inline
258 M: real sin >float sin ; inline
260 : cosec ( x -- y ) sin recip ; inline
262 GENERIC: sinh ( x -- y ) foldable
266 [ [ fsinh ] [ fcos ] bi* * ]
267 [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
269 M: float sinh fsinh ; inline
271 M: real sinh >float sinh ; inline
273 : cosech ( x -- y ) sinh recip ; inline
275 GENERIC: tan ( x -- y ) foldable
277 M: complex tan [ sin ] [ cos ] bi / ;
279 M: float tan ftan ; inline
281 M: real tan >float tan ; inline
283 GENERIC: tanh ( x -- y ) foldable
285 M: complex tanh [ sinh ] [ cosh ] bi / ;
287 M: float tanh ftanh ; inline
289 M: real tanh >float tanh ; inline
291 : cot ( x -- y ) tan recip ; inline
293 : coth ( x -- y ) tanh recip ; inline
296 dup sq 1 - sqrt + log ; inline
298 : asech ( x -- y ) recip acosh ; inline
301 dup sq 1 + sqrt + log ; inline
303 : acosech ( x -- y ) recip asinh ; inline
306 [ 1 + ] [ 1 - neg ] bi / log 2 / ; inline
308 : acoth ( x -- y ) recip atanh ; inline
310 : i* ( x -- y ) >rect neg swap rect> ;
312 : -i* ( x -- y ) >rect swap neg rect> ;
315 dup [-1,1]? [ >float fasin ] [ i* asinh -i* ] if ; inline
318 dup [-1,1]? [ >float facos ] [ asin pi 2 / swap - ] if ;
321 GENERIC: atan ( x -- y ) foldable
323 M: complex atan i* atanh i* ; inline
325 M: float atan fatan ; inline
327 M: real atan >float atan ; inline
329 : asec ( x -- y ) recip acos ; inline
331 : acosec ( x -- y ) recip asin ; inline
333 : acot ( x -- y ) recip atan ; inline
335 : truncate ( x -- y ) dup 1 mod - ; inline
337 : round ( x -- y ) dup sgn 2 / + truncate ; inline
341 [ ] [ dup 0 < [ - 1 - ] [ - ] if ] if-zero ; foldable
343 : ceiling ( x -- y ) neg floor neg ; foldable
345 : floor-to ( x step -- y )
346 [ [ / floor ] [ * ] bi ] unless-zero ;
348 : lerp ( a b t -- a_t ) [ over - ] dip * + ; inline