FunctionImagingInPascal/ucomplex.pas
2020-04-26 16:08:03 +03:00

72 lines
1.1 KiB
ObjectPascal

unit ucomplex;
interface
type complex = object
r: extended;
i: extended;
function length(): extended;
function squared(): complex;
function mult(a: complex): complex;
function power(n: integer): complex;
procedure update(re: extended; im: extended);
end;
function complex_sum(a: complex; b: complex): complex;
implementation
procedure complex.update(re: extended; im: extended);
begin
r := re;
i := im;
end;
function complex.length(): extended;
begin
length := sqrt(r * r + i * i);
end;
function complex.squared(): complex;
var
res: complex;
begin
res.r := sqr(r) - sqr(i);
res.i := 2 * r * i;
squared := res;
end;
function complex.mult(a: complex): complex;
var
res: complex;
begin
res.r := r * a.r - i * a.i;
res.i := r * a.i + i * a.r;
mult := res;
end;
function complex.power(n: integer): complex;
var
j: integer;
c, res: complex;
begin
c.update(r, i);
res.update(r, i);
for j := 1 to n - 1 do begin
res := res.mult(c);
end;
power := res;
end;
function complex_sum(a: complex; b: complex): complex;
var
res: complex;
begin
res.r := a.r + b.r;
res.i := a.i + b.i;
complex_sum := res;
end;
begin
end.