1 ! Copyright (C) 2004, 2007 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: math kernel math.constants math.private
4 math.libm combinators math.order ;
10 dup zero? [ drop ] [ <complex> ] if ; inline
15 over real? over real? and [
18 "Complex number must have real components" throw
21 GENERIC: sqrt ( x -- y ) foldable
24 >float dup 0.0 < [ neg fsqrt 0.0 swap rect> ] [ fsqrt ] if ;
26 : each-bit ( n quot: ( ? -- ) -- )
27 over 0 number= pick -1 number= or [
30 2dup >r >r >r odd? r> call r> 2/ r> each-bit
31 ] if ; inline recursive
33 GENERIC: (^) ( x y -- z ) foldable
37 [ dupd * ] when >r sq r>
38 ] each-bit nip ; inline
41 dup 0 < [ neg ^n recip ] [ ^n ] if ;
46 [ 2drop 0.0 0.0 / ] [ 0 < [ drop 1.0 0.0 / ] when ] if
51 : (^mod) ( n x y -- z )
53 [ dupd * pick mod ] when >r sq over mod r>
54 ] each-bit 2nip ; inline
56 : (gcd) ( b a x y -- a d )
60 swap [ /mod >r over * swapd - r> ] keep (gcd)
64 0 -rot 1 -rot (gcd) dup 0 < [ neg ] when ; foldable
67 [ * ] 2keep gcd nip /i ; foldable
69 : mod-inv ( x n -- y )
71 dup 0 < [ + ] [ nip ] if
73 "Non-trivial divisor found" throw
78 [ >r neg r> ^mod ] keep mod-inv
83 GENERIC: absq ( x -- y ) foldable
87 : ~abs ( x y epsilon -- ? )
90 : ~rel ( x y epsilon -- ? )
91 >r [ - abs ] 2keep [ abs ] bi@ + r> * < ;
93 : ~ ( x y epsilon -- ? )
95 { [ pick fp-nan? pick fp-nan? or ] [ 3drop f ] }
96 { [ dup zero? ] [ drop number= ] }
97 { [ dup 0 < ] [ ~rel ] }
101 : >rect ( z -- x y ) dup real-part swap imaginary-part ; inline
103 : conjugate ( z -- z* ) >rect neg rect> ; inline
105 : >float-rect ( z -- x y )
106 >rect swap >float swap >float ; inline
108 : arg ( z -- arg ) >float-rect swap fatan2 ; inline
110 : >polar ( z -- abs arg )
111 >float-rect [ [ sq ] bi@ + fsqrt ] 2keep swap fatan2 ;
114 : cis ( arg -- z ) dup fcos swap fsin rect> ; inline
116 : polar> ( abs arg -- z ) cis * ; inline
118 : ^mag ( w abs arg -- magnitude )
119 >r >r >float-rect swap r> swap fpow r> rot * fexp /f ;
122 : ^theta ( w abs arg -- theta )
123 >r >r >float-rect r> flog * swap r> * + ; inline
126 swap >polar 3dup ^theta >r ^mag r> polar> ;
129 dup complex? [ drop f ] [ abs 1 <= ] if ; inline
132 dup complex? [ drop f ] [ 1 >= ] if ; inline
134 : exp ( x -- y ) >rect swap fexp swap polar> ; inline
136 : log ( x -- y ) >polar swap flog swap rect> ; inline
141 fcosh swap fcos * -rot
142 fsinh swap fsin neg * rect>
143 ] [ fcos ] if ; foldable
145 : sec ( x -- y ) cos recip ; inline
150 fcos swap fcosh * -rot
151 fsin swap fsinh * rect>
152 ] [ fcosh ] if ; foldable
154 : sech ( x -- y ) cosh recip ; inline
159 fcosh swap fsin * -rot
160 fsinh swap fcos * rect>
161 ] [ fsin ] if ; foldable
163 : cosec ( x -- y ) sin recip ; inline
168 fcos swap fsinh * -rot
169 fsin swap fcosh * rect>
170 ] [ fsinh ] if ; foldable
172 : cosech ( x -- y ) sinh recip ; inline
175 dup complex? [ dup sin swap cos / ] [ ftan ] if ; inline
178 dup complex? [ dup sinh swap cosh / ] [ ftanh ] if ; inline
180 : cot ( x -- y ) tan recip ; inline
182 : coth ( x -- y ) tanh recip ; inline
185 dup sq 1- sqrt + log ; inline
187 : asech ( x -- y ) recip acosh ; inline
190 dup sq 1+ sqrt + log ; inline
192 : acosech ( x -- y ) recip asinh ; inline
195 dup 1+ swap 1- neg / log 2 / ; inline
197 : acoth ( x -- y ) recip atanh ; inline
199 : i* ( x -- y ) >rect neg swap rect> ;
201 : -i* ( x -- y ) >rect swap neg rect> ;
204 dup [-1,1]? [ fasin ] [ i* asinh -i* ] if ; inline
207 dup [-1,1]? [ facos ] [ asin pi 2 / swap - ] if ;
211 dup complex? [ i* atanh i* ] [ fatan ] if ; inline
213 : asec ( x -- y ) recip acos ; inline
215 : acosec ( x -- y ) recip asin ; inline
217 : acot ( x -- y ) recip atan ; inline
219 : truncate ( x -- y ) dup 1 mod - ; inline
221 : round ( x -- y ) dup sgn 2 / + truncate ; inline
225 [ drop ] [ dup 0 < [ - 1- ] [ - ] if ] if ; foldable
227 : ceiling ( x -- y ) neg floor neg ; foldable