! Complex arithmetic in LINGO.
LINGO uses only real numbers.
Thus, if you wish to work with complex numbers,
you must represent each complex number by a pair
of real numbers. If we denote the square root of -1 by i,
we write a complex number Z = XR + i*XI.
For some applications, e.g., electrical, it is
useful to use polar coordinates,
(XR + i*XI) = R*( COS( THETA) + i*SIN( THETA))
= R*EXP(i*THETA), (using Euler's Formula),
where R = (XR*XR + XI*XI)^0.5, and THETA = ATAN(XI/XR) =
angle in radians whose tangent is XI/XR.
Below are code fragments for how to compute
the real and imaginary parts resulting from
various math operators;
! Keywords: Complex number, Imaginary number;
SETS:
VECTOR: XR, XI, YR, YI, ZR, ZI;
ENDSETS DATA:
! Some arbitrary data;
XR = 3 6 -12 .2 4 1 1.1668506228; ! Real parts of X vector;
XI = 4 0 6 .3 5 4 0.3609491955; ! Imaginary parts of X vector;
YR = 2 -2 -2 -2 3 3 0; ! Real parts of Y vector;
YI = 5 1 1 3 4 2 0; ! Imaginary parts of Y vector;
ENDDATA
! If we want to allow the Z to be negative;
@FOR( VECTOR(j):
@FREE(ZR(j));
@FREE(ZI(j));
);
! Some arbitrary code to illustrate computing the real and
imaginary parts of Z = F( X, Y)...;
! Complex addition: Z(j) = X(j) + Y(j) for j = 1;
@FOR( VECTOR(j) | j #EQ# 1:
ZR(j) = XR(j) + YR(j); ! Real part;
ZI(j) = XI(j) + YI(j); ! Imaginary part;
);
! Complex multiplication: Z(j) = X(j) * Y(j) for j = 2;
@FOR( VECTOR(j) | j #EQ# 2:
ZR(j) = XR(j) * YR(j) - XI(j)*YI(j); ! Real part;
ZI(j) = XR(j) * YI(j) + YR(j)*XI(j); ! Imaginary part;
);
! Complex division: Z(j) = X(j) / Y(j) for j = 3;
@FOR( VECTOR(j) | j #EQ# 3:
ZR(j) = (XR(j) * YR(j) + XI(j) * YI(j)) / (YR(j)^2 + YI(j)^2); ! Real part;
ZI(j) = (XI(j) * YR(j) - XR(j) * YI(j)) / (YR(j)^2 + YI(j)^2); ! Imaginary part;
);
! Exponentiation: Z(j) = @EXP(X(j)), for j = 4;
@FOR( VECTOR(j) | j #EQ# 4:
ZR(j) = @EXP(XR(j))*@COS(XI(j)); ! Real part;
ZI(j) = @EXP(XR(j))*@SIN(XI(j)); ! Imaginary part;
);
! Absolute value: Z(j) = |X(j)| (absolute value) for j = 5;
@FOR( VECTOR(j) | j #EQ# 5:
ZR(j) = (XR(j)^2 + XI(j)^2)^0.5; ! Real part;
ZI(j) = 0; ! Imaginary part;
);
! Square root: Z(j) = positive square root of X(j) for j = 6;
@FOR( VECTOR(j) | j #EQ# 6:
ZR(j) = ((XR(j) + (XR(j)^2 + XI(j)^2)^0.5)/2)^0.5; ! Real part;
ZI(j) = @SIGN(XI(j))*((-XR(j) + (XR(j)^2 + XI(j)^2)^0.5)/2)^0.5; ! Imaginary part;
);
! Logarithm: Z(j) = @LOG(X(j)), for j = 7;
@FOR( VECTOR(j) | j #EQ# 7:
ZR(j) = @LOG( ( XR(j)*XR(j) + XI(j)*XI(j))^0.5);! Real part;
ZI(j) = @ATAN( XI(j)/XR(j)); ! Imaginary part;
);
|