1 ! Copyright (C) 2004, 2008 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 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
16 >float dup 0.0 < [ neg fsqrt 0.0 swap rect> ] [ fsqrt ] if ; inline
18 : factor-2s ( n -- r s )
19 #! factor an integer into 2^r * s
21 0 swap [ dup even? ] [ [ 1 + ] [ 2/ ] bi* ] while
26 GENERIC# ^n 1 ( z w -- z^w ) foldable
29 make-bits 1 [ [ dupd * ] when [ sq ] dip ] reduce nip ; inline
32 [ factor-2s ] dip [ (^n) ] keep rot * shift ;
35 [ >fraction ] dip [ ^n ] curry bi@ / ;
41 : integer^ ( x y -- z )
42 dup 0 > [ ^n ] [ neg ^n recip ] if ; inline
47 [ real-part ] [ imaginary-part ] bi ; inline
49 : >float-rect ( z -- x y )
50 >rect [ >float ] bi@ ; inline
52 : >polar ( z -- abs arg )
53 >float-rect [ [ sq ] bi@ + fsqrt ] [ swap fatan2 ] 2bi ; inline
55 : cis ( arg -- z ) >float [ fcos ] [ fsin ] bi rect> ; inline
57 : polar> ( abs arg -- z ) cis * ; inline
59 GENERIC: exp ( x -- y )
61 M: float exp fexp ; inline
63 M: real exp >float exp ; inline
65 M: complex exp >rect swap fexp swap polar> ; inline
69 : ^mag ( w abs arg -- magnitude )
71 [ >float swap >float fpow ]
75 : ^theta ( w abs arg -- theta )
76 [ >float-rect ] [ flog * swap ] [ * + ] tri* ; inline
78 : ^complex ( x y -- z )
79 swap >polar [ ^mag ] [ ^theta ] 3bi polar> ; inline
82 2dup [ real? ] both? [ drop 0 >= ] [ 2drop f ] if ; inline
85 [ 0/0. ] [ 0 < 1/0. 0 ? ] if-zero ; inline
87 : (^mod) ( n x y -- z )
89 [ dupd * pick mod ] when [ sq over mod ] dip
90 ] reduce 2nip ; inline
92 : (gcd) ( b a x y -- a d )
96 swap [ /mod [ over * swapd - ] dip ] keep (gcd)
103 { [ over 0 = ] [ nip 0^ ] }
104 { [ dup integer? ] [ integer^ ] }
105 { [ 2dup real^? ] [ [ >float ] bi@ fpow ] }
110 [ 0 1 ] 2dip (gcd) dup 0 < [ neg ] when ; foldable
113 [ * ] 2keep gcd nip /i ; foldable
115 : divisor? ( m n -- ? )
118 ERROR: non-trivial-divisor n ;
120 : mod-inv ( x n -- y )
121 [ nip ] [ gcd 1 = ] 2bi
122 [ dup 0 < [ + ] [ nip ] if ]
123 [ non-trivial-divisor ] if ; foldable
125 : ^mod ( x y n -- z )
127 [ [ neg ] dip ^mod ] keep mod-inv
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 { [ 2over [ fp-nan? ] either? ] [ 3drop f ] }
145 { [ dup zero? ] [ drop number= ] }
146 { [ dup 0 < ] [ ~rel ] }
150 : conjugate ( z -- z* ) >rect neg rect> ; inline
152 : arg ( z -- arg ) >float-rect swap fatan2 ; inline
155 dup complex? [ drop f ] [ abs 1 <= ] if ; inline
158 dup complex? [ drop f ] [ 1 >= ] if ; inline
160 GENERIC: log ( x -- y )
162 M: float log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ; inline
164 M: real log >float log ; inline
166 M: complex log >polar swap flog swap rect> ; inline
168 : 10^ ( x -- y ) 10 swap ^ ; inline
170 : log10 ( x -- y ) log 10 log / ; inline
172 GENERIC: cos ( x -- y ) foldable
176 [ [ fcos ] [ fcosh ] bi* * ]
177 [ [ fsin neg ] [ fsinh ] bi* * ] 2bi rect> ;
179 M: float cos fcos ; inline
181 M: real cos >float cos ; inline
183 : sec ( x -- y ) cos recip ; inline
185 GENERIC: cosh ( x -- y ) foldable
189 [ [ fcosh ] [ fcos ] bi* * ]
190 [ [ fsinh ] [ fsin ] bi* * ] 2bi rect> ;
192 M: float cosh fcosh ; inline
194 M: real cosh >float cosh ; inline
196 : sech ( x -- y ) cosh recip ; inline
198 GENERIC: sin ( x -- y ) foldable
202 [ [ fsin ] [ fcosh ] bi* * ]
203 [ [ fcos ] [ fsinh ] bi* * ] 2bi rect> ;
205 M: float sin fsin ; inline
207 M: real sin >float sin ; inline
209 : cosec ( x -- y ) sin recip ; inline
211 GENERIC: sinh ( x -- y ) foldable
215 [ [ fsinh ] [ fcos ] bi* * ]
216 [ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
218 M: float sinh fsinh ; inline
220 M: real sinh >float sinh ; inline
222 : cosech ( x -- y ) sinh recip ; inline
224 GENERIC: tan ( x -- y ) foldable
226 M: complex tan [ sin ] [ cos ] bi / ;
228 M: float tan ftan ; inline
230 M: real tan >float tan ; inline
232 GENERIC: tanh ( x -- y ) foldable
234 M: complex tanh [ sinh ] [ cosh ] bi / ;
236 M: float tanh ftanh ; inline
238 M: real tanh >float tanh ; inline
240 : cot ( x -- y ) tan recip ; inline
242 : coth ( x -- y ) tanh recip ; inline
245 dup sq 1 - sqrt + log ; inline
247 : asech ( x -- y ) recip acosh ; inline
250 dup sq 1 + sqrt + log ; inline
252 : acosech ( x -- y ) recip asinh ; inline
255 [ 1 + ] [ 1 - neg ] bi / log 2 / ; inline
257 : acoth ( x -- y ) recip atanh ; inline
259 : i* ( x -- y ) >rect neg swap rect> ;
261 : -i* ( x -- y ) >rect swap neg rect> ;
264 dup [-1,1]? [ >float fasin ] [ i* asinh -i* ] if ; inline
267 dup [-1,1]? [ >float facos ] [ asin pi 2 / swap - ] if ;
270 GENERIC: atan ( x -- y ) foldable
272 M: complex atan i* atanh i* ; inline
274 M: float atan fatan ; inline
276 M: real atan >float atan ; inline
278 : asec ( x -- y ) recip acos ; inline
280 : acosec ( x -- y ) recip asin ; inline
282 : acot ( x -- y ) recip atan ; inline
284 : truncate ( x -- y ) dup 1 mod - ; inline
286 : round ( x -- y ) dup sgn 2 / + truncate ; inline
290 [ ] [ dup 0 < [ - 1 - ] [ - ] if ] if-zero ; foldable
292 : ceiling ( x -- y ) neg floor neg ; foldable
294 : floor-to ( x step -- y )
295 [ [ / floor ] [ * ] bi ] unless-zero ;
297 : lerp ( a b t -- a_t ) [ over - ] dip * + ; inline