]> gitweb.factorcode.org Git - factor.git/blob - extra/math/complex/complex.factor
588f34d3fcc3166f4fa40ad3abf6b9f2cc088c79
[factor.git] / extra / math / complex / complex.factor
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 prettyprint.backend arrays
6 math.functions.private sequences parser ;
7
8 M: real real-part ;
9 M: real imaginary-part drop 0 ;
10
11 M: complex absq >rect [ sq ] bi@ + ;
12
13 : 2>rect ( x y -- xr yr xi yi )
14     [ [ real-part ] bi@ ] 2keep
15     [ imaginary-part ] bi@ ; inline
16
17 M: complex number=
18     2>rect number= [ number= ] [ 2drop f ] if ;
19
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
22
23 M: complex + 2>rect + >r + r> (rect>) ;
24 M: complex - 2>rect - >r - r> (rect>) ;
25 M: complex * 2dup *re - -rot *im + (rect>) ;
26
27 : complex/ ( x y -- r i m )
28     dup absq >r 2dup *re + -rot *im - r> ; inline
29
30 M: complex / complex/ tuck / >r / r> (rect>) ;
31
32 M: complex abs absq >float fsqrt ;
33
34 M: complex sqrt >polar swap fsqrt swap 2.0 / polar> ;
35
36 M: complex hashcode* nip >rect >fixnum swap >fixnum bitxor ;
37
38 IN: syntax
39
40 : C{ \ } [ first2 rect> ] parse-literal ; parsing
41
42 M: complex pprint-delims drop \ C{ \ } ;
43
44 M: complex >pprint-sequence >rect 2array ;