-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathinterpolation_caches.jl
1677 lines (1503 loc) · 70.9 KB
/
interpolation_caches.jl
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
LinearInterpolation(u, t; extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation::ExtrapolationType.T = ExtrapolationType.None, extrapolation_right::ExtrapolationType.T = ExtrapolationType.None,
cache_parameters = false)
It is the method of interpolating between the data points using a linear polynomial. For any point, two data points one each side are chosen and connected with a line.
Extrapolation extends the last linear polynomial on each side.
## Arguments
- `u`: data points.
- `t`: time points.
## Keyword Arguments
- `extrapolation`: The extrapolation type applied left and right of the data. Possible options
are `ExtrapolationType.None` (default), `ExtrapolationType.Constant`, `ExtrapolationType.Linear`
`ExtrapolationType.Extension`, `ExtrapolationType.Periodic` and `ExtrapolationType.Reflective`.
- `extrapolation_left`: The extrapolation type applied left of the data. See `extrapolation` for
the possible options. This keyword is ignored if `extrapolation != Extrapolation.none`.
- `extrapolation_right`: The extrapolation type applied right of the data. See `extrapolation` for
the possible options. This keyword is ignored if `extrapolation != Extrapolation.none`.
- `cache_parameters`: precompute parameters at initialization for faster interpolation
computations. Note: if activated, `u` and `t` should not be modified. Defaults to `false`.
- `assume_linear_t`: boolean value to specify a faster index lookup behavior for
evenly-distributed abscissae. Alternatively, a numerical threshold may be specified
for a test based on the normalized standard deviation of the difference with respect
to the straight line (see [`looks_linear`](@ref)). Defaults to 1e-2.
"""
struct LinearInterpolation{uType, tType, IType, pType, T} <: AbstractInterpolation{T}
u::uType
t::tType
I::IType
p::LinearParameterCache{pType}
extrapolation_left::ExtrapolationType.T
extrapolation_right::ExtrapolationType.T
iguesser::Guesser{tType}
cache_parameters::Bool
linear_lookup::Bool
function LinearInterpolation(u, t, I, p, extrapolation_left, extrapolation_right,
cache_parameters, assume_linear_t)
linear_lookup = seems_linear(assume_linear_t, t)
new{typeof(u), typeof(t), typeof(I), typeof(p.slope), eltype(u)}(
u, t, I, p, extrapolation_left, extrapolation_right,
Guesser(t), cache_parameters, linear_lookup)
end
end
function LinearInterpolation(
u, t; extrapolation::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.None, cache_parameters = false, assume_linear_t = 1e-2)
extrapolation_left, extrapolation_right = munge_extrapolation(
extrapolation, extrapolation_left, extrapolation_right)
u, t = munge_data(u, t)
p = LinearParameterCache(u, t, cache_parameters)
A = LinearInterpolation(
u, t, nothing, p, extrapolation_left,
extrapolation_right, cache_parameters, assume_linear_t)
I = cumulative_integral(A, cache_parameters)
LinearInterpolation(
u, t, I, p, extrapolation_left, extrapolation_right,
cache_parameters, assume_linear_t)
end
"""
QuadraticInterpolation(u, t, mode = :Forward; extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation::ExtrapolationType.T = ExtrapolationType.None, extrapolation_right::ExtrapolationType.T = ExtrapolationType.None,
cache_parameters = false)
It is the method of interpolating between the data points using quadratic polynomials. For any point, three data points nearby are taken to fit a quadratic polynomial.
Extrapolation extends the last quadratic polynomial on each side.
## Arguments
- `u`: data points.
- `t`: time points.
- `mode`: `:Forward` or `:Backward`. If `:Forward`, two data points ahead of the point and one data point behind is taken for interpolation. If `:Backward`, two data points behind and one ahead is taken for interpolation.
## Keyword Arguments
- `extrapolation`: The extrapolation type applied left and right of the data. Possible options
are `ExtrapolationType.None` (default), `ExtrapolationType.Constant`, `ExtrapolationType.Linear`
`ExtrapolationType.Extension`, `ExtrapolationType.Periodic` and `ExtrapolationType.Reflective`.
- `extrapolation_left`: The extrapolation type applied left of the data. See `extrapolation` for
the possible options. This keyword is ignored if `extrapolation != Extrapolation.none`.
- `extrapolation_right`: The extrapolation type applied right of the data. See `extrapolation` for
the possible options. This keyword is ignored if `extrapolation != Extrapolation.none`.
- `cache_parameters`: precompute parameters at initialization for faster interpolation computations. Note: if activated, `u` and `t` should not be modified. Defaults to `false`.
- `assume_linear_t`: boolean value to specify a faster index lookup behaviour for
evenly-distributed abscissae. Alternatively, a numerical threshold may be specified
for a test based on the normalized standard deviation of the difference with respect
to the straight line (see [`looks_linear`](@ref)). Defaults to 1e-2.
"""
struct QuadraticInterpolation{uType, tType, IType, pType, T} <:
AbstractInterpolation{T}
u::uType
t::tType
I::IType
p::QuadraticParameterCache{pType}
mode::Symbol
extrapolation_left::ExtrapolationType.T
extrapolation_right::ExtrapolationType.T
iguesser::Guesser{tType}
cache_parameters::Bool
linear_lookup::Bool
function QuadraticInterpolation(
u, t, I, p, mode, extrapolation_left,
extrapolation_right, cache_parameters, assume_linear_t)
mode ∈ (:Forward, :Backward) ||
error("mode should be :Forward or :Backward for QuadraticInterpolation")
linear_lookup = seems_linear(assume_linear_t, t)
new{typeof(u), typeof(t), typeof(I), typeof(p.α), eltype(u)}(
u, t, I, p, mode, extrapolation_left, extrapolation_right,
Guesser(t), cache_parameters, linear_lookup)
end
end
function QuadraticInterpolation(
u, t, mode; extrapolation::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.None, cache_parameters = false, assume_linear_t = 1e-2)
extrapolation_left, extrapolation_right = munge_extrapolation(
extrapolation, extrapolation_left, extrapolation_right)
u, t = munge_data(u, t)
linear_lookup = seems_linear(assume_linear_t, t)
p = QuadraticParameterCache(u, t, cache_parameters, mode)
A = QuadraticInterpolation(
u, t, nothing, p, mode, extrapolation_left,
extrapolation_right, cache_parameters, linear_lookup)
I = cumulative_integral(A, cache_parameters)
QuadraticInterpolation(u, t, I, p, mode, extrapolation_left,
extrapolation_right, cache_parameters, linear_lookup)
end
function QuadraticInterpolation(u, t; kwargs...)
QuadraticInterpolation(u, t, :Forward; kwargs...)
end
"""
LagrangeInterpolation(u, t, n = length(t) - 1; extrapolation::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_left::ExtrapolationType.T = ExtrapolationType.None, extrapolation_right::ExtrapolationType.T = ExtrapolationType.None)
It is the method of interpolation using Lagrange polynomials of (k-1)th order passing through all the data points where k is the number of data points.
## Arguments
- `u`: data points.
- `t`: time points.
- `n`: order of the polynomial. Currently only (k-1)th order where k is the number of data points.
## Keyword Arguments
- `extrapolation`: The extrapolation type applied left and right of the data. Possible options
are `ExtrapolationType.None` (default), `ExtrapolationType.Constant`, `ExtrapolationType.Linear`
`ExtrapolationType.Extension`, `ExtrapolationType.Periodic` and `ExtrapolationType.Reflective`.
- `extrapolation_left`: The extrapolation type applied left of the data. See `extrapolation` for
the possible options. This keyword is ignored if `extrapolation != Extrapolation.none`.
- `extrapolation_right`: The extrapolation type applied right of the data. See `extrapolation` for
the possible options. This keyword is ignored if `extrapolation != Extrapolation.none`.
"""
struct LagrangeInterpolation{uType, tType, T, bcacheType} <:
AbstractInterpolation{T}
u::uType
t::tType
n::Int
bcache::bcacheType
idxs::Vector{Int}
extrapolation_left::ExtrapolationType.T
extrapolation_right::ExtrapolationType.T
iguesser::Guesser{tType}
function LagrangeInterpolation(u, t, n, extrapolation_left, extrapolation_right)
bcache = zeros(eltype(u[1]), n + 1)
idxs = zeros(Int, n + 1)
fill!(bcache, NaN)
new{typeof(u), typeof(t), eltype(u), typeof(bcache)}(u,
t,
n,
bcache,
idxs,
extrapolation_left,
extrapolation_right,
Guesser(t)
)
end
end
function LagrangeInterpolation(
u, t, n = length(t) - 1;
extrapolation::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.None)
extrapolation_left, extrapolation_right = munge_extrapolation(
extrapolation, extrapolation_left, extrapolation_right)
u, t = munge_data(u, t)
if n != length(t) - 1
error("Currently only n=length(t) - 1 is supported")
end
LagrangeInterpolation(u, t, n, extrapolation_left, extrapolation_right)
end
"""
AkimaInterpolation(u, t; extrapolation::ExtrapolationType.T = ExtrapolationType.None, extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.None, cache_parameters = false)
It is a spline interpolation built from cubic polynomials. It forms a continuously differentiable function. For more details, refer: [https://en.wikipedia.org/wiki/Akima_spline](https://en.wikipedia.org/wiki/Akima_spline).
Extrapolation extends the last cubic polynomial on each side.
## Arguments
- `u`: data points.
- `t`: time points.
## Keyword Arguments
- `extrapolation`: The extrapolation type applied left and right of the data. Possible options
are `ExtrapolationType.None` (default), `ExtrapolationType.Constant`, `ExtrapolationType.Linear`
`ExtrapolationType.Extension`, `ExtrapolationType.Periodic` and `ExtrapolationType.Reflective`.
- `extrapolation_left`: The extrapolation type applied left of the data. See `extrapolation` for
the possible options. This keyword is ignored if `extrapolation != Extrapolation.none`.
- `extrapolation_right`: The extrapolation type applied right of the data. See `extrapolation` for
the possible options. This keyword is ignored if `extrapolation != Extrapolation.none`.
- `cache_parameters`: precompute parameters at initialization for faster interpolation computations. Note: if activated, `u` and `t` should not be modified. Defaults to `false`.
- `assume_linear_t`: boolean value to specify a faster index lookup behaviour for
evenly-distributed abscissae. Alternatively, a numerical threshold may be specified
for a test based on the normalized standard deviation of the difference with respect
to the straight line (see [`looks_linear`](@ref)). Defaults to 1e-2.
"""
struct AkimaInterpolation{uType, tType, IType, bType, cType, dType, T} <:
AbstractInterpolation{T}
u::uType
t::tType
I::IType
b::bType
c::cType
d::dType
extrapolation_left::ExtrapolationType.T
extrapolation_right::ExtrapolationType.T
iguesser::Guesser{tType}
cache_parameters::Bool
linear_lookup::Bool
function AkimaInterpolation(
u, t, I, b, c, d, extrapolation_left,
extrapolation_right, cache_parameters, assume_linear_t)
linear_lookup = seems_linear(assume_linear_t, t)
new{typeof(u), typeof(t), typeof(I), typeof(b), typeof(c),
typeof(d), eltype(u)}(u,
t,
I,
b,
c,
d,
extrapolation_left,
extrapolation_right,
Guesser(t),
cache_parameters,
linear_lookup
)
end
end
function AkimaInterpolation(
u, t; extrapolation::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.None, cache_parameters = false, assume_linear_t = 1e-2)
extrapolation_left, extrapolation_right = munge_extrapolation(
extrapolation, extrapolation_left, extrapolation_right)
u, t = munge_data(u, t)
linear_lookup = seems_linear(assume_linear_t, t)
n = length(t)
dt = diff(t)
m = Array{eltype(u)}(undef, n + 3)
m[3:(end - 2)] = diff(u) ./ dt
m[2] = 2m[3] - m[4]
m[1] = 2m[2] - m[3]
m[end - 1] = 2m[end - 2] - m[end - 3]
m[end] = 2m[end - 1] - m[end - 2]
b = (m[4:end] .+ m[1:(end - 3)]) ./ 2
dm = abs.(diff(m))
f1 = dm[3:(n + 2)]
f2 = dm[1:n]
f12 = f1 + f2
ind = findall(f12 .> 1e-9 * maximum(f12))
b[ind] = (f1[ind] .* m[ind .+ 1] .+
f2[ind] .* m[ind .+ 2]) ./ f12[ind]
c = (3 .* m[3:(end - 2)] .- 2 .* b[1:(end - 1)] .- b[2:end]) ./ dt
d = (b[1:(end - 1)] .+ b[2:end] .- 2 .* m[3:(end - 2)]) ./ dt .^ 2
A = AkimaInterpolation(
u, t, nothing, b, c, d, extrapolation_left,
extrapolation_right, cache_parameters, linear_lookup)
I = cumulative_integral(A, cache_parameters)
AkimaInterpolation(u, t, I, b, c, d, extrapolation_left,
extrapolation_right, cache_parameters, linear_lookup)
end
"""
ConstantInterpolation(u, t; dir = :left, extrapolation::ExtrapolationType.T = ExtrapolationType.None, extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.None, cache_parameters = false)
It is the method of interpolating using a constant polynomial. For any point, two adjacent data points are found on either side (left and right). The value at that point depends on `dir`.
If it is `:left`, then the value at the left point is chosen and if it is `:right`, the value at the right point is chosen.
Extrapolation extends the last constant polynomial at the end points on each side.
## Arguments
- `u`: data points.
- `t`: time points.
## Keyword Arguments
- `dir`: indicates which value should be used for interpolation (`:left` or `:right`).
- `extrapolation`: The extrapolation type applied left and right of the data. Possible options
are `ExtrapolationType.None` (default), `ExtrapolationType.Constant`, `ExtrapolationType.Linear`
`ExtrapolationType.Extension`, `ExtrapolationType.Periodic` and `ExtrapolationType.Reflective`.
- `extrapolation_left`: The extrapolation type applied left of the data. See `extrapolation` for
the possible options. This keyword is ignored if `extrapolation != Extrapolation.none`.
- `extrapolation_right`: The extrapolation type applied right of the data. See `extrapolation` for
the possible options. This keyword is ignored if `extrapolation != Extrapolation.none`.
- `cache_parameters`: precompute parameters at initialization for faster interpolation computations. Note: if activated, `u` and `t` should not be modified. Defaults to `false`.
- `assume_linear_t`: boolean value to specify a faster index lookup behaviour for
evenly-distributed abscissae. Alternatively, a numerical threshold may be specified
for a test based on the normalized standard deviation of the difference with respect
to the straight line (see [`looks_linear`](@ref)). Defaults to 1e-2.
"""
struct ConstantInterpolation{uType, tType, IType, T} <: AbstractInterpolation{T}
u::uType
t::tType
I::IType
p::Nothing
dir::Symbol # indicates if value to the $dir should be used for the interpolation
extrapolation_left::ExtrapolationType.T
extrapolation_right::ExtrapolationType.T
iguesser::Guesser{tType}
cache_parameters::Bool
linear_lookup::Bool
function ConstantInterpolation(
u, t, I, dir, extrapolation_left, extrapolation_right,
cache_parameters, assume_linear_t)
linear_lookup = seems_linear(assume_linear_t, t)
new{typeof(u), typeof(t), typeof(I), eltype(u)}(
u, t, I, nothing, dir, extrapolation_left, extrapolation_right,
Guesser(t), cache_parameters, linear_lookup)
end
end
function ConstantInterpolation(
u, t; dir = :left, extrapolation::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.None,
cache_parameters = false, assume_linear_t = 1e-2)
extrapolation_left, extrapolation_right = munge_extrapolation(
extrapolation, extrapolation_left, extrapolation_right)
u, t = munge_data(u, t)
A = ConstantInterpolation(
u, t, nothing, dir, extrapolation_left,
extrapolation_right, cache_parameters, assume_linear_t)
I = cumulative_integral(A, cache_parameters)
ConstantInterpolation(u, t, I, dir, extrapolation_left, extrapolation_right,
cache_parameters, assume_linear_t)
end
"""
QuadraticSpline(u, t; extrapolation::ExtrapolationType.T = ExtrapolationType.None, extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.None, cache_parameters = false)
It is a spline interpolation using piecewise quadratic polynomials between each pair of data points. Its first derivative is also continuous.
Extrapolation extends the last quadratic polynomial on each side.
## Arguments
- `u`: data points.
- `t`: time points.
## Keyword Arguments
- `extrapolation`: The extrapolation type applied left and right of the data. Possible options
are `ExtrapolationType.None` (default), `ExtrapolationType.Constant`, `ExtrapolationType.Linear`
`ExtrapolationType.Extension`, `ExtrapolationType.Periodic` and `ExtrapolationType.Reflective`.
- `extrapolation_left`: The extrapolation type applied left of the data. See `extrapolation` for
the possible options. This keyword is ignored if `extrapolation != Extrapolation.none`.
- `extrapolation_right`: The extrapolation type applied right of the data. See `extrapolation` for
the possible options. This keyword is ignored if `extrapolation != Extrapolation.none`.
- `cache_parameters`: precompute parameters at initialization for faster interpolation computations. Note: if activated, `u` and `t` should not be modified. Defaults to `false`.
- `assume_linear_t`: boolean value to specify a faster index lookup behaviour for
evenly-distributed abscissae. Alternatively, a numerical threshold may be specified
for a test based on the normalized standard deviation of the difference with respect
to the straight line (see [`looks_linear`](@ref)). Defaults to 1e-2.
"""
struct QuadraticSpline{uType, tType, IType, pType, kType, cType, scType, T} <:
AbstractInterpolation{T}
u::uType
t::tType
I::IType
p::QuadraticSplineParameterCache{pType}
k::kType # knot vector
c::cType # B-spline control points
sc::scType # Spline coefficients (preallocated memory)
extrapolation_left::ExtrapolationType.T
extrapolation_right::ExtrapolationType.T
iguesser::Guesser{tType}
cache_parameters::Bool
linear_lookup::Bool
function QuadraticSpline(
u, t, I, p, k, c, sc, extrapolation_left,
extrapolation_right, cache_parameters, assume_linear_t)
linear_lookup = seems_linear(assume_linear_t, t)
new{typeof(u), typeof(t), typeof(I), typeof(p.α), typeof(k),
typeof(c), typeof(sc), eltype(u)}(u,
t,
I,
p,
k,
c,
sc,
extrapolation_left,
extrapolation_right,
Guesser(t),
cache_parameters,
linear_lookup
)
end
end
function QuadraticSpline(
u::uType, t; extrapolation::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.None,
cache_parameters = false, assume_linear_t = 1e-2) where {uType <:
AbstractVector{<:Number}}
extrapolation_left, extrapolation_right = munge_extrapolation(
extrapolation, extrapolation_left, extrapolation_right)
u, t = munge_data(u, t)
n = length(t)
dtype_sc = typeof(t[1] / t[1])
sc = zeros(dtype_sc, n)
k, A = quadratic_spline_params(t, sc)
c = A \ u
p = QuadraticSplineParameterCache(u, t, k, c, sc, cache_parameters)
A = QuadraticSpline(
u, t, nothing, p, k, c, sc, extrapolation_left,
extrapolation_right, cache_parameters, assume_linear_t)
I = cumulative_integral(A, cache_parameters)
QuadraticSpline(u, t, I, p, k, c, sc, extrapolation_left,
extrapolation_right, cache_parameters, assume_linear_t)
end
function QuadraticSpline(
u::uType, t; extrapolation::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.None, cache_parameters = false,
assume_linear_t = 1e-2) where {uType <:
AbstractVector}
extrapolation_left, extrapolation_right = munge_extrapolation(
extrapolation, extrapolation_left, extrapolation_right)
u, t = munge_data(u, t)
n = length(t)
dtype_sc = typeof(t[1] / t[1])
sc = zeros(dtype_sc, n)
k, A = quadratic_spline_params(t, sc)
eltype_c_prototype = one(dtype_sc) * first(u)
c = [similar(eltype_c_prototype) for _ in 1:n]
# Assuming u contains arrays of equal shape
for j in eachindex(eltype_c_prototype)
c_dim = A \ [u_[j] for u_ in u]
for (i, c_dim_) in enumerate(c_dim)
c[i][j] = c_dim_
end
end
p = QuadraticSplineParameterCache(u, t, k, c, sc, cache_parameters)
A = QuadraticSpline(
u, t, nothing, p, k, c, sc, extrapolation_left,
extrapolation_right, cache_parameters, assume_linear_t)
I = cumulative_integral(A, cache_parameters)
QuadraticSpline(u, t, I, p, k, c, sc, extrapolation_left,
extrapolation_right, cache_parameters, assume_linear_t)
end
"""
CubicSpline(u, t; extrapolation::ExtrapolationType.T = ExtrapolationType.None, extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.None, cache_parameters = false)
It is a spline interpolation using piecewise cubic polynomials between each pair of data points. Its first and second derivative is also continuous.
Second derivative on both ends are zero, which are also called "natural" boundary conditions. Extrapolation extends the last cubic polynomial on each side.
## Arguments
- `u`: data points.
- `t`: time points.
## Keyword Arguments
- `extrapolation`: The extrapolation type applied left and right of the data. Possible options
are `ExtrapolationType.None` (default), `ExtrapolationType.Constant`, `ExtrapolationType.Linear`
`ExtrapolationType.Extension`, `ExtrapolationType.Periodic` and `ExtrapolationType.Reflective`.
- `extrapolation_left`: The extrapolation type applied left of the data. See `extrapolation` for
the possible options. This keyword is ignored if `extrapolation != Extrapolation.none`.
- `extrapolation_right`: The extrapolation type applied right of the data. See `extrapolation` for
the possible options. This keyword is ignored if `extrapolation != Extrapolation.none`.
- `cache_parameters`: precompute parameters at initialization for faster interpolation computations. Note: if activated, `u` and `t` should not be modified. Defaults to `false`.
- `assume_linear_t`: boolean value to specify a faster index lookup behaviour for
evenly-distributed abscissae. Alternatively, a numerical threshold may be specified
for a test based on the normalized standard deviation of the difference with respect
to the straight line (see [`looks_linear`](@ref)). Defaults to 1e-2.
"""
struct CubicSpline{uType, tType, IType, pType, hType, zType, T} <:
AbstractInterpolation{T}
u::uType
t::tType
I::IType
p::CubicSplineParameterCache{pType}
h::hType
z::zType
extrapolation_left::ExtrapolationType.T
extrapolation_right::ExtrapolationType.T
iguesser::Guesser{tType}
cache_parameters::Bool
linear_lookup::Bool
function CubicSpline(u, t, I, p, h, z, extrapolation_left,
extrapolation_right, cache_parameters, assume_linear_t)
linear_lookup = seems_linear(assume_linear_t, t)
new{typeof(u), typeof(t), typeof(I), typeof(p.c₁),
typeof(h), typeof(z), eltype(u)}(
u,
t,
I,
p,
h,
z,
extrapolation_left,
extrapolation_right,
Guesser(t),
cache_parameters,
linear_lookup
)
end
end
function CubicSpline(u::AbstractVector{<:Number},
t; extrapolation::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.None, cache_parameters = false,
assume_linear_t = 1e-2)
extrapolation_left, extrapolation_right = munge_extrapolation(
extrapolation, extrapolation_left, extrapolation_right)
u, t = munge_data(u, t)
n = length(t) - 1
h = vcat(0, map(k -> t[k + 1] - t[k], 1:(length(t) - 1)), 0)
dl = vcat(h[2:n], zero(eltype(h)))
d_tmp = 2 .* (h[1:(n + 1)] .+ h[2:(n + 2)])
du = vcat(zero(eltype(h)), h[3:(n + 1)])
tA = Tridiagonal(dl, d_tmp, du)
# zero for element type of d, which we don't know yet
typed_zero = zero(6(u[begin + 2] - u[begin + 1]) / h[begin + 2] -
6(u[begin + 1] - u[begin]) / h[begin + 1])
d = map(
i -> i == 1 || i == n + 1 ? typed_zero :
6(u[i + 1] - u[i]) / h[i + 1] - 6(u[i] - u[i - 1]) / h[i],
1:(n + 1))
z = tA \ d
linear_lookup = seems_linear(assume_linear_t, t)
p = CubicSplineParameterCache(u, h, z, cache_parameters)
A = CubicSpline(
u, t, nothing, p, h[1:(n + 1)], z, extrapolation_left,
extrapolation_right, cache_parameters, linear_lookup)
I = cumulative_integral(A, cache_parameters)
CubicSpline(u, t, I, p, h[1:(n + 1)], z, extrapolation_left,
extrapolation_right, cache_parameters, linear_lookup)
end
function CubicSpline(u::AbstractArray{T, N},
t;
extrapolation::ExtrapolationType.T = ExtrapolationType.None, extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.None, cache_parameters = false,
assume_linear_t = 1e-2) where {T, N}
extrapolation_left, extrapolation_right = munge_extrapolation(
extrapolation, extrapolation_left, extrapolation_right)
u, t = munge_data(u, t)
n = length(t) - 1
h = vcat(0, map(k -> t[k + 1] - t[k], 1:(length(t) - 1)), 0)
dl = vcat(h[2:n], zero(eltype(h)))
d_tmp = 2 .* (h[1:(n + 1)] .+ h[2:(n + 2)])
du = vcat(zero(eltype(h)), h[3:(n + 1)])
tA = Tridiagonal(dl, d_tmp, du)
# zero for element type of d, which we don't know yet
ax = axes(u)[1:(end - 1)]
typed_zero = zero(6(u[ax..., begin + 2] - u[ax..., begin + 1]) / h[begin + 2] -
6(u[ax..., begin + 1] - u[ax..., begin]) / h[begin + 1])
h_ = reshape(h, ones(Int64, N - 1)..., :)
ax_h = axes(h_)[1:(end - 1)]
d = 6 * ((u[ax..., 3:(n + 1)] - u[ax..., 2:n]) ./ h_[ax_h..., 3:(n + 1)]) -
6 * ((u[ax..., 2:n] - u[ax..., 1:(n - 1)]) ./ h_[ax_h..., 2:n])
d = cat(typed_zero, d, typed_zero; dims = ndims(d))
d_reshaped = reshape(d, prod(size(d)[1:(end - 1)]), :)
z = (tA \ d_reshaped')'
z = reshape(z, size(u)...)
linear_lookup = seems_linear(assume_linear_t, t)
p = CubicSplineParameterCache(u, h, z, cache_parameters)
A = CubicSpline(
u, t, nothing, p, h[1:(n + 1)], z, extrapolation_left,
extrapolation_right, cache_parameters, linear_lookup)
I = cumulative_integral(A, cache_parameters)
CubicSpline(u, t, I, p, h[1:(n + 1)], z, extrapolation_left,
extrapolation_right, cache_parameters, linear_lookup)
end
function CubicSpline(
u::AbstractVector, t; extrapolation::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.None, cache_parameters = false,
assume_linear_t = 1e-2)
extrapolation_left, extrapolation_right = munge_extrapolation(
extrapolation, extrapolation_left, extrapolation_right)
u, t = munge_data(u, t)
n = length(t) - 1
h = vcat(0, map(k -> t[k + 1] - t[k], 1:(length(t) - 1)), 0)
dl = vcat(h[2:n], zero(eltype(h)))
d_tmp = 2 .* (h[1:(n + 1)] .+ h[2:(n + 2)])
du = vcat(zero(eltype(h)), h[3:(n + 1)])
tA = Tridiagonal(dl, d_tmp, du)
d_ = map(
i -> i == 1 || i == n + 1 ? zeros(eltype(t), size(u[1])) :
6(u[i + 1] - u[i]) / h[i + 1] - 6(u[i] - u[i - 1]) / h[i],
1:(n + 1))
d = transpose(reshape(reduce(hcat, d_), :, n + 1))
z_ = reshape(transpose(tA \ d), size(u[1])..., :)
z = [z_s for z_s in eachslice(z_, dims = ndims(z_))]
p = CubicSplineParameterCache(u, h, z, cache_parameters)
A = CubicSpline(
u, t, nothing, p, h[1:(n + 1)], z, extrapolation_left,
extrapolation_right, cache_parameters, assume_linear_t)
I = cumulative_integral(A, cache_parameters)
CubicSpline(u, t, I, p, h[1:(n + 1)], z, extrapolation_left,
extrapolation_right, cache_parameters, assume_linear_t)
end
"""
BSplineInterpolation(u, t, d, pVecType, knotVecType; extrapolation::ExtrapolationType.T = ExtrapolationType.None, extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.None)
It is a curve defined by the linear combination of `n` basis functions of degree `d` where `n` is the number of data points. For more information, refer [https://pages.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/B-spline/bspline-curve.html](https://pages.mtu.edu/%7Eshene/COURSES/cs3621/NOTES/spline/B-spline/bspline-curve.html).
Extrapolation is a constant polynomial of the end points on each side.
## Arguments
- `u`: data points.
- `t`: time points.
- `d`: degree of the piecewise polynomial.
- `pVecType`: symbol to parameters vector, `:Uniform` for uniform spaced parameters and `:ArcLen` for parameters generated by chord length method.
- `knotVecType`: symbol to knot vector, `:Uniform` for uniform knot vector, `:Average` for average spaced knot vector.
## Keyword Arguments
- `extrapolation`: The extrapolation type applied left and right of the data. Possible options
are `ExtrapolationType.None` (default), `ExtrapolationType.Constant`, `ExtrapolationType.Linear`
`ExtrapolationType.Extension`, `ExtrapolationType.Periodic` and `ExtrapolationType.Reflective`.
- `extrapolation_left`: The extrapolation type applied left of the data. See `extrapolation` for
the possible options. This keyword is ignored if `extrapolation != Extrapolation.none`.
- `extrapolation_right`: The extrapolation type applied right of the data. See `extrapolation` for
the possible options. This keyword is ignored if `extrapolation != Extrapolation.none`.
- `assume_linear_t`: boolean value to specify a faster index lookup behavior for
evenly-distributed abscissae. Alternatively, a numerical threshold may be specified
for a test based on the normalized standard deviation of the difference with respect
to the straight line (see [`looks_linear`](@ref)). Defaults to 1e-2.
"""
struct BSplineInterpolation{uType, tType, pType, kType, cType, scType, T} <:
AbstractInterpolation{T}
u::uType
t::tType
d::Int # degree
p::pType # params vector
k::kType # knot vector
c::cType # control points
sc::scType # Spline coefficients (preallocated memory)
pVecType::Symbol
knotVecType::Symbol
extrapolation_left::ExtrapolationType.T
extrapolation_right::ExtrapolationType.T
iguesser::Guesser{tType}
linear_lookup::Bool
function BSplineInterpolation(u,
t,
d,
p,
k,
c,
sc,
pVecType,
knotVecType,
extrapolation_left,
extrapolation_right,
assume_linear_t)
linear_lookup = seems_linear(assume_linear_t, t)
new{typeof(u), typeof(t), typeof(p), typeof(k), typeof(c), typeof(sc), eltype(u)}(
u,
t,
d,
p,
k,
c,
sc,
pVecType,
knotVecType,
extrapolation_left,
extrapolation_right,
Guesser(t),
linear_lookup
)
end
end
function BSplineInterpolation(
u::AbstractVector, t, d, pVecType, knotVecType;
extrapolation::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.None, assume_linear_t = 1e-2)
extrapolation_left, extrapolation_right = munge_extrapolation(
extrapolation, extrapolation_left, extrapolation_right)
u, t = munge_data(u, t)
n = length(t)
n < d + 1 && error("BSplineInterpolation needs at least d + 1, i.e. $(d+1) points.")
s = zero(eltype(u))
p = zero(t)
k = zeros(eltype(t), n + d + 1)
l = zeros(eltype(u), n - 1)
p[1] = zero(eltype(t))
p[end] = one(eltype(t))
for i in 2:n
s += √((t[i] - t[i - 1])^2 + (u[i] - u[i - 1])^2)
l[i - 1] = s
end
if pVecType == :Uniform
for i in 2:(n - 1)
p[i] = p[1] + (i - 1) * (p[end] - p[1]) / (n - 1)
end
elseif pVecType == :ArcLen
for i in 2:(n - 1)
p[i] = p[1] + l[i - 1] / s * (p[end] - p[1])
end
end
lidx = 1
ridx = length(k)
while lidx <= (d + 1) && ridx >= (length(k) - d)
k[lidx] = p[1]
k[ridx] = p[end]
lidx += 1
ridx -= 1
end
ps = zeros(eltype(t), n - 2)
s = zero(eltype(t))
for i in 2:(n - 1)
s += p[i]
ps[i - 1] = s
end
if knotVecType == :Uniform
# uniformly spaced knot vector
# this method is not recommended because, if it is used with the chord length method for global interpolation,
# the system of linear equations would be singular.
for i in (d + 2):n
k[i] = k[1] + (i - d - 1) // (n - d) * (k[end] - k[1])
end
elseif knotVecType == :Average
# average spaced knot vector
idx = 1
if d + 2 <= n
k[d + 2] = 1 // d * ps[d]
end
for i in (d + 3):n
k[i] = 1 // d * (ps[idx + d] - ps[idx])
idx += 1
end
end
# control points
sc = zeros(eltype(t), n, n)
spline_coefficients!(sc, d, k, p)
c = vec(sc \ u[:, :])
sc = zeros(eltype(t), n)
BSplineInterpolation(
u, t, d, p, k, c, sc, pVecType, knotVecType,
extrapolation_left, extrapolation_right, assume_linear_t)
end
function BSplineInterpolation(
u::AbstractArray, t, d, pVecType, knotVecType;
extrapolation::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.None,
assume_linear_t = 1e-2)
extrapolation_left, extrapolation_right = munge_extrapolation(
extrapolation, extrapolation_left, extrapolation_right)
u, t = munge_data(u, t)
n = length(t)
n < d + 1 && error("BSplineInterpolation needs at least d + 1, i.e. $(d+1) points.")
s = zero(eltype(u))
p = zero(t)
k = zeros(eltype(t), n + d + 1)
l = zeros(eltype(u), n - 1)
p[1] = zero(eltype(t))
p[end] = one(eltype(t))
ax_u = axes(u)[1:(end - 1)]
for i in 2:n
s += √((t[i] - t[i - 1])^2 + sum((u[ax_u..., i] - u[ax_u..., i - 1]) .^ 2))
l[i - 1] = s
end
if pVecType == :Uniform
for i in 2:(n - 1)
p[i] = p[1] + (i - 1) * (p[end] - p[1]) / (n - 1)
end
elseif pVecType == :ArcLen
for i in 2:(n - 1)
p[i] = p[1] + l[i - 1] / s * (p[end] - p[1])
end
end
lidx = 1
ridx = length(k)
while lidx <= (d + 1) && ridx >= (length(k) - d)
k[lidx] = p[1]
k[ridx] = p[end]
lidx += 1
ridx -= 1
end
ps = zeros(eltype(t), n - 2)
s = zero(eltype(t))
for i in 2:(n - 1)
s += p[i]
ps[i - 1] = s
end
if knotVecType == :Uniform
# uniformly spaced knot vector
# this method is not recommended because, if it is used with the chord length method for global interpolation,
# the system of linear equations would be singular.
for i in (d + 2):n
k[i] = k[1] + (i - d - 1) // (n - d) * (k[end] - k[1])
end
elseif knotVecType == :Average
# average spaced knot vector
idx = 1
if d + 2 <= n
k[d + 2] = 1 // d * ps[d]
end
for i in (d + 3):n
k[i] = 1 // d * (ps[idx + d] - ps[idx])
idx += 1
end
end
# control points
sc = zeros(eltype(t), n, n)
spline_coefficients!(sc, d, k, p)
c = (sc \ reshape(u, prod(size(u)[1:(end - 1)]), :)')'
c = reshape(c, size(u)...)
sc = zeros(eltype(t), n)
BSplineInterpolation(
u, t, d, p, k, c, sc, pVecType, knotVecType,
extrapolation_left, extrapolation_right, assume_linear_t)
end
"""
BSplineApprox(u, t, d, h, pVecType, knotVecType; extrapolation::ExtrapolationType.T = ExtrapolationType.None, extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.None)
It is a regression based B-spline. The argument choices are the same as the `BSplineInterpolation`, with the additional parameter `h < length(t)` which is the number of control points to use, with smaller `h` indicating more smoothing.
For more information, refer [http://www.cad.zju.edu.cn/home/zhx/GM/009/00-bsia.pdf](http://www.cad.zju.edu.cn/home/zhx/GM/009/00-bsia.pdf).
Extrapolation is a constant polynomial of the end points on each side.
## Arguments
- `u`: data points.
- `t`: time points.
- `d`: degree of the piecewise polynomial.
- `h`: number of control points to use.
- `pVecType`: symbol to parameters vector, `:Uniform` for uniform spaced parameters and `:ArcLen` for parameters generated by chord length method.
- `knotVecType`: symbol to knot vector, `:Uniform` for uniform knot vector, `:Average` for average spaced knot vector.
## Keyword Arguments
- `extrapolation`: The extrapolation type applied left and right of the data. Possible options
are `ExtrapolationType.None` (default), `ExtrapolationType.Constant`, `ExtrapolationType.Linear`
`ExtrapolationType.Extension`, `ExtrapolationType.Periodic` and `ExtrapolationType.Reflective`.
- `extrapolation_left`: The extrapolation type applied left of the data. See `extrapolation` for
the possible options. This keyword is ignored if `extrapolation != Extrapolation.none`.
- `extrapolation_right`: The extrapolation type applied right of the data. See `extrapolation` for
the possible options. This keyword is ignored if `extrapolation != Extrapolation.none`.
- `assume_linear_t`: boolean value to specify a faster index lookup behaviour for
evenly-distributed abscissae. Alternatively, a numerical threshold may be specified
for a test based on the normalized standard deviation of the difference with respect
to the straight line (see [`looks_linear`](@ref)). Defaults to 1e-2.
"""
struct BSplineApprox{uType, tType, pType, kType, cType, scType, T} <:
AbstractInterpolation{T}
u::uType
t::tType
d::Int # degree
h::Int # number of control points (n => h >= d >= 1)
p::pType # params vector
k::kType # knot vector
c::cType # control points
sc::scType # Spline coefficients (preallocated memory)
pVecType::Symbol
knotVecType::Symbol
extrapolation_left::ExtrapolationType.T
extrapolation_right::ExtrapolationType.T
iguesser::Guesser{tType}
linear_lookup::Bool
function BSplineApprox(u,
t,
d,
h,
p,
k,
c,
sc,
pVecType,
knotVecType,
extrapolation_left,
extrapolation_right,
assume_linear_t
)
linear_lookup = seems_linear(assume_linear_t, t)
new{typeof(u), typeof(t), typeof(p), typeof(k), typeof(c), typeof(sc), eltype(u)}(
u,
t,
d,
h,
p,
k,
c,
sc,
pVecType,
knotVecType,
extrapolation_left,
extrapolation_right,
Guesser(t),
linear_lookup
)
end
end
function BSplineApprox(
u::AbstractVector, t, d, h, pVecType, knotVecType;
extrapolation::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_left::ExtrapolationType.T = ExtrapolationType.None,
extrapolation_right::ExtrapolationType.T = ExtrapolationType.None, assume_linear_t = 1e-2)
extrapolation_left, extrapolation_right = munge_extrapolation(
extrapolation, extrapolation_left, extrapolation_right)
u, t = munge_data(u, t)
n = length(t)
h < d + 1 && error("BSplineApprox needs at least d + 1, i.e. $(d+1) control points.")
s = zero(eltype(u))
p = zero(t)
k = zeros(eltype(t), h + d + 1)
l = zeros(eltype(u), n - 1)
p[1] = zero(eltype(t))
p[end] = one(eltype(t))
for i in 2:n
s += √((t[i] - t[i - 1])^2 + (u[i] - u[i - 1])^2)
l[i - 1] = s
end
if pVecType == :Uniform
for i in 2:(n - 1)
p[i] = p[1] + (i - 1) * (p[end] - p[1]) / (n - 1)
end
elseif pVecType == :ArcLen
for i in 2:(n - 1)
p[i] = p[1] + l[i - 1] / s * (p[end] - p[1])
end
end
lidx = 1
ridx = length(k)
while lidx <= (d + 1) && ridx >= (length(k) - d)
k[lidx] = p[1]
k[ridx] = p[end]
lidx += 1
ridx -= 1
end
ps = zeros(eltype(t), n - 2)
s = zero(eltype(t))