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 0 = [ 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 = pick -1 = or [
30 2dup >r >r >r odd? r> call r> 2/ r> each-bit
31 ] if ; inline recursive
35 [ dupd * ] when >r sq r>
36 ] each-bit nip ; inline
38 : integer^ ( x y -- z )
39 dup 0 > [ ^n ] [ neg ^n recip ] if ; inline
42 [ real-part ] [ imaginary-part ] bi ; inline
44 : >float-rect ( z -- x y )
45 >rect [ >float ] bi@ ; inline
47 : >polar ( z -- abs arg )
48 >float-rect [ [ sq ] bi@ + fsqrt ] [ swap fatan2 ] 2bi ;
51 : cis ( arg -- z ) dup fcos swap fsin rect> ; inline
53 : polar> ( abs arg -- z ) cis * ; inline
55 : ^mag ( w abs arg -- magnitude )
56 >r >r >float-rect swap r> swap fpow r> rot * fexp /f ;
59 : ^theta ( w abs arg -- theta )
60 >r >r >float-rect r> flog * swap r> * + ; inline
62 : ^complex ( x y -- z )
63 swap >polar [ ^mag ] [ ^theta ] 3bi polar> ; inline
66 2dup [ real? ] both? [ drop 0 >= ] [ 2drop f ] if ; inline
69 dup zero? [ drop 0./0. ] [ 0 < 1./0. 0 ? ] if ; inline
73 { [ over zero? ] [ nip 0^ ] }
74 { [ dup integer? ] [ integer^ ] }
75 { [ 2dup real^? ] [ fpow ] }
79 : (^mod) ( n x y -- z )
81 [ dupd * pick mod ] when >r sq over mod r>
82 ] each-bit 2nip ; inline
84 : (gcd) ( b a x y -- a d )
88 swap [ /mod >r over * swapd - r> ] keep (gcd)
92 0 -rot 1 -rot (gcd) dup 0 < [ neg ] when ; foldable
95 [ * ] 2keep gcd nip /i ; foldable
97 : mod-inv ( x n -- y )
99 dup 0 < [ + ] [ nip ] if
101 "Non-trivial divisor found" throw
104 : ^mod ( x y n -- z )
106 [ >r neg r> ^mod ] keep mod-inv
111 GENERIC: absq ( x -- y ) foldable
115 : ~abs ( x y epsilon -- ? )
118 : ~rel ( x y epsilon -- ? )
119 >r [ - abs ] 2keep [ abs ] bi@ + r> * < ;
121 : ~ ( x y epsilon -- ? )
123 { [ pick fp-nan? pick fp-nan? or ] [ 3drop f ] }
124 { [ dup zero? ] [ drop number= ] }
125 { [ dup 0 < ] [ ~rel ] }
129 : conjugate ( z -- z* ) >rect neg rect> ; inline
131 : arg ( z -- arg ) >float-rect swap fatan2 ; inline
134 dup complex? [ drop f ] [ abs 1 <= ] if ; inline
137 dup complex? [ drop f ] [ 1 >= ] if ; inline
139 GENERIC: exp ( x -- y )
143 M: complex exp >rect swap fexp swap polar> ;
145 GENERIC: log ( x -- y )
147 M: real log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ;
149 M: complex log >polar swap flog swap rect> ;
154 fcosh swap fcos * -rot
155 fsinh swap fsin neg * rect>
156 ] [ fcos ] if ; foldable
158 : sec ( x -- y ) cos recip ; inline
163 fcos swap fcosh * -rot
164 fsin swap fsinh * rect>
165 ] [ fcosh ] if ; foldable
167 : sech ( x -- y ) cosh recip ; inline
172 fcosh swap fsin * -rot
173 fsinh swap fcos * rect>
174 ] [ fsin ] if ; foldable
176 : cosec ( x -- y ) sin recip ; inline
181 fcos swap fsinh * -rot
182 fsin swap fcosh * rect>
183 ] [ fsinh ] if ; foldable
185 : cosech ( x -- y ) sinh recip ; inline
188 dup complex? [ dup sin swap cos / ] [ ftan ] if ; inline
191 dup complex? [ dup sinh swap cosh / ] [ ftanh ] if ; inline
193 : cot ( x -- y ) tan recip ; inline
195 : coth ( x -- y ) tanh recip ; inline
198 dup sq 1- sqrt + log ; inline
200 : asech ( x -- y ) recip acosh ; inline
203 dup sq 1+ sqrt + log ; inline
205 : acosech ( x -- y ) recip asinh ; inline
208 dup 1+ swap 1- neg / log 2 / ; inline
210 : acoth ( x -- y ) recip atanh ; inline
212 : i* ( x -- y ) >rect neg swap rect> ;
214 : -i* ( x -- y ) >rect swap neg rect> ;
217 dup [-1,1]? [ fasin ] [ i* asinh -i* ] if ; inline
220 dup [-1,1]? [ facos ] [ asin pi 2 / swap - ] if ;
224 dup complex? [ i* atanh i* ] [ fatan ] if ; inline
226 : asec ( x -- y ) recip acos ; inline
228 : acosec ( x -- y ) recip asin ; inline
230 : acot ( x -- y ) recip atan ; inline
232 : truncate ( x -- y ) dup 1 mod - ; inline
234 : round ( x -- y ) dup sgn 2 / + truncate ; inline
238 [ drop ] [ dup 0 < [ - 1- ] [ - ] if ] if ; foldable
240 : ceiling ( x -- y ) neg floor neg ; foldable