1 ! Copyright (C) 2006, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel kernel.private math math.private
4 math.libm math.functions prettyprint.backend arrays
5 math.functions.private sequences parser ;
6 IN: math.complex.private
9 M: real imaginary-part drop 0 ;
11 M: complex real-part real>> ;
12 M: complex imaginary-part imaginary>> ;
14 M: complex absq >rect [ sq ] bi@ + ;
16 : 2>rect ( x y -- xr yr xi yi )
17 [ [ real-part ] bi@ ] 2keep
18 [ imaginary-part ] bi@ ; inline
21 nip >rect [ hashcode ] bi@ bitxor ;
25 2>rect = [ = ] [ 2drop f ] if
29 2>rect number= [ number= ] [ 2drop f ] if ;
31 : *re ( x y -- xr*yr xi*ri ) 2>rect * >r * r> ; inline
32 : *im ( x y -- xi*yr xr*yi ) 2>rect >r * swap r> * ; inline
34 M: complex + 2>rect + >r + r> (rect>) ;
35 M: complex - 2>rect - >r - r> (rect>) ;
36 M: complex * 2dup *re - -rot *im + (rect>) ;
38 : complex/ ( x y -- r i m )
39 dup absq >r 2dup *re + -rot *im - r> ; inline
41 M: complex / complex/ tuck / >r / r> (rect>) ;
43 M: complex abs absq >float fsqrt ;
45 M: complex sqrt >polar swap fsqrt swap 2.0 / polar> ;
49 : C{ \ } [ first2 rect> ] parse-literal ; parsing
51 M: complex pprint-delims drop \ C{ \ } ;
52 M: complex >pprint-sequence >rect 2array ;
53 M: complex pprint* pprint-object ;