DIRECTORY Ieee, Real, RealFns, Vector, Complex; ComplexImpl: CEDAR PROGRAM IMPORTS RealFns, Vector, Complex EXPORTS Complex = BEGIN Vec: TYPE = Vector.Vec; Mul: PUBLIC PROCEDURE [a: Vec, b: Vec] RETURNS [Vec] = {RETURN[[(a.x*b.x - a.y*b.y), (a.x*b.y + a.y*b.x)]]}; -- complex product Div: PUBLIC PROCEDURE [a: Vec, b: Vec] RETURNS [Vec] = 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; FromPolar: PUBLIC PROCEDURE [r: REAL, radians: REAL] RETURNS [Vec] = {RETURN[[r*RealFns.Cos[radians], r*RealFns.Sin[radians]]]}; Exponent: PROCEDURE [x: REAL] RETURNS [INTEGER] = INLINE BEGIN fl: Ieee.SingleReal _ LOOPHOLE[x]; RETURN[fl.exp]; END; AlmostEqual: PUBLIC PROCEDURE [a: Vec, b: Vec, mag:[-126..0] _ -20] RETURNS [BOOLEAN] = BEGIN sumSqrAbs: REAL _ Complex.SqrAbs[a] + Complex.SqrAbs[b]; sqrAbsDif: REAL _ Complex.SqrAbs[Complex.Sub[a, b]]; RETURN [Exponent[sumSqrAbs]+mag+mag-1>Exponent[sqrAbsDif]]; END; Arg: PUBLIC PROCEDURE [a: Vec] RETURNS [REAL] = {RETURN[RealFns.ArcTan[a.y, a.x]]}; Exp: PUBLIC PROCEDURE [a: Vec] RETURNS [Vec] = {RETURN[FromPolar[RealFns.Exp[a.x], a.y]]}; Ln: PUBLIC PROCEDURE [a: Vec] RETURNS [Vec] = {RETURN[[RealFns.Ln[Complex.Abs[a]], Arg[a]]]}; Sqr: PUBLIC PROCEDURE [a: Vec] RETURNS [Vec] = {RETURN[[(a.x*a.x - a.y*a.y), (2*a.x*a.y)]]}; SqRt: PUBLIC PROCEDURE [a: Vec] RETURNS [Vec] = BEGIN -- Find the complex square root, using Newton's iteration z:Vec _ (IF a.y>=0 THEN [1, 1] ELSE [1, -1]); oldz:Vec; IF a.y = 0 THEN BEGIN IF a.x>0 THEN z.y_0 -- real square root ELSE IF a.x<0 THEN z.x_0 -- pure imaginary ELSE RETURN[[0, 0]] END; FOR I:NAT IN [0..50) DO oldz _ z; z _ Vector.Mul[Complex.Add[z, Div[a, z]], 0.5]; IF AlmostEqual[z, oldz, -20] THEN EXIT; ENDLOOP; RETURN[z]; END; END. ComplexImpl.mesa Last edited by Michael Plass, January 4, 1984 12:00 pm Written by Michael Plass, 29-Sep-81 complex exponential function ʘJšœ™Jšœ6™6Jšœ#™#J˜JšÏk œ&˜/Jšœ œœœ ˜Mš˜J˜—šœœ˜J˜—šÏnœœ œœ ˜7Jšœœ/Ïc˜HJ˜—šžœœ œœ˜6Jšœœ˜!Jšœ2Ÿ˜KJšœ˜J˜—š ž œœ œœ œœ˜DJšœœ4˜;J˜—š žœ œœœœ˜8Jš˜Jšœœ˜"Jšœ ˜Jšœ˜J˜—šž œœ œ&˜CJšœœ˜Jš˜Jšœ œ)˜8Jšœ œ%˜4Jšœ5˜;Jšœ˜J˜—š žœœ œ œœ˜/Jšœœ˜#J˜—šžœœ œ œ˜.Jšœœ$˜+Jšœ™J˜—šžœœ œ œ˜-Jšœœ(˜/J˜—šžœœ œ œ˜.Jšœœ&˜-J˜—šžœœ œ œ˜/JšœŸ9˜?Jšœ œœœ ˜-J˜ šœ œ˜Jš˜JšœœŸ˜'JšœœœŸ˜*Jšœœ˜Jšœ˜—š œœœœ ˜J˜ J˜/Jšœœœ˜'Jšœ˜—Jšœ˜ Jšœ˜J˜—Jšœ˜J˜—…— š