]> gitweb.factorcode.org Git - factor.git/blob - core/math/complex/complex.factor
Initial import
[factor.git] / core / 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 ;
6
7 M: real real ;
8 M: real imaginary drop 0 ;
9
10 M: number equal? number= ;
11
12 M: complex absq >rect [ sq ] 2apply + ;
13
14 : 2>rect ( x y -- xr yr xi yi )
15     [ [ real ] 2apply ] 2keep [ imaginary ] 2apply ; 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 ;