Complex.mesa
Last edited by Michael Plass 29-Sep-81 9:53:26
Written by Michael Plass, 28-Sep-81
DIRECTORY
RealFns USING [SqRt,Cos,Sin,ArcTan,Exp,Ln],
Vector USING [Vec];
Complex: DEFINITIONS IMPORTS RealFns, Vector =
BEGIN
Vec: TYPE = Vector.Vec;
Vector operations
Add: PROCEDURE [a: Vec, b: Vec] RETURNS [Vec] = INLINE
{RETURN[[a.x+b.x,a.y+b.y]]}; -- same as vector sum
Sub: PROCEDURE [a: Vec, b: Vec] RETURNS [Vec] = INLINE
{RETURN[[a.x-b.x,a.y-b.y]]}; -- same as vector difference
Neg: PROCEDURE [a: Vec] RETURNS [Vec] = INLINE
{RETURN[[-a.x,-a.y]]}; -- same as vector complement
Mul: PROCEDURE [a: Vec, b: Vec] RETURNS [Vec] = INLINE
{RETURN[[(a.x*b.x - a.y*b.y), (a.x*b.y + a.y*b.x)]]}; -- complex product
Div: PROCEDURE [a: Vec, b: Vec] RETURNS [Vec] = INLINE
BEGIN d:REAL = b.x*b.x + b.y*b.y;
RETURN[[(a.x*b.x + a.y*b.y)/d, (-a.x*b.y + a.y*b.x)/d]] -- complex quotient
END;
Conjugate: PROCEDURE [a: Vec] RETURNS [Vec] = INLINE
{RETURN[[a.x,-a.y]]}; -- complex conjugate
AlmostEqual: PROCEDURE [a: Vec, b: Vec, mag:[-126..0] ← -20] RETURNS [BOOLEAN];
FromPolar: PROCEDURE [r: REAL, radians: REAL] RETURNS [Vec] = INLINE
{RETURN[[r*RealFns.Cos[radians], r*RealFns.Sin[radians]]]};
Abs: PROCEDURE [a: Vec] RETURNS [REAL] = INLINE
{RETURN[RealFns.SqRt[a.x*a.x+a.y*a.y]]}; -- same as Vector.Mag
SqrAbs: PROCEDURE [a: Vec] RETURNS [REAL] = INLINE
{RETURN[a.x*a.x+a.y*a.y]}; -- good for checking tolerance
Arg: PROCEDURE [a: Vec] RETURNS [REAL] = INLINE
{RETURN[RealFns.ArcTan[a.y,a.x]]};
returns the angle from the x axis to a, in radians.
Exp: PROCEDURE [a: Vec] RETURNS [Vec] = INLINE
{RETURN[FromPolar[RealFns.Exp[a.x], a.y]]};
complex exponential function
Ln: PROCEDURE [a: Vec] RETURNS [Vec] = INLINE
{RETURN[[RealFns.Ln[Abs[a]],Arg[a]]]};
complex natural logarithm
Sqr: PROCEDURE [a: Vec] RETURNS [Vec] = INLINE
{RETURN[[(a.x*a.x - a.y*a.y), (2*a.x*a.y)]]}; -- like Mul[a,a]
SqRt: PROCEDURE [a: Vec] RETURNS [Vec]; -- complex square root
END.