Apollo  6.0
Open source self driving car software
integral.h
Go to the documentation of this file.
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 <functional>
26 #include <utility>
27 #include <vector>
28 
33 namespace apollo {
34 namespace common {
35 namespace math {
36 
37 double IntegrateBySimpson(const std::vector<double>& funv_vec, const double dx,
38  const std::size_t nsteps);
39 
40 double IntegrateByTrapezoidal(const std::vector<double>& funv_vec,
41  const double dx, const std::size_t nsteps);
42 
48 template <std::size_t N>
49 std::pair<std::array<double, N>, std::array<double, N>>
51 
52 template <>
53 inline std::pair<std::array<double, 2>, std::array<double, 2>>
55  std::array<double, 2> x;
56  x[0] = -5.77350269189625764507e-01;
57  x[1] = 5.77350269189625764507e-01;
58 
59  std::array<double, 2> w;
60  w[0] = 1.0;
61  w[1] = 1.0;
62 
63  return std::make_pair(x, w);
64 }
65 
66 template <>
67 inline std::pair<std::array<double, 3>, std::array<double, 3>>
69  std::array<double, 3> x;
70  x[0] = 0.00000000000000000000e+00;
71  x[1] = 7.74596669241483377010e-01;
72  x[2] = -7.74596669241483377010e-01;
73 
74  std::array<double, 3> w;
75  w[0] = 8.88888888888888888877e-01;
76  w[1] = 5.55555555555555555562e-01;
77  w[2] = 5.55555555555555555562e-01;
78 
79  return std::make_pair(x, w);
80 }
81 
82 template <>
83 inline std::pair<std::array<double, 4>, std::array<double, 4>>
85  std::array<double, 4> x;
86  x[0] = 3.39981043584856264792e-01;
87  x[1] = -3.39981043584856264792e-01;
88  x[2] = 8.61136311594052575248e-01;
89  x[3] = -8.61136311594052575248e-01;
90 
91  std::array<double, 4> w;
92  w[0] = 6.52145154862546142644e-01;
93  w[1] = 6.52145154862546142644e-01;
94  w[2] = 3.47854845137453857383e-01;
95  w[3] = 3.47854845137453857383e-01;
96 
97  return std::make_pair(x, w);
98 }
99 
100 template <>
101 inline std::pair<std::array<double, 5>, std::array<double, 5>>
103  std::array<double, 5> x;
104  x[0] = 0.00000000000000000000e+00;
105  x[1] = 5.38469310105683091018e-01;
106  x[2] = -5.38469310105683091018e-01;
107  x[3] = 9.06179845938663992811e-01;
108  x[4] = -9.06179845938663992811e-01;
109 
110  std::array<double, 5> w;
111  w[0] = 5.68888888888888888883e-01;
112  w[1] = 4.78628670499366468030e-01;
113  w[2] = 4.78628670499366468030e-01;
114  w[3] = 2.36926885056189087515e-01;
115  w[4] = 2.36926885056189087515e-01;
116 
117  return std::make_pair(x, w);
118 }
119 
120 template <>
121 inline std::pair<std::array<double, 6>, std::array<double, 6>>
123  std::array<double, 6> x;
124  x[0] = 6.61209386466264513688e-01;
125  x[1] = -6.61209386466264513688e-01;
126  x[2] = 2.38619186083196908630e-01;
127  x[3] = -2.38619186083196908630e-01;
128  x[4] = 9.32469514203152027832e-01;
129  x[5] = -9.32469514203152027832e-01;
130 
131  std::array<double, 6> w;
132  w[0] = 3.60761573048138607569e-01;
133  w[1] = 3.60761573048138607569e-01;
134  w[2] = 4.67913934572691047389e-01;
135  w[3] = 4.67913934572691047389e-01;
136  w[4] = 1.71324492379170345043e-01;
137  w[5] = 1.71324492379170345043e-01;
138 
139  return std::make_pair(x, w);
140 }
141 
142 template <>
143 inline std::pair<std::array<double, 7>, std::array<double, 7>>
145  std::array<double, 7> x;
146  x[0] = 0.00000000000000000000e+00;
147  x[1] = 4.05845151377397166917e-01;
148  x[2] = -4.05845151377397166917e-01;
149  x[3] = 7.41531185599394439864e-01;
150  x[4] = -7.41531185599394439864e-01;
151  x[5] = 9.49107912342758524541e-01;
152  x[6] = -9.49107912342758524541e-01;
153 
154  std::array<double, 7> w;
155  w[0] = 4.17959183673469387749e-01;
156  w[1] = 3.81830050505118944961e-01;
157  w[2] = 3.81830050505118944961e-01;
158  w[3] = 2.79705391489276667890e-01;
159  w[4] = 2.79705391489276667890e-01;
160  w[5] = 1.29484966168869693274e-01;
161  w[6] = 1.29484966168869693274e-01;
162 
163  return std::make_pair(x, w);
164 }
165 
166 template <>
167 inline std::pair<std::array<double, 8>, std::array<double, 8>>
169  std::array<double, 8> x;
170  x[0] = 1.83434642495649804936e-01;
171  x[1] = -1.83434642495649804936e-01;
172  x[2] = 5.25532409916328985830e-01;
173  x[3] = -5.25532409916328985830e-01;
174  x[4] = 7.96666477413626739567e-01;
175  x[5] = -7.96666477413626739567e-01;
176  x[6] = 9.60289856497536231661e-01;
177  x[7] = -9.60289856497536231661e-01;
178 
179  std::array<double, 8> w;
180  w[0] = 3.62683783378361982976e-01;
181  w[1] = 3.62683783378361982976e-01;
182  w[2] = 3.13706645877887287338e-01;
183  w[3] = 3.13706645877887287338e-01;
184  w[4] = 2.22381034453374470546e-01;
185  w[5] = 2.22381034453374470546e-01;
186  w[6] = 1.01228536290376259154e-01;
187  w[7] = 1.01228536290376259154e-01;
188 
189  return std::make_pair(x, w);
190 }
191 
192 template <>
193 inline std::pair<std::array<double, 9>, std::array<double, 9>>
195  std::array<double, 9> x;
196  x[0] = 0.00000000000000000000e+00;
197  x[1] = 8.36031107326635794313e-01;
198  x[2] = -8.36031107326635794313e-01;
199  x[3] = 9.68160239507626089810e-01;
200  x[4] = -9.68160239507626089810e-01;
201  x[5] = 3.24253423403808929042e-01;
202  x[6] = -3.24253423403808929042e-01;
203  x[7] = 6.13371432700590397285e-01;
204  x[8] = -6.13371432700590397285e-01;
205 
206  std::array<double, 9> w;
207  w[0] = 3.30239355001259763154e-01;
208  w[1] = 1.80648160694857404059e-01;
209  w[2] = 1.80648160694857404059e-01;
210  w[3] = 8.12743883615744119737e-02;
211  w[4] = 8.12743883615744119737e-02;
212  w[5] = 3.12347077040002840057e-01;
213  w[6] = 3.12347077040002840057e-01;
214  w[7] = 2.60610696402935462313e-01;
215  w[8] = 2.60610696402935462313e-01;
216 
217  return std::make_pair(x, w);
218 }
219 
220 template <>
221 inline std::pair<std::array<double, 10>, std::array<double, 10>>
223  std::array<double, 10> x;
224  x[0] = 1.48874338981631210881e-01;
225  x[1] = -1.48874338981631210881e-01;
226  x[2] = 4.33395394129247190794e-01;
227  x[3] = -4.33395394129247190794e-01;
228  x[4] = 6.79409568299024406207e-01;
229  x[5] = -6.79409568299024406207e-01;
230  x[6] = 8.65063366688984510759e-01;
231  x[7] = -8.65063366688984510759e-01;
232  x[8] = 9.73906528517171720066e-01;
233  x[9] = -9.73906528517171720066e-01;
234 
235  std::array<double, 10> w;
236  w[0] = 2.95524224714752870187e-01;
237  w[1] = 2.95524224714752870187e-01;
238  w[2] = 2.69266719309996355105e-01;
239  w[3] = 2.69266719309996355105e-01;
240  w[4] = 2.19086362515982044000e-01;
241  w[5] = 2.19086362515982044000e-01;
242  w[6] = 1.49451349150580593150e-01;
243  w[7] = 1.49451349150580593150e-01;
244  w[8] = 6.66713443086881375920e-02;
245  w[9] = 6.66713443086881375920e-02;
246 
247  return std::make_pair(x, w);
248 }
249 
270 template <std::size_t N>
271 double IntegrateByGaussLegendre(const std::function<double(double)>& func,
272  const double lower_bound,
273  const double upper_bound) {
274  auto p = GetGaussLegendrePoints<N>();
275 
276  std::array<double, N> x = p.first;
277  std::array<double, N> w = p.second;
278 
279  const double t = (upper_bound - lower_bound) * 0.5;
280  const double m = (upper_bound + lower_bound) * 0.5;
281 
282  double integral = 0.0;
283  for (size_t i = 0; i < N; ++i) {
284  integral += w[i] * func(t * x[i] + m);
285  }
286 
287  return integral * t;
288 }
289 
290 } // namespace math
291 } // namespace common
292 } // namespace apollo
void(* func)(void *)
Definition: routine_context.h:41
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
std::pair< std::array< double, 6 >, std::array< double, 6 > > GetGaussLegendrePoints< 6 >()
Definition: integral.h:122
std::pair< std::array< double, N >, std::array< double, N > > GetGaussLegendrePoints()
Get the points and weights for different ordered Gauss-Legendre integration. Currently support order ...
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:271
double IntegrateBySimpson(const std::vector< double > &funv_vec, const double dx, const std::size_t nsteps)
std::pair< std::array< double, 9 >, std::array< double, 9 > > GetGaussLegendrePoints< 9 >()
Definition: integral.h:194
std::pair< std::array< double, 5 >, std::array< double, 5 > > GetGaussLegendrePoints< 5 >()
Definition: integral.h:102
std::pair< std::array< double, 7 >, std::array< double, 7 > > GetGaussLegendrePoints< 7 >()
Definition: integral.h:144
std::pair< std::array< double, 2 >, std::array< double, 2 > > GetGaussLegendrePoints< 2 >()
Definition: integral.h:54
std::pair< std::array< double, 10 >, std::array< double, 10 > > GetGaussLegendrePoints< 10 >()
Definition: integral.h:222
std::pair< std::array< double, 3 >, std::array< double, 3 > > GetGaussLegendrePoints< 3 >()
Definition: integral.h:68
double IntegrateByTrapezoidal(const std::vector< double > &funv_vec, const double dx, const std::size_t nsteps)
std::pair< std::array< double, 4 >, std::array< double, 4 > > GetGaussLegendrePoints< 4 >()
Definition: integral.h:84
std::pair< std::array< double, 8 >, std::array< double, 8 > > GetGaussLegendrePoints< 8 >()
Definition: integral.h:168