-
Notifications
You must be signed in to change notification settings - Fork 3
/
generic_cubic_spline.adb
386 lines (371 loc) · 13.5 KB
/
generic_cubic_spline.adb
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
-- --
-- package Generic_Cubic_Spline Copyright (c) Dmitry A. Kazakov --
-- Implementation Luebeck --
-- Spring, 2012 --
-- --
-- Last revision : 23:15 18 Feb 2015 --
-- --
-- This library is free software; you can redistribute it and/or --
-- modify it under the terms of the GNU General Public License as --
-- published by the Free Software Foundation; either version 2 of --
-- the License, or (at your option) any later version. This library --
-- is distributed in the hope that it will be useful, but WITHOUT --
-- ANY WARRANTY; without even the implied warranty of --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU --
-- General Public License for more details. You should have --
-- received a copy of the GNU General Public License along with --
-- this library; if not, write to the Free Software Foundation, --
-- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. --
-- --
-- As a special exception, if other files instantiate generics from --
-- this unit, or you link this unit with other files to produce an --
-- executable, this unit does not by itself cause the resulting --
-- executable to be covered by the GNU General Public License. This --
-- exception does not however invalidate any other reasons why the --
-- executable file might be covered by the GNU Public License. --
--____________________________________________________________________--
with Ada.Exceptions; use Ada.Exceptions;
with Ada.IO_Exceptions; use Ada.IO_Exceptions;
package body Generic_Cubic_Spline is
function Acceleration
( Offset : Number;
Coefficients : Spline_Coefficients
) return Number is
begin
return 6.0 * Coefficients.D * Offset + 2.0 * Coefficients.C;
end Acceleration;
function Acceleration (Spline : Cubic_Spline; Argument : Number)
return Number is
Intervals : Map renames Spline.Intervals;
Index : Integer := Find (Intervals, Argument);
begin
if Index > 0 then -- Exact hit
return Get (Intervals, Index).C;
else
Index := - Index - 1;
if Index = 0 then -- Left of the interval
return 0.0;
elsif Index >= Get_Size (Intervals) then
return 0.0;
else
return Acceleration
( Argument - Get_Key (Intervals, Index),
Get (Intervals, Index)
);
end if;
end if;
end Acceleration;
function Get
( Spline : Cubic_Spline;
Index : Positive
) return Pair is
begin
return
( X => Get_Key (Spline.Intervals, Index),
Y => Get (Spline.Intervals, Index).A
);
end Get;
function Get_Size (Spline : Cubic_Spline) return Natural is
begin
return Get_Size (Spline.Intervals);
end Get_Size;
procedure Solve (Data : in out Map) is
procedure Set (Index : Positive; B, C, D : Number) is
begin
Replace (Data, Index, (Get (Data, Index).A, B, C, D));
end Set;
N : constant Natural := Get_Size (Data);
DX, D : array (1..N - 1) of Number;
A : array (2..N - 1) of Number;
C : array (1..N) of Number;
begin
--
-- Calculate interval differences of the arguments and values
--
for I in DX'Range loop
DX (I) := Get_Key (Data, I + 1) - Get_Key (Data, I);
D (I) := (Get (Data, I + 1).A - Get (Data, I).A) / DX (I);
end loop;
--
-- The system: to solve:
--
-- [Aij] [Si] = [Ci] Ci = 3 dYi/dXi - dYi-1/dXi-1
--
-- | 2(dX1+dX2) dX2 0 ... 0 0 |
-- | dX2 2(dX2+dX3) dX3 ... 0 0 |
-- | 0 dX3 2(dX3+dX4) ... 0 0 |
-- | ... ... ... ... ... ... |
-- | 0 0 0 ... dXN-2 2(dXN-2+dXN-1) |
--
A (2) := 2.0 * (DX (2) + DX (1)); -- Diagonal element
C (2) := 3.0 * ( D (2) - D (1)); -- Right side
for I in 2..N - 2 loop -- Excluding elements under the diagonal
declare
F : constant Number := DX (I) / A (I);
begin
A (I + 1) := 2.0 * (DX (I + 1) + DX (I)) - DX (I) * F;
C (I + 1) := 3.0 * ( D (I + 1) - D (I)) - C (I) * F;
end;
end loop;
-- declare
-- function Image (X : Number) return String is
-- type Fixed is delta 0.01 digits 9;
-- begin
-- return Fixed'Image (Fixed (X));
-- end Image;
-- function Image (X : Natural) return String is
-- Result : String := Integer'Image (X);
-- begin
-- return Result (Result'First + 1..Result'Last);
-- end Image;
-- begin
-- for I in A'Range loop
-- Ada.Text_IO.Put_Line
-- ( "A" & Image (I) & Image (I) & " =" & Image (A (I))
-- & " B" & Image (I) & " =" & Image (C (I))
-- );
-- end loop;
-- end;
--
-- Now the matrix is this:
--
-- | A(2) dX2 0 ... 0 0 | | C(2) |
-- | 0 A(3) dX3 ... 0 0 | | C(3) |
-- | 0 0 A(4) ... 0 0 | | C(4) |
-- | ... ... ... ... ... ... | | ... |
-- | 0 0 0 ... 0 A(XN-1) | | C(N-1) |
--
-- Excluding elements above the diagonal
--
C (N - 1) := C (N - 1) / A (N - 1);
for I in reverse 2..N - 2 loop
C (I) := (C (I) - DX (I) * C (I + 1)) / A (I);
end loop;
C (1) := 0.0;
C (N) := 0.0;
-- declare
-- function Image (X : Number) return String is
-- type Fixed is delta 0.01 digits 9;
-- begin
-- return Fixed'Image (Fixed (X));
-- end Image;
-- function Image (X : Natural) return String is
-- Result : String := Integer'Image (X);
-- begin
-- return Result (Result'First + 1..Result'Last);
-- end Image;
-- begin
-- for I in 1..N loop
-- Ada.Text_IO.Put_Line
-- ( "C" & Image (I) & " =" & Image (C (I))
-- );
-- end loop;
-- end;
--
-- Setting spline coefficients as:
--
-- Bi = dYi/dXi - dXi (2 Ci + Ci+1) / 3
-- Ci = Bi
-- Di = (Ci+1 - Ci) / 3 dXi
--
for I in 1..N - 1 loop
Set
( I,
D (I) - DX (I) * (2.0 * C (I) + C (I + 1)) / 3.0,
C (I),
(C (I + 1) - C (I)) / (3.0 * DX (I))
);
end loop;
Set
( N,
Velocity (DX (N - 1), Get (Data, N - 1)),
0.0,
0.0
);
-- declare
-- function Image (X : Number) return String is
-- type Fixed is delta 0.01 digits 9;
-- begin
-- return Fixed'Image (Fixed (X));
-- end Image;
-- function Image (X : Natural) return String is
-- Result : String := Integer'Image (X);
-- begin
-- return Result (Result'First + 1..Result'Last);
-- end Image;
-- begin
-- for I in 1..N - 1 loop
-- declare
-- This : Spline_Coefficients renames Get (Data, I);
-- begin
-- Ada.Text_IO.Put_Line
-- ( "X" & Image (I) & " =" & Image (Get_Key (Data, I))
-- & " dX" & Image (I) & " =" & Image (DX (I))
-- & " A" & Image (I) & " =" & Image (This.A)
-- & " B" & Image (I) & " =" & Image (This.B)
-- & " C" & Image (I) & " =" & Image (This.C)
-- & " D" & Image (I) & " =" & Image (This.D)
-- & " Y(R)=" & Image (Value (DX (I), This))
-- & " Y'(R)=" & Image (Velocity (DX (I), This))
-- & " Y""(R)=" & Image (Acceleration (DX (I), This))
-- );
-- end;
-- end loop;
-- end;
end Solve;
procedure Set
( Spline : in out Cubic_Spline;
Points : in out Map
) is
begin
case Get_Size (Points) is
when 0 | 1 =>
null;
when 2 =>
declare
Left : Spline_Coefficients := Get (Points, 1);
Right : Spline_Coefficients := Get (Points, 2);
begin
Left.B :=
( (Right.A - Left.A)
/ (Get_Key (Points, 2) - Get_Key (Points, 1))
);
Right.B := Left.B;
Replace (Points, 1, Left);
Replace (Points, 2, Right);
end;
when others =>
Solve (Points);
end case;
Spline.Intervals := Points;
end Set;
procedure Set
( Spline : in out Cubic_Spline;
Pairs : Pairs_Array
) is
Data : Map;
begin
for Index in Pairs'Range loop
begin
Add
( Data,
Pairs (Index).X,
(Pairs (Index).Y, others => 0.0)
);
exception
when Constraint_Error =>
if Pairs (Index).Y /= Get (Data, Pairs (Index).X).A then
Raise_Exception
( Constraint_Error'Identity,
"Conflicting (x,y)-pairs in the list"
);
end if;
end;
end loop;
Set (Spline, Data);
end Set;
procedure Set_From_Container
( Spline : in out Cubic_Spline;
Pairs : Abstract_Pairs_Container'Class
) is
Data : Map;
This : Pair;
begin
for Index in 1..Get_Size (Pairs) loop
This := Get (Pairs, Index);
begin
Add (Data, This.X, (This.Y, others => 0.0));
exception
when Constraint_Error =>
if This.Y /= Get (Data, This.X).A then
Raise_Exception
( Constraint_Error'Identity,
"Conflicting (x,y)-pairs in the list"
);
end if;
end;
end loop;
Set (Spline, Data);
end Set_From_Container;
function Value
( Offset : Number;
Coefficients : Spline_Coefficients
) return Number is
begin
return
( ( ( Coefficients.D * Offset
+ Coefficients.C
)
* Offset
+ Coefficients.B
)
* Offset
+ Coefficients.A
);
end Value;
function Value (Spline : Cubic_Spline; Argument : Number)
return Number is
Intervals : Map renames Spline.Intervals;
Index : Integer := Find (Intervals, Argument);
begin
if Index > 0 then -- Exact hit
return Get (Intervals, Index).A;
else
Index := - Index - 1;
if Index = 0 then
declare
This : Spline_Coefficients renames Get (Intervals, 1);
begin
return
This.A + This.B * (Argument - Get_Key (Intervals, 1));
end;
elsif Index >= Get_Size (Spline.Intervals) then
declare
Size : constant Natural := Get_Size (Intervals);
This : Spline_Coefficients renames Get (Intervals, Size);
begin
return This.A +
This.B * (Argument - Get_Key (Intervals, Size));
end;
end if;
return Value
( Argument - Get_Key (Intervals, Index),
Get (Intervals, Index)
);
end if;
end Value;
function Velocity
( Offset : Number;
Coefficients : Spline_Coefficients
) return Number is
begin
return
( ( 3.0 * Coefficients.D * Offset
+ 2.0 * Coefficients.C
)
* Offset
+ Coefficients.B
);
end Velocity;
function Velocity (Spline : Cubic_Spline; Argument : Number)
return Number is
Intervals : Map renames Spline.Intervals;
Index : Integer := Find (Intervals, Argument);
begin
if Index > 0 then -- Exact hit
return Get (Intervals, Index).B;
else
Index := - Index - 1;
if Index = 0 then -- Left of the interval
return Get (Intervals, 1).B;
elsif Index >= Get_Size (Intervals) then
return Get (Intervals, Get_Size (Intervals)).B;
else
return Velocity
( Argument - Get_Key (Intervals, Index),
Get (Intervals, Index)
);
end if;
end if;
end Velocity;
end Generic_Cubic_Spline;