-
Notifications
You must be signed in to change notification settings - Fork 5
/
Polynom.pas
238 lines (212 loc) · 6.7 KB
/
Polynom.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
unit Polynom;
////////////////////////////////////////////////////////////////////////////////
//
// Author: Jaap Baak
// https://github.com/transportmodelling/Utils
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
interface
////////////////////////////////////////////////////////////////////////////////
Uses
Types,ArrayBld;
Type
TPolynomial = record
private
FDegree: Integer;
FCoefs: TDoubleDynArray;
Procedure Allocate; overload;
Procedure Allocate(Const MaxDegree: Integer); overload;
Procedure SetDegree(MaxDegree: Integer);
Function GetCoefs(Power: Integer): Float64;
Function GetValue(x: Float64): Float64; inline;
public
Class Operator Implicit(const Constant: Float64): TPolynomial;
Class Operator Implicit(const Coefs: array of Float64): TPolynomial;
Class Operator Equal(a: TPolynomial; b: TPolynomial): Boolean;
Class Operator Add(a: Float64; b: TPolynomial): TPolynomial;
Class Operator Add(a: TPolynomial; b: TPolynomial): TPolynomial;
Class Operator Multiply(a: Float64; b: TPolynomial): TPolynomial;
Class Operator Multiply(a: TPolynomial; b: TPolynomial): TPolynomial;
public
Constructor Create(const Constant: Float64); overload;
Constructor Create(const Coefs: array of Float64); overload;
Function Null: Boolean;
Procedure Differentiate;
Function Derivative: TPolynomial;
Procedure AntiDifferentiate(Const Constant: Float64 = 0);
Function Primitive(Const Constant: Float64 = 0): TPolynomial;
Function Integrate(Const a,b: Float64): Float64;
public
Property Degree: Integer read FDegree;
Property Coefs[Power: Integer]: Float64 read GetCoefs;
Property Value[x: Float64]: Float64 read GetValue; default;
end;
////////////////////////////////////////////////////////////////////////////////
implementation
////////////////////////////////////////////////////////////////////////////////
Class Operator TPolynomial.Implicit(const Constant: Float64): TPolynomial;
begin
Result.FDegree := 0;
Result.Allocate;
Result.FCoefs[0] := Constant;
end;
Class Operator TPolynomial.Implicit(Const Coefs: array of Float64): TPolynomial;
begin
Result.FCoefs := TFloatArrayBuilder.Create(Coefs);
Result.SetDegree(Length(Coefs)-1);
end;
Class Operator TPolynomial.Equal(a: TPolynomial; b: TPolynomial): Boolean;
begin
if a.FDegree = b.FDegree then
begin
Result := true;
for var Coef := 0 to a.FDegree do
if a.FCoefs[Coef] <> b.FCoefs[Coef] then
begin
Result := false;
Break;
end;
end else
Result := false;
end;
Class Operator TPolynomial.Add(a: Float64; b: TPolynomial): TPolynomial;
begin
Result.FDegree := b.FDegree;
Result.Allocate;
for var Coef := 0 to b.FDegree do Result.FCoefs[Coef] := b.FCoefs[Coef];
Result.FCoefs[0] := Result.FCoefs[0] + a;
end;
Class Operator TPolynomial.Add(a: TPolynomial; b: TPolynomial): TPolynomial;
Var
Coef: Integer;
begin
if a.FDegree = b.FDegree then
begin
Result.Allocate(a.FDegree);
for Coef := 0 to a.FDegree do
Result.FCoefs[Coef] := a.FCoefs[Coef] + b.FCoefs[Coef];
Result.SetDegree(a.FDegree);
end else
if a.FDegree < b.FDegree then
begin
Result.FDegree := b.FDegree;
Result.Allocate;
for Coef := 0 to a.FDegree do Result.FCoefs[Coef] := a.FCoefs[Coef] + b.FCoefs[Coef];
for Coef := a.FDegree+1 to b.FDegree do Result.FCoefs[Coef] := b.FCoefs[Coef];
end else
begin
Result.FDegree := a.FDegree;
Result.Allocate;
for Coef := 0 to b.FDegree do Result.FCoefs[Coef] := a.FCoefs[Coef] + b.FCoefs[Coef];
for Coef := b.FDegree+1 to a.FDegree do Result.FCoefs[Coef] := a.FCoefs[Coef];
end;
end;
Class Operator TPolynomial.Multiply(a: Float64; b: TPolynomial): TPolynomial;
begin
if a = 0 then Result := 0 else
for var Coef := 0 to b.FDegree do Result.FCoefs[Coef] := a*b.FCoefs[Coef];
end;
Class Operator TPolynomial.Multiply(a: TPolynomial; b: TPolynomial): TPolynomial;
begin
if a.Null or b.Null then Result := 0 else
begin
Result.FDegree := a.FDegree+b.FDegree;
Result.Allocate;
for var aCoef := 0 to a.FDegree do
for var bCoef := 0 to b.FDegree do
Result.FCoefs[aCoef+bCoef] := Result.FCoefs[aCoef+bCoef] + a.FCoefs[aCoef]*b.FCoefs[bCoef];
end;
end;
Constructor TPolynomial.Create(const Constant: Float64);
begin
FDegree := 0;
Allocate;
FCoefs[0] := Constant;
end;
Constructor TPolynomial.Create(const Coefs: array of Float64);
begin
FCoefs := TFloatArrayBuilder.Create(Coefs);
SetDegree(Length(Coefs)-1);
end;
Procedure TPolynomial.Allocate;
begin
FCoefs := nil;
SetLength(FCoefs,FDegree+8);
end;
Procedure TPolynomial.Allocate(Const MaxDegree: Integer);
begin
SetLength(FCoefs,MaxDegree+8);
end;
Procedure TPolynomial.SetDegree(MaxDegree: Integer);
begin
FDegree := 0;
for var Power := MaxDegree downto 0 do
if FCoefs[Power] <> 0 then
begin
FDegree := Power;
Break;
end;
end;
Function TPolynomial.GetCoefs(Power: Integer): Float64;
begin
Result := FCoefs[Power];
end;
Function TPolynomial.GetValue(x: Float64): Float64;
begin
Result := FCoefs[FDegree];
for var Power := FDegree-1 downto 0 do
Result := x*Result + FCoefs[Power];
end;
Function TPolynomial.Null: Boolean;
begin
Result := (FDegree = 0) and (FCoefs[0] = 0);
end;
Procedure TPolynomial.Differentiate;
begin
if FDegree = 0 then FCoefs[0] := 0 else
begin
FDegree := FDegree-1;
for var Coef := 0 to FDegree do FCoefs[Coef] := (Coef+1)*FCoefs[Coef+1]
end;
end;
Function TPolynomial.Derivative: TPolynomial;
begin
if FDegree = 0 then Result := 0 else
begin
Result.FDegree := FDegree-1;
Result.Allocate;
for var Coef := 0 to FDegree-1 do Result.FCoefs[Coef] := (Coef+1)*FCoefs[Coef+1]
end;
end;
Procedure TPolynomial.AntiDifferentiate(Const Constant: Float64 = 0);
begin
FDegree := FDegree+1;
if Length(FCoefs) <= FDegree then SetLength(FCoefs,FDegree+8);
for var Coef := FDegree downto 1 do FCoefs[Coef] := FCoefs[Coef-1]/Coef;
FCoefs[0] := Constant;
end;
Function TPolynomial.Primitive(Const Constant: Float64 = 0): TPolynomial;
begin
if Null then Result := Constant else
begin
Result.FDegree := FDegree+1;
Result.Allocate;
for var Coef := FDegree+1 downto 1 do Result.FCoefs[Coef] := FCoefs[Coef-1]/Coef;
Result.FCoefs[0] := Constant;
end;
end;
Function TPolynomial.Integrate(Const a,b: Float64): Float64;
begin
var Coef := FCoefs[FDegree]/(FDegree+1);
var Primitive_a := a*Coef;
var Primitive_b := b*Coef;
for var Power := FDegree-1 downto 0 do
begin
Coef := FCoefs[Power]/(Power+1);
Primitive_a := a*(Primitive_a + Coef);
Primitive_b := b*(Primitive_b + Coef);
end;
Result := Primitive_b - Primitive_a;
end;
end.