2009-01-14 02:02:27 -05:00
|
|
|
! Copyright (C) 2006, 2009 Slava Pestov.
|
2007-09-20 18:09:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-06-28 03:36:20 -04:00
|
|
|
USING: accessors kernel kernel.private math math.private
|
2008-12-08 15:58:00 -05:00
|
|
|
math.libm math.functions arrays math.functions.private sequences
|
|
|
|
parser ;
|
2008-06-28 03:36:20 -04:00
|
|
|
IN: math.complex.private
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2007-12-27 17:26:39 -05:00
|
|
|
M: real real-part ;
|
|
|
|
M: real imaginary-part drop 0 ;
|
2008-06-28 03:36:20 -04:00
|
|
|
M: complex real-part real>> ;
|
|
|
|
M: complex imaginary-part imaginary>> ;
|
2008-03-29 21:36:58 -04:00
|
|
|
M: complex absq >rect [ sq ] bi@ + ;
|
2009-02-19 01:56:30 -05:00
|
|
|
M: complex hashcode* nip >rect [ hashcode ] bi@ bitxor ;
|
|
|
|
: componentwise ( x y quot -- a b ) [ [ >rect ] bi@ ] dip bi-curry@ bi* ; inline
|
|
|
|
: complex= ( x y quot -- ? ) componentwise and ; inline
|
|
|
|
M: complex equal? over complex? [ [ = ] complex= ] [ 2drop f ] if ;
|
|
|
|
M: complex number= [ number= ] complex= ;
|
|
|
|
: complex-op ( x y quot -- z ) componentwise (rect>) ; inline
|
|
|
|
M: complex + [ + ] complex-op ;
|
|
|
|
M: complex - [ - ] complex-op ;
|
|
|
|
: *re ( x y -- xr*yr xi*yi ) [ >rect ] bi@ [ * ] bi-curry@ bi* ; inline
|
|
|
|
: *im ( x y -- xi*yr xr*yi ) swap [ >rect ] bi@ swap [ * ] bi-curry@ bi* ; inline
|
2008-11-30 23:21:37 -05:00
|
|
|
M: complex * [ *re - ] [ *im + ] 2bi (rect>) ;
|
2009-02-19 01:56:30 -05:00
|
|
|
: (complex/) ( x y -- r i m ) [ [ *re + ] [ *im - ] 2bi ] keep absq ; inline
|
|
|
|
: complex/ ( x y quot -- z ) [ (complex/) ] dip curry bi@ (rect>) ; inline
|
|
|
|
M: complex / [ / ] complex/ ;
|
|
|
|
M: complex /f [ /f ] complex/ ;
|
|
|
|
M: complex /i [ /i ] complex/ ;
|
2007-09-20 18:09:08 -04:00
|
|
|
M: complex abs absq >float fsqrt ;
|
2008-11-30 23:21:37 -05:00
|
|
|
M: complex sqrt >polar [ fsqrt ] [ 2.0 / ] bi* polar> ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2007-10-14 20:38:23 -04:00
|
|
|
IN: syntax
|
|
|
|
|
2009-03-21 02:27:50 -04:00
|
|
|
SYNTAX: C{ \ } [ first2 rect> ] parse-literal ;
|
2009-01-14 02:02:27 -05:00
|
|
|
|
|
|
|
USE: prettyprint.custom
|
|
|
|
|
|
|
|
M: complex pprint* pprint-object ;
|
|
|
|
M: complex pprint-delims drop \ C{ \ } ;
|
|
|
|
M: complex >pprint-sequence >rect 2array ;
|