1 ! Copyright (C) 2006 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 IN: math.complex.private
4 USING: kernel kernel.private math math.private
5 math.libm math.functions ;
8 M: real imaginary drop 0 ;
10 M: number equal? number= ;
12 M: complex absq >rect [ sq ] 2apply + ;
14 : 2>rect ( x y -- xr yr xi yi )
15 [ [ real ] 2apply ] 2keep [ imaginary ] 2apply ; inline
18 2>rect number= [ number= ] [ 2drop f ] if ;
20 : *re ( x y -- xr*yr xi*ri ) 2>rect * >r * r> ; inline
21 : *im ( x y -- xi*yr xr*yi ) 2>rect >r * swap r> * ; inline
23 M: complex + 2>rect + >r + r> (rect>) ;
24 M: complex - 2>rect - >r - r> (rect>) ;
25 M: complex * 2dup *re - -rot *im + (rect>) ;
27 : complex/ ( x y -- r i m )
28 dup absq >r 2dup *re + -rot *im - r> ; inline
30 M: complex / complex/ tuck / >r / r> (rect>) ;
32 M: complex abs absq >float fsqrt ;
34 M: complex sqrt >polar swap fsqrt swap 2.0 / polar> ;
36 M: complex hashcode* nip >rect >fixnum swap >fixnum bitxor ;