Apollo 10.0
自动驾驶开放平台
integral.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
22#pragma once
23
24#include <array>
25#include <cstddef>
26#include <functional>
27#include <utility>
28#include <vector>
29
34namespace apollo {
35namespace common {
36namespace math {
37
38double IntegrateBySimpson(const std::vector<double>& funv_vec, const double dx,
39 const std::size_t nsteps);
40
41double IntegrateByTrapezoidal(const std::vector<double>& funv_vec,
42 const double dx, const std::size_t nsteps);
43
49template <std::size_t N>
50std::pair<std::array<double, N>, std::array<double, N>>
52
53template <>
54inline std::pair<std::array<double, 2>, std::array<double, 2>>
56 std::array<double, 2> x;
57 x[0] = -5.77350269189625764507e-01;
58 x[1] = 5.77350269189625764507e-01;
59
60 std::array<double, 2> w;
61 w[0] = 1.0;
62 w[1] = 1.0;
63
64 return std::make_pair(x, w);
65}
66
67template <>
68inline std::pair<std::array<double, 3>, std::array<double, 3>>
70 std::array<double, 3> x;
71 x[0] = 0.00000000000000000000e+00;
72 x[1] = 7.74596669241483377010e-01;
73 x[2] = -7.74596669241483377010e-01;
74
75 std::array<double, 3> w;
76 w[0] = 8.88888888888888888877e-01;
77 w[1] = 5.55555555555555555562e-01;
78 w[2] = 5.55555555555555555562e-01;
79
80 return std::make_pair(x, w);
81}
82
83template <>
84inline std::pair<std::array<double, 4>, std::array<double, 4>>
86 std::array<double, 4> x;
87 x[0] = 3.39981043584856264792e-01;
88 x[1] = -3.39981043584856264792e-01;
89 x[2] = 8.61136311594052575248e-01;
90 x[3] = -8.61136311594052575248e-01;
91
92 std::array<double, 4> w;
93 w[0] = 6.52145154862546142644e-01;
94 w[1] = 6.52145154862546142644e-01;
95 w[2] = 3.47854845137453857383e-01;
96 w[3] = 3.47854845137453857383e-01;
97
98 return std::make_pair(x, w);
99}
100
101template <>
102inline std::pair<std::array<double, 5>, std::array<double, 5>>
104 std::array<double, 5> x;
105 x[0] = 0.00000000000000000000e+00;
106 x[1] = 5.38469310105683091018e-01;
107 x[2] = -5.38469310105683091018e-01;
108 x[3] = 9.06179845938663992811e-01;
109 x[4] = -9.06179845938663992811e-01;
110
111 std::array<double, 5> w;
112 w[0] = 5.68888888888888888883e-01;
113 w[1] = 4.78628670499366468030e-01;
114 w[2] = 4.78628670499366468030e-01;
115 w[3] = 2.36926885056189087515e-01;
116 w[4] = 2.36926885056189087515e-01;
117
118 return std::make_pair(x, w);
119}
120
121template <>
122inline std::pair<std::array<double, 6>, std::array<double, 6>>
124 std::array<double, 6> x;
125 x[0] = 6.61209386466264513688e-01;
126 x[1] = -6.61209386466264513688e-01;
127 x[2] = 2.38619186083196908630e-01;
128 x[3] = -2.38619186083196908630e-01;
129 x[4] = 9.32469514203152027832e-01;
130 x[5] = -9.32469514203152027832e-01;
131
132 std::array<double, 6> w;
133 w[0] = 3.60761573048138607569e-01;
134 w[1] = 3.60761573048138607569e-01;
135 w[2] = 4.67913934572691047389e-01;
136 w[3] = 4.67913934572691047389e-01;
137 w[4] = 1.71324492379170345043e-01;
138 w[5] = 1.71324492379170345043e-01;
139
140 return std::make_pair(x, w);
141}
142
143template <>
144inline std::pair<std::array<double, 7>, std::array<double, 7>>
146 std::array<double, 7> x;
147 x[0] = 0.00000000000000000000e+00;
148 x[1] = 4.05845151377397166917e-01;
149 x[2] = -4.05845151377397166917e-01;
150 x[3] = 7.41531185599394439864e-01;
151 x[4] = -7.41531185599394439864e-01;
152 x[5] = 9.49107912342758524541e-01;
153 x[6] = -9.49107912342758524541e-01;
154
155 std::array<double, 7> w;
156 w[0] = 4.17959183673469387749e-01;
157 w[1] = 3.81830050505118944961e-01;
158 w[2] = 3.81830050505118944961e-01;
159 w[3] = 2.79705391489276667890e-01;
160 w[4] = 2.79705391489276667890e-01;
161 w[5] = 1.29484966168869693274e-01;
162 w[6] = 1.29484966168869693274e-01;
163
164 return std::make_pair(x, w);
165}
166
167template <>
168inline std::pair<std::array<double, 8>, std::array<double, 8>>
170 std::array<double, 8> x;
171 x[0] = 1.83434642495649804936e-01;
172 x[1] = -1.83434642495649804936e-01;
173 x[2] = 5.25532409916328985830e-01;
174 x[3] = -5.25532409916328985830e-01;
175 x[4] = 7.96666477413626739567e-01;
176 x[5] = -7.96666477413626739567e-01;
177 x[6] = 9.60289856497536231661e-01;
178 x[7] = -9.60289856497536231661e-01;
179
180 std::array<double, 8> w;
181 w[0] = 3.62683783378361982976e-01;
182 w[1] = 3.62683783378361982976e-01;
183 w[2] = 3.13706645877887287338e-01;
184 w[3] = 3.13706645877887287338e-01;
185 w[4] = 2.22381034453374470546e-01;
186 w[5] = 2.22381034453374470546e-01;
187 w[6] = 1.01228536290376259154e-01;
188 w[7] = 1.01228536290376259154e-01;
189
190 return std::make_pair(x, w);
191}
192
193template <>
194inline std::pair<std::array<double, 9>, std::array<double, 9>>
196 std::array<double, 9> x;
197 x[0] = 0.00000000000000000000e+00;
198 x[1] = 8.36031107326635794313e-01;
199 x[2] = -8.36031107326635794313e-01;
200 x[3] = 9.68160239507626089810e-01;
201 x[4] = -9.68160239507626089810e-01;
202 x[5] = 3.24253423403808929042e-01;
203 x[6] = -3.24253423403808929042e-01;
204 x[7] = 6.13371432700590397285e-01;
205 x[8] = -6.13371432700590397285e-01;
206
207 std::array<double, 9> w;
208 w[0] = 3.30239355001259763154e-01;
209 w[1] = 1.80648160694857404059e-01;
210 w[2] = 1.80648160694857404059e-01;
211 w[3] = 8.12743883615744119737e-02;
212 w[4] = 8.12743883615744119737e-02;
213 w[5] = 3.12347077040002840057e-01;
214 w[6] = 3.12347077040002840057e-01;
215 w[7] = 2.60610696402935462313e-01;
216 w[8] = 2.60610696402935462313e-01;
217
218 return std::make_pair(x, w);
219}
220
221template <>
222inline std::pair<std::array<double, 10>, std::array<double, 10>>
224 std::array<double, 10> x;
225 x[0] = 1.48874338981631210881e-01;
226 x[1] = -1.48874338981631210881e-01;
227 x[2] = 4.33395394129247190794e-01;
228 x[3] = -4.33395394129247190794e-01;
229 x[4] = 6.79409568299024406207e-01;
230 x[5] = -6.79409568299024406207e-01;
231 x[6] = 8.65063366688984510759e-01;
232 x[7] = -8.65063366688984510759e-01;
233 x[8] = 9.73906528517171720066e-01;
234 x[9] = -9.73906528517171720066e-01;
235
236 std::array<double, 10> w;
237 w[0] = 2.95524224714752870187e-01;
238 w[1] = 2.95524224714752870187e-01;
239 w[2] = 2.69266719309996355105e-01;
240 w[3] = 2.69266719309996355105e-01;
241 w[4] = 2.19086362515982044000e-01;
242 w[5] = 2.19086362515982044000e-01;
243 w[6] = 1.49451349150580593150e-01;
244 w[7] = 1.49451349150580593150e-01;
245 w[8] = 6.66713443086881375920e-02;
246 w[9] = 6.66713443086881375920e-02;
247
248 return std::make_pair(x, w);
249}
250
271template <std::size_t N>
272double IntegrateByGaussLegendre(const std::function<double(double)>& func,
273 const double lower_bound,
274 const double upper_bound) {
275 auto p = GetGaussLegendrePoints<N>();
276
277 std::array<double, N> x = p.first;
278 std::array<double, N> w = p.second;
279
280 const double t = (upper_bound - lower_bound) * 0.5;
281 const double m = (upper_bound + lower_bound) * 0.5;
282
283 double integral = 0.0;
284 for (size_t i = 0; i < N; ++i) {
285 integral += w[i] * func(t * x[i] + m);
286 }
287
288 return integral * t;
289}
290
291} // namespace math
292} // namespace common
293} // namespace apollo
std::pair< std::array< double, N >, std::array< double, N > > GetGaussLegendrePoints()
Get the points and weights for different ordered Gauss-Legendre integration.
std::pair< std::array< double, 7 >, std::array< double, 7 > > GetGaussLegendrePoints< 7 >()
Definition integral.h:145
std::pair< std::array< double, 2 >, std::array< double, 2 > > GetGaussLegendrePoints< 2 >()
Definition integral.h:55
std::pair< std::array< double, 10 >, std::array< double, 10 > > GetGaussLegendrePoints< 10 >()
Definition integral.h:223
std::pair< std::array< double, 8 >, std::array< double, 8 > > GetGaussLegendrePoints< 8 >()
Definition integral.h:169
double IntegrateByGaussLegendre(const std::function< double(double)> &func, const double lower_bound, const double upper_bound)
Compute the integral of a target single-variable function from a lower bound to an upper bound,...
Definition integral.h:272
std::pair< std::array< double, 6 >, std::array< double, 6 > > GetGaussLegendrePoints< 6 >()
Definition integral.h:123
std::pair< std::array< double, 3 >, std::array< double, 3 > > GetGaussLegendrePoints< 3 >()
Definition integral.h:69
double IntegrateByTrapezoidal(const std::vector< double > &func, const double dx, const std::size_t nsteps)
Definition integral.cc:42
std::pair< std::array< double, 9 >, std::array< double, 9 > > GetGaussLegendrePoints< 9 >()
Definition integral.h:195
std::pair< std::array< double, 5 >, std::array< double, 5 > > GetGaussLegendrePoints< 5 >()
Definition integral.h:103
double IntegrateBySimpson(const std::vector< double > &func, const double dx, const std::size_t nsteps)
Definition integral.cc:27
std::pair< std::array< double, 4 >, std::array< double, 4 > > GetGaussLegendrePoints< 4 >()
Definition integral.h:85
class register implement
Definition arena_queue.h:37