Complex:
CEDAR
DEFINITIONS
IMPORTS RealFns =
BEGIN
Number: TYPE = RECORD [ re, im: REAL ];
Polar: TYPE = RECORD [magnitude, angleDeg: REAL ];
Add:
PROC [ x, y: Number ]
RETURNS [ Number ] =
INLINE
{RETURN[[re: x.re+y.re, im: x.im+y.im]]};
Subtract:
PROC [ x, y: Number ]
RETURNS [ Number ] =
INLINE
{RETURN[[re: x.re-y.re, im: x.im-y.im]]};
Multiply:
PROC [ x, y: Number ]
RETURNS [ Number ] =
INLINE
{RETURN[[re: x.re*y.re-x.im*y.im, im: x.re*y.im+y.re*x.im]]};
Divide:
PROC [ x, y: Number ]
RETURNS [ Number ] =
INLINE
BEGIN
div: REAL = y.re*y.re+y.im*y.im;
RETURN[[re: (x.re*y.re+x.im*y.im)/div, im: (x.im*y.re-x.re*y.im)/div]];
END;
Magnitude:
PROC [ x: Number ]
RETURNS [
REAL ] =
INLINE
{RETURN[RealFns.SqRt[x.re*x.re+x.im*x.im]]};
AngleDeg:
PROC [ x: Number ]
RETURNS [
REAL
-- [-180.0..180.0) -- ] =
INLINE
{RETURN[RealFns.ArcTanDeg[y: x.im, x: x.re]]};
FromPolar:
PROC [ x: Polar ]
RETURNS [ Number ] =
INLINE
{RETURN[[re: x.magnitude*RealFns.CosDeg[x.angleDeg],
im: x.magnitude*RealFns.SinDeg[x.angleDeg]]]};
ToPolar:
PROC [ x: Number ]
RETURNS [ Polar ] =
INLINE
{RETURN[[magnitude: Magnitude[x], angleDeg: AngleDeg[x]]]};
END.