]> gitweb.factorcode.org Git - factor.git/blob - basis/math/complex/complex.factor
Merge branch 'master' into experimental
[factor.git] / basis / math / complex / complex.factor
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
5 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@ ]
18     [ [ imaginary-part ] bi@ ] 2bi ; 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 [ * ] 2bi@ ; inline
32 : *im ( x y -- xi*yr xr*yi ) 2>rect [ * swap ] dip * ; inline
33
34 M: complex + 2>rect [ + ] 2bi@ (rect>) ;
35 M: complex - 2>rect [ - ] 2bi@ (rect>) ;
36 M: complex * [ *re - ] [ *im + ] 2bi (rect>) ;
37
38 : complex/ ( x y -- r i m )
39     [ [ *re + ] [ *im - ] 2bi ] keep absq ; inline
40
41 M: complex / complex/ tuck [ / ] 2bi@ (rect>) ;
42
43 M: complex abs absq >float fsqrt ;
44
45 M: complex sqrt >polar [ fsqrt ] [ 2.0 / ] bi* polar> ;
46
47 IN: syntax
48
49 : C{ \ } [ first2 rect> ] parse-literal ; parsing
50
51 USE: prettyprint.custom
52
53 M: complex pprint* pprint-object ;
54 M: complex pprint-delims drop \ C{ \ } ;
55 M: complex >pprint-sequence >rect 2array ;