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