1 ! Copyright (C) 2006, 2009 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 arrays math.functions.private sequences
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 )
18 [ [ imaginary-part ] bi@ ] 2bi ; 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 [ * ] 2bi@ ; inline
32 : *im ( x y -- xi*yr xr*yi ) 2>rect [ * swap ] dip * ; inline
34 M: complex + 2>rect [ + ] 2bi@ (rect>) ;
35 M: complex - 2>rect [ - ] 2bi@ (rect>) ;
36 M: complex * [ *re - ] [ *im + ] 2bi (rect>) ;
38 : complex/ ( x y -- r i m )
39 [ [ *re + ] [ *im - ] 2bi ] keep absq ; inline
41 M: complex / complex/ tuck [ / ] 2bi@ (rect>) ;
43 M: complex abs absq >float fsqrt ;
45 M: complex sqrt >polar [ fsqrt ] [ 2.0 / ] bi* polar> ;
49 : C{ \ } [ first2 rect> ] parse-literal ; parsing
51 USE: prettyprint.custom
53 M: complex pprint* pprint-object ;
54 M: complex pprint-delims drop \ C{ \ } ;
55 M: complex >pprint-sequence >rect 2array ;