]> gitweb.factorcode.org Git - factor.git/blob - basis/math/complex/complex.factor
acc8a9d6d6f9505da81b43b1bb436d9ddd8ec059
[factor.git] / basis / 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 hashcode*
21     nip >rect [ hashcode ] bi@ bitxor ;
22
23 M: complex equal?
24     over complex? [
25         2>rect = [ = ] [ 2drop f ] if
26     ] [ 2drop f ] if ;
27
28 M: complex number=
29     2>rect number= [ number= ] [ 2drop f ] if ;
30
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
33
34 M: complex + 2>rect + >r + r> (rect>) ;
35 M: complex - 2>rect - >r - r> (rect>) ;
36 M: complex * 2dup *re - -rot *im + (rect>) ;
37
38 : complex/ ( x y -- r i m )
39     dup absq >r 2dup *re + -rot *im - r> ; inline
40
41 M: complex / complex/ tuck / >r / r> (rect>) ;
42
43 M: complex abs absq >float fsqrt ;
44
45 M: complex sqrt >polar swap fsqrt swap 2.0 / polar> ;
46
47 IN: syntax
48
49 : C{ \ } [ first2 rect> ] parse-literal ; parsing
50
51 M: complex pprint-delims drop \ C{ \ } ;
52 M: complex >pprint-sequence >rect 2array ;
53 M: complex pprint* pprint-object ;