
{COMPLEX ARITHMETIC LIBRARY FOR PASCAL; (C) 1981 BY FICOMP, INC. FAIRFAX, VA}

MODULE CMPXLIB;

TYPE 
     COMPLEX = RECORD
		 RE,IM: REAL
     END;

TYPE 
     PHASOR = RECORD
		MAG,ANG: REAL
     END;

PROCEDURE CMPX(N1:COMPLEX;OP:CHAR;N2:COMPLEX;VAR CRES:COMPLEX);
{TO ADD, SUBTRACT, MULTIPLY, OR DIVIDE COMPLEX NUMBERS}
VAR TERM: REAL;
BEGIN
  IF OP = '+' THEN
    BEGIN {COMPLEX SUM OF 2 COMPLEX NUMBERS, N1+N2}
      CRES.RE := N1.RE+N2.RE;
      CRES.IM := N1.IM+N2.IM;
    END
  ELSE
    IF OP = '-' THEN
      BEGIN {COMPLEX DIFFERENCE OF 2 COMPLEX NUMBERS, N1-N2}
	CRES.RE := N1.RE-N2.RE;
	CRES.IM := N1.IM-N2.IM;
      END
  ELSE
    IF OP = '*' THEN
      BEGIN {COMPLEX PRODUCT OF 2 COMPLEX NUMBERS, N1*N2}
	CRES.RE := N1.RE*N2.RE-N1.IM*N2.IM;
	CRES.IM := N1.RE*N2.IM+N1.IM*N2.RE;
      END
  ELSE
    IF OP = '/' THEN
      BEGIN {COMPLEX QUOTIENT OF 2 COMPLEX NUMBERS, N1/N2}
	TERM := SQR(N2.RE)+SQR(N2.IM);
	CRES.RE := (N1.RE*N2.RE+N1.IM*N2.IM)/TERM;
	CRES.IM := (N2.RE*N1.IM-N1.RE*N2.IM)/TERM;
      END
  ELSE
    WRITELN(OP,' IS AN ILLEGAL COMPLEX OPERATOR');
END;

PROCEDURE POLAR(N:COMPLEX;VAR VECT:PHASOR);
{TO CONVERT COMPLEX NUMBER FROM CARTESIAN TO POLAR COORDINATES}
BEGIN
  VECT.MAG := SQRT(SQR(N.RE)+SQR(N.IM));
  VECT.ANG := ARCTAN(N.IM/N.RE);
END;

PROCEDURE CART(N:PHASOR;VAR XY:COMPLEX);
{TO CONVERT COMPLEX NUMBER FROM POLAR TO CARTESIAN COORDINATES}
BEGIN
  XY.RE := N.MAG*COS(N.ANG);
  XY.IM := N.MAG*SIN(N.ANG);
END;

PROCEDURE CSQR(N:COMPLEX;VAR CSQ:COMPLEX);
{SQUARE OF COMPLEX NUMBER, SQR(N)}
BEGIN
  CSQ.RE := SQR(N.RE)-SQR(N.IM);
  CSQ.IM := 2*N.RE*N.IM;
END;

PROCEDURE CSQRT(N:COMPLEX;VAR CSQT:COMPLEX);
{SQUARE ROOT OF COMPLEX NUMBER, SQRT(N)}
BEGIN
  CSQT.RE := SQRT((N.RE+SQRT(SQR(N.RE)+SQR(N.IM)))/2);
  CSQT.IM := N.IM/(2*CSQT.RE);
END;

PROCEDURE CONJG(N:COMPLEX;VAR CONJ:COMPLEX);
{CONJUGATE OF A COMPLEX NUMBER}
BEGIN
  CONJ.RE := N.RE;
  CONJ.IM := -N.IM;
END;

FUNCTION CABS(N:COMPLEX): REAL;
{ABSOLUTE VALUE OF COMPLEX NUMBER, ABS(N)}
BEGIN
  CABS := SQRT(SQR(N.RE)+SQR(N.IM));
END;

MODEND.
S(N:COMPLEX): REAL;
{ABSOLUTE VALUE OF