gps_pvt 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,121 @@
1
+ /*
2
+ * Copyright (c) 2022, M.Naruoka (fenrir)
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without modification,
6
+ * are permitted provided that the following conditions are met:
7
+ *
8
+ * - Redistributions of source code must retain the above copyright notice,
9
+ * this list of conditions and the following disclaimer.
10
+ * - Redistributions in binary form must reproduce the above copyright notice,
11
+ * this list of conditions and the following disclaimer in the documentation
12
+ * and/or other materials provided with the distribution.
13
+ * - Neither the name of the naruoka.org nor the names of its contributors
14
+ * may be used to endorse or promote products derived from this software
15
+ * without specific prior written permission.
16
+ *
17
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
21
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
22
+ * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+ *
30
+ */
31
+
32
+ #ifndef __INTERPOLATE_H__
33
+ #define __INTERPOLATE_H__
34
+
35
+ #include <cstddef>
36
+ #include <vector>
37
+
38
+ /*
39
+ * perform Neville's interpolation (with derivative)
40
+ *
41
+ * @param x_given list of x assumed to be accessible with [0..n], order is non-sensitive
42
+ * @param y_given list of y assumed to be accessible with [0..n], order is non-sensitive
43
+ * @param x desired x
44
+ * @param y y[0] is output, and y[1..n-1] are used as buffer to store temporary results
45
+ * @param n order
46
+ * @param dy dy[i][0] is (i+1)th derivative outputs like y[0];
47
+ * dy[0..nd][0..n-1] should be accessible
48
+ * @param nd maximum order of required derivative
49
+ * @return (Ty &) [0] = interpolated result
50
+ */
51
+ template <class Tx_Array, class Ty_Array, class Tx, class Ty>
52
+ Ty &interpolate_Neville(
53
+ const Tx_Array &x_given, const Ty_Array &y_given,
54
+ const Tx &x, Ty &y, const std::size_t &n,
55
+ Ty *dy = NULL, const std::size_t &nd = 0) {
56
+ if(n == 0){
57
+ y[0] = y_given[0];
58
+ // for(std::size_t d(nd); d >= 0; --d){dy[d][0] = 0;}
59
+ return y;
60
+ }
61
+ { // first step
62
+ if(nd > 0){ // for 1st order derivative
63
+ for(std::size_t i(0); i < n; ++i){
64
+ dy[0][i] = (y_given[i+1] - y_given[i]);
65
+ dy[0][i] /= (x_given[i+1] - x_given[i]);
66
+ }
67
+ }
68
+ for(std::size_t i(0); i < n; ++i){ // linear interpolation
69
+ Tx a(x_given[i+1] - x), b(x - x_given[i]);
70
+ y[i] = (y_given[i] * a + y_given[i+1] * b);
71
+ y[i] /= (x_given[i+1] - x_given[i]);
72
+ }
73
+ }
74
+ for(std::size_t j(2); j <= n; ++j){
75
+ int d((nd >= j) ? j : nd);
76
+ // d = 1, 2, ... are 1st, 2nd, ... order derivative
77
+ // In order to avoid overwriting of temporary calculation,
78
+ // higher derivative calculation is performed earlier.
79
+ // dy[d(>=j+1)] is skipped because of 0
80
+ if(d >= (int)j){ // for derivative, just use lower level
81
+ Ty &dy0(dy[d-1]), &dy1(d > 1 ? (Ty &)dy[d-2] : (Ty &)y); // cast required for MSVC
82
+ for(std::size_t i(0); i <= (n - j); ++i){
83
+ dy0[i] = (dy1[i+1] - dy1[i]) * d;
84
+ dy0[i] /= (x_given[i + j] - x_given[i]);
85
+ }
86
+ --d;
87
+ }
88
+ for(; d > 0; --d){ // for derivative, use same and lower level
89
+ Ty &dy0(dy[d-1]), &dy1(d > 1 ? (Ty &)dy[d-2] : (Ty &)y); // cast required for MSVC
90
+ for(std::size_t i(0); i <= (n - j); ++i){
91
+ Tx a(x_given[i + j] - x), b(x - x_given[i]);
92
+ dy0[i] = dy0[i] * a + dy0[i+1] * b;
93
+ dy0[i] += (dy1[i+1] - dy1[i]) * d;
94
+ dy0[i] /= (x_given[i + j] - x_given[i]);
95
+ }
96
+ }
97
+ for(std::size_t i(0); i <= (n - j); ++i){ // d == 0 (interpolation), just use same level
98
+ Tx a(x_given[i + j] - x), b(x - x_given[i]);
99
+ y[i] = (y[i] * a + y[i+1] * b);
100
+ y[i] /= (x_given[i + j] - x_given[i]);
101
+ }
102
+ }
103
+ return y;
104
+ }
105
+
106
+ template <class Tx_Array, class Ty_Array, class Tx, class Ty, std::size_t N>
107
+ Ty interpolate_Neville(
108
+ const Tx_Array &x_given, const Ty_Array &y_given,
109
+ const Tx &x, Ty (&y_buf)[N]) {
110
+ return interpolate_Neville(x_given, y_given, x, y_buf, N)[0];
111
+ }
112
+
113
+ template <class Ty, class Tx_Array, class Ty_Array, class Tx>
114
+ Ty interpolate_Neville(
115
+ const Tx_Array &x_given, const Ty_Array &y_given, const Tx &x, const std::size_t &n) {
116
+ if(n == 0){return y_given[0];}
117
+ std::vector<Ty> y_buf(n);
118
+ return interpolate_Neville(x_given, y_given, x, y_buf, n)[0];
119
+ }
120
+
121
+ #endif /* __INTERPOLATE_H__ */