-
Notifications
You must be signed in to change notification settings - Fork 1
/
complexmath.pas
275 lines (230 loc) · 6.17 KB
/
complexmath.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
{ Classes to store and calculate with complex numbers }
unit ComplexMath;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
type
TComplexNumber = class;
{ TComplexNumberHandler }
{ TComplexAbstract }
{ Class template (base class) for complex numbers and complex operations in
this library }
TComplexAbstract = class(TPersistent)
private
FInstanceName: string;
function GetAbs: Extended; virtual; abstract;
function GetArg: Extended; virtual; abstract;
function GetIm: Extended; virtual; abstract;
function GetRe: Extended; virtual; abstract;
procedure SetAbs(AValue: Extended); virtual; abstract;
procedure SetArg(AValue: Extended); virtual; abstract;
procedure SetIm(AValue: Extended); virtual; abstract;
procedure SetRe(AValue: Extended); virtual; abstract;
procedure SetPolar(AnAbs, AnArg: Extended); virtual; abstract;
public
function Reciprocal(var ResultPointer: TComplexNumber): TComplexNumber; virtual;
property Re: Extended read GetRe write SetRe;
property Im: Extended read GetIm write SetIm;
property Abs: Extended read GetAbs write SetAbs;
property Arg: Extended read GetArg write SetArg;
property InstanceName: string read FInstanceName write FInstanceName; {use
it for a unique Id to find the instance during debugging}
end;
TComplexNumber = class(TComplexAbstract)
private
FRe, FIm: Extended;
function GetRe: Extended; override;
procedure SetRe(AValue: Extended); override;
function GetIm: Extended; override;
procedure SetIm(AValue: Extended); override;
function GetAbs: Extended; override;
procedure SetAbs(AValue: Extended); override;
function GetArg: Extended; override;
procedure SetArg(AValue: Extended); override;
procedure SetPolar(AnAbs, AnArg: Extended); override;
public
end;
{ TComplexOperation }
{ template for classes that operate on a collection of complex numbers
providing the result of the operation }
TComplexOperation = class(TComplexNumber)
private
FOperandList: TList;
function GetOperandList: TList;
function GetOperandCount: Integer;
function GetOperands(AnIndex: Integer): TComplexAbstract;
function GetRe: Extended; override;
function GetIm: Extended; override;
procedure Operate; virtual; abstract;
procedure SetOperands(AnIndex: Integer; AValue: TComplexAbstract);
property OperandList: TList read GetOperandList;
public
destructor Destroy; override;
procedure AddOperand(AnOperand: TComplexAbstract);
procedure RemoveOperands;
property OperandCount: Integer read GetOperandCount;
property Operands[AnIndex: Integer]: TComplexAbstract read GetOperands write SetOperands;
end;
{ TComplexSum }
{ sum of complex numbers }
TComplexSum = class(TComplexOperation)
public
procedure Operate; override;
end;
{ TComplexProduct }
{ product of complex numbers }
TComplexProduct = class(TComplexOperation)
private
procedure Operate; override;
public
end;
{ TComplexQuotient }
{ quotient of complex numbers }
TComplexQuotient = class(TComplexOperation)
public
procedure Operate; override;
end;
implementation
uses Math;
{ TComplexSum }
procedure TComplexSum.Operate;
var
i: Integer;
begin
FRe := 0; FIm := 0;
for i := 0 to OperandCount - 1 do begin
FRe := FRe + Operands[i].Re;
FIm := FIm + Operands[i].Im
end;
end;
{ TComplexQuotient }
procedure TComplexQuotient.Operate;
var
x, y: Extended;
i: Integer;
begin
x := Operands[0].Abs;
y := Operands[0].Arg;
for i := 1 to OperandCount - 1 do begin
x := x / Operands[i].Abs;
y := y - Operands[i].Arg;
end;
SetPolar(x, y)
end;
{ TComplexNumber }
function TComplexNumber.GetRe: Extended;
begin
Result := FRe
end;
procedure TComplexNumber.SetRe(AValue: Extended);
begin
FRe := AValue
end;
function TComplexNumber.GetIm: Extended;
begin
Result := FIm
end;
procedure TComplexNumber.SetIm(AValue: Extended);
begin
FIm := AValue
end;
function TComplexNumber.GetAbs: Extended;
begin
Result := Sqrt(Sqr(Re) + Sqr(Im))
end;
procedure TComplexNumber.SetAbs(AValue: Extended);
var
a: Extended;
begin
a := Arg;
Re := AValue * Cos(a);
Im := AValue * Sin(a)
end;
function TComplexNumber.GetArg: Extended;
begin
Result := arctan2(Im, Re);
end;
procedure TComplexNumber.SetArg(AValue: Extended);
var
a: Extended;
begin
a := Abs;
Re := a * cos(AValue);
Im := a * sin(AValue);
end;
procedure TComplexNumber.SetPolar(AnAbs, AnArg: Extended);
begin
Re := AnAbs * cos(AnArg);
Im := AnAbs * sin(AnArg)
end;
{ TComplexProduct }
procedure TComplexProduct.Operate;
var
x, y: Extended;
i: Integer;
begin
x := 1; y := 0;
for i := 0 to OperandCount -1 do begin
x := x * Operands[i].Abs;
y := y + Operands[i].Arg;
end;
SetPolar(x, y);
end;
{ TComplexOperation }
function TComplexOperation.GetOperandList: TList;
begin
if not Assigned(FOperandList) then FOperandList := TList.Create;
Result := FOperandList
end;
function TComplexOperation.GetOperandCount: Integer;
begin
if Assigned(FOperandList) then Result := FOperandList.Count
else Result := 0;
end;
function TComplexOperation.GetOperands(AnIndex: Integer): TComplexAbstract;
begin
Pointer(Result) := OperandList[AnIndex]
end;
function TComplexOperation.GetRe: Extended;
begin
Operate;
Result := inherited GetRe;
end;
function TComplexOperation.GetIm: Extended;
begin
Operate;
Result := inherited GetIm
end;
procedure TComplexOperation.SetOperands(AnIndex: Integer;
AValue: TComplexAbstract);
begin
OperandList[AnIndex] := AValue
end;
destructor TComplexOperation.Destroy;
var
i: Integer;
begin
FOperandList.Free;
inherited Destroy;
end;
procedure TComplexOperation.AddOperand(AnOperand: TComplexAbstract);
begin
OperandList.Add(AnOperand);
end;
procedure TComplexOperation.RemoveOperands;
begin
if not Assigned(FOperandList) then Exit;
if FOperandList.Count = 0 then Exit;
FOperandList.Clear;
end;
{ TComplexAbstract }
function TComplexAbstract.Reciprocal(var ResultPointer: TComplexNumber
): TComplexNumber;
begin
if not Assigned(ResultPointer) then ResultPointer := TComplexNumber.Create;
ResultPointer.Abs := 1 / Abs;
ResultPointer.Arg := -Arg;
Result := ResultPointer;
end;
end.