Title: Implementing a complex number object
Question: This article describes how to implement a complex number object to be used in vector related operations
Answer:
This article shows how to use OOP for mathematical calculations
A complex number can be visualized as a vector and has 4 basic properties. In the Complex number data type these properties and various procedures and functions will be implemented.
The properties of a complex number are
1) Real part
2) Imaginary part
3) Radius (vector length)
4) alfa (angle measured counterclock wise with the real axis
The methods implemented will be
1) Data input using x,y coordinates
2) Data input using vector anotation
3) information functions
4) operations like addition, subtraction, multiplication and division
5) scalar operation
The data type will be written in a unit called COMPLEX
UNIT COMPLEX;
INTERFACE
Type CN = Class
Re,Img,r,alfa :extended; //properties
procedure update; //internal function for conversion
procedure re_img(x,y :extended);
procedure r_angle (radius, ang:extended);
procedure operate(ch: char; B:CN ;Var Result : CN);
procedure scalar(x:extended);
function getReal: extended;
function getImg: extended;
function getRadius: extended;
function getAngle: extended;
end;
IMPLEMENTATION
{ this procedure is for converting from the x-y system to vector annotation}
procedure CN.update;
Begin
R:= sqrt(sqr(Re) + sqr(Img));
if Re = 0 then
begin
if Img 0 then alfa:= pi/2
else if Img = 0 then alfa:= 0
else alfa:= 3*pi/2;
end
else
begin
alfa:= ArcTan(Img/Re);
if Re end;
End;
// x - y input method
procedure CN.Re_img(x,y :extended);
Begin
Re:= x; Img:=y;
update; // calculating angle and radius
End;
// vector input (angle is in radians)
procedure CN.r_angle (radius, ang:extended);
Begin
r:= abs(radius); alfa:=ang;
Re:= r * cos(alfa); Img:= r * sin(alfa);
End;
// operations involving 2 complex numbers
procedure CN.operate(ch: char; B:CN ;Var Result : CN);
Begin
Case ch of
'+' : begin
result.Re := Re + B.Re;
result.Img := Img + B.Img;
result.Update;
end;
'-' : begin
result.Re := Re - B.Re;
result.Img := Img - B.Img;
result.Update;
end;
'*' : begin
result.Re := Re*B.Re - Img*B.Img;
result.Img := Img*B.Re + Re*B.Img;
result.Update;
end;
'/' :
begin
result.Re := (Re*B.Re + Img*B.Img) / (sqr(B.Re) + sqr(B.Img));
result.Img := (Img*B.Re - Re*B.Img) / (sqr(B.Re) + sqr(B.Img));
result.Update;
end
end;
End;
//multiplication by a scalar
procedure CN.scalar(x:extended);
Begin
Re:= x * Re; Img := x * Img; update;
End;
//functions for information
function CN.getReal: extended;
Begin
getReal:= Re;
End;
function CN.getImg: extended;
Begin
getImg:= Img;
End;
function CN.getRadius: extended;
Begin
getRadius:= R;
End;
function CN.getAngle: extended;
Begin
getAngle:= Alfa;
End;
END.
--------------------------------------------------------------
USAGE:
defining a complex number like 8.5
A: CN;
A.r_angle(8.5, 60 *Pi/180);
don't forget to make the conversion to radians with *pi/180
Using x, y coordinates:
A.Re_Img(5,5)
Addition:
X A.operate('+',B,X);
Division
X A.Operate('/',B,X)
With this unit you can use complex algebra in your delphi project