Apollo 10.0
自动驾驶开放平台
polynomial.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 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 *****************************************************************************/
17
18namespace apollo {
19namespace perception {
20namespace base {
21
22Polynomial::Polynomial() { coeff_[0] = 0.0; }
24
25const std::map<uint32_t, double>& Polynomial::getCoeff() const {
26 return coeff_;
27}
28
29double& Polynomial::operator[](const uint32_t& order) {
30 initialized_ = false;
31 return coeff_[order];
32}
33
34double Polynomial::operator()(const double& x) {
35 if (!initialized_) {
36 index_gap_.resize(coeff_.size() - 1, 0);
37 auto it = coeff_.begin();
38 auto it_previous = coeff_.begin();
39 ++it;
40 size_t idx = 0;
41 for (; it != coeff_.end(); ++it, ++it_previous) {
42 const uint32_t& order = it->first;
43 const uint32_t& order_pre = it_previous->first;
44 uint32_t gap = order - order_pre;
45 index_gap_[idx] = gap;
46 ++idx;
47 power_cache_[gap] = 0.0;
48 }
49 initialized_ = true;
50 }
51
52 for (auto& item : power_cache_) {
53 item.second = x;
54 for (size_t i = 0; i < item.first - 1; ++i) {
55 item.second *= x;
56 }
57 }
58
59 auto r_it = coeff_.rbegin();
60 double sum = r_it->second;
61 auto it_gap = index_gap_.rbegin();
62 while (it_gap != index_gap_.rend()) {
63 ++r_it;
64 sum = power_cache_[*it_gap] * sum + r_it->second;
65 ++it_gap;
66 }
67
68 return sum;
69}
70
71std::ostream& operator<<(std::ostream& o, const Polynomial& p) {
72 const std::map<uint32_t, double>& coeff = p.getCoeff();
73 size_t i = 0;
74 const size_t coeff_num = coeff.size();
75 for (auto it = coeff.rbegin(); it != coeff.rend(); ++it) {
76 const uint32_t order = it->first;
77 const double c = it->second;
78
79 o << "(" << c << ")*(t^" << order << ")";
80 if (i < coeff_num - 1) {
81 o << " + ";
82 }
83 ++i;
84 }
85 return o;
86}
87
88} // namespace base
89} // namespace perception
90} // namespace apollo
const std::map< uint32_t, double > & getCoeff() const
Definition polynomial.cc:25
double operator()(const double &x)
Definition polynomial.cc:34
double & operator[](const uint32_t &order)
Definition polynomial.cc:29
std::ostream & operator<<(std::ostream &o, const Polynomial &p)
Definition polynomial.cc:71
class register implement
Definition arena_queue.h:37