GCC Code Coverage Report


Directory: ./
File: include/2geom/polynomial.h
Date: 2024-03-18 17:01:34
Exec Total Coverage
Lines: 26 52 50.0%
Functions: 7 11 63.6%
Branches: 17 40 42.5%

Line Branch Exec Source
1 /**
2 * \file
3 * \brief Polynomial in canonical (monomial) basis
4 *//*
5 * Authors:
6 * MenTaLguY <mental@rydia.net>
7 * Krzysztof Kosiński <tweenk.pl@gmail.com>
8 * Rafał Siejakowski <rs@rs-math.net>
9 *
10 * Copyright 2007-2015 Authors
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it either under the terms of the GNU Lesser General Public
14 * License version 2.1 as published by the Free Software Foundation
15 * (the "LGPL") or, at your option, under the terms of the Mozilla
16 * Public License Version 1.1 (the "MPL"). If you do not alter this
17 * notice, a recipient may use your version of this file under either
18 * the MPL or the LGPL.
19 *
20 * You should have received a copy of the LGPL along with this library
21 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * You should have received a copy of the MPL along with this library
24 * in the file COPYING-MPL-1.1
25 *
26 * The contents of this file are subject to the Mozilla Public License
27 * Version 1.1 (the "License"); you may not use this file except in
28 * compliance with the License. You may obtain a copy of the License at
29 * http://www.mozilla.org/MPL/
30 *
31 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
32 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
33 * the specific language governing rights and limitations.
34 */
35
36 #ifndef LIB2GEOM_SEEN_POLY_H
37 #define LIB2GEOM_SEEN_POLY_H
38 #include <assert.h>
39 #include <vector>
40 #include <iostream>
41 #include <algorithm>
42 #include <complex>
43 #include <2geom/coord.h>
44 #include <2geom/utils.h>
45
46 namespace Geom {
47
48 /** @brief Polynomial in canonical (monomial) basis.
49 * @ingroup Fragments */
50 class Poly : public std::vector<double>{
51 public:
52 // coeff; // sum x^i*coeff[i]
53
54 //unsigned size() const { return coeff.size();}
55 424 unsigned degree() const { return size()-1;}
56
57 //double operator[](const int i) const { return (*this)[i];}
58 //double& operator[](const int i) { return (*this)[i];}
59
60 184 Poly operator+(const Poly& p) const {
61 184 Poly result;
62 184 const unsigned out_size = std::max(size(), p.size());
63 184 const unsigned min_size = std::min(size(), p.size());
64
1/2
✓ Branch 1 taken 184 times.
✗ Branch 2 not taken.
184 result.reserve(out_size);
65
66
2/2
✓ Branch 0 taken 358 times.
✓ Branch 1 taken 184 times.
542 for(unsigned i = 0; i < min_size; i++) {
67
1/2
✓ Branch 4 taken 358 times.
✗ Branch 5 not taken.
358 result.push_back((*this)[i] + p[i]);
68 }
69
2/2
✓ Branch 1 taken 1608 times.
✓ Branch 2 taken 184 times.
1792 for(unsigned i = min_size; i < size(); i++)
70
1/2
✓ Branch 2 taken 1608 times.
✗ Branch 3 not taken.
1608 result.push_back((*this)[i]);
71
2/2
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 184 times.
217 for(unsigned i = min_size; i < p.size(); i++)
72
1/2
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
33 result.push_back(p[i]);
73
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 184 times.
184 assert(result.size() == out_size);
74 184 return result;
75 }
76 Poly operator-(const Poly& p) const {
77 Poly result;
78 const unsigned out_size = std::max(size(), p.size());
79 const unsigned min_size = std::min(size(), p.size());
80 result.reserve(out_size);
81
82 for(unsigned i = 0; i < min_size; i++) {
83 result.push_back((*this)[i] - p[i]);
84 }
85 for(unsigned i = min_size; i < size(); i++)
86 result.push_back((*this)[i]);
87 for(unsigned i = min_size; i < p.size(); i++)
88 result.push_back(-p[i]);
89 assert(result.size() == out_size);
90 return result;
91 }
92 Poly operator-=(const Poly& p) {
93 const unsigned out_size = std::max(size(), p.size());
94 const unsigned min_size = std::min(size(), p.size());
95 resize(out_size);
96
97 for(unsigned i = 0; i < min_size; i++) {
98 (*this)[i] -= p[i];
99 }
100 for(unsigned i = min_size; i < out_size; i++)
101 (*this)[i] = -p[i];
102 return *this;
103 }
104 Poly operator-(const double k) const {
105 Poly result;
106 const unsigned out_size = size();
107 result.reserve(out_size);
108
109 for(unsigned i = 0; i < out_size; i++) {
110 result.push_back((*this)[i]);
111 }
112 result[0] -= k;
113 return result;
114 }
115 Poly operator-() const {
116 Poly result;
117 result.resize(size());
118
119 for(unsigned i = 0; i < size(); i++) {
120 result[i] = -(*this)[i];
121 }
122 return result;
123 }
124 174 Poly operator*(const double p) const {
125 174 Poly result;
126 174 const unsigned out_size = size();
127
1/2
✓ Branch 1 taken 174 times.
✗ Branch 2 not taken.
174 result.reserve(out_size);
128
129
2/2
✓ Branch 0 taken 348 times.
✓ Branch 1 taken 174 times.
522 for(unsigned i = 0; i < out_size; i++) {
130
1/2
✓ Branch 3 taken 348 times.
✗ Branch 4 not taken.
348 result.push_back((*this)[i]*p);
131 }
132
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 174 times.
174 assert(result.size() == out_size);
133 174 return result;
134 }
135 // equivalent to multiply by x^terms, negative terms are disallowed
136 Poly shifted(unsigned const terms) const {
137 Poly result;
138 size_type const out_size = size() + terms;
139 result.reserve(out_size);
140
141 result.resize(terms, 0.0);
142 result.insert(result.end(), this->begin(), this->end());
143
144 assert(result.size() == out_size);
145 return result;
146 }
147 Poly operator*(const Poly& p) const;
148
149 template <typename T>
150 T eval(T x) const {
151 T r = 0;
152 for(int k = size()-1; k >= 0; k--) {
153 r = r*x + T((*this)[k]);
154 }
155 return r;
156 }
157
158 template <typename T>
159 T operator()(T t) const { return (T)eval(t);}
160
161 void normalize();
162
163 void monicify();
164 37 Poly() {}
165 52 Poly(const Poly& p) : std::vector<double>(p) {}
166
1/2
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
10 Poly(const double a) {push_back(a);}
167
168 public:
169 template <class T, class U>
170 void val_and_deriv(T x, U &pd) const {
171 pd[0] = back();
172 int nc = size() - 1;
173 int nd = pd.size() - 1;
174 for(unsigned j = 1; j < pd.size(); j++)
175 pd[j] = 0.0;
176 for(int i = nc -1; i >= 0; i--) {
177 int nnd = std::min(nd, nc-i);
178 for(int j = nnd; j >= 1; j--)
179 pd[j] = pd[j]*x + operator[](i);
180 pd[0] = pd[0]*x + operator[](i);
181 }
182 double cnst = 1;
183 for(int i = 2; i <= nd; i++) {
184 cnst *= i;
185 pd[i] *= cnst;
186 }
187 }
188
189 static Poly linear(double ax, double b) {
190 Poly p;
191 p.push_back(b);
192 p.push_back(ax);
193 return p;
194 }
195 };
196
197 174 inline Poly operator*(double a, Poly const & b) { return b * a;}
198
199 Poly integral(Poly const & p);
200 Poly derivative(Poly const & p);
201 Poly divide_out_root(Poly const & p, double x);
202 Poly compose(Poly const & a, Poly const & b);
203 Poly divide(Poly const &a, Poly const &b, Poly &r);
204 Poly gcd(Poly const &a, Poly const &b, const double tol=1e-10);
205
206 /*** solve(Poly p)
207 * find all p.degree() roots of p.
208 * This function can take a long time with suitably crafted polynomials, but in practice it should be fast. Should we provide special forms for degree() <= 4?
209 */
210 std::vector<std::complex<double> > solve(const Poly & p);
211
212 #ifdef HAVE_GSL
213 /*** solve_reals(Poly p)
214 * find all real solutions to Poly p.
215 * currently we just use solve and pick out the suitably real looking values, there may be a better algorithm.
216 */
217 std::vector<double> solve_reals(const Poly & p);
218 #endif
219 double polish_root(Poly const & p, double guess, double tol);
220
221
222 /** @brief Analytically solve quadratic equation.
223 * The equation is given in the standard form: ax^2 + bx + c = 0.
224 * Only real roots are returned. */
225 std::vector<Coord> solve_quadratic(Coord a, Coord b, Coord c);
226
227 /** @brief Analytically solve cubic equation.
228 * The equation is given in the standard form: ax^3 + bx^2 + cx + d = 0.
229 * Only real roots are returned. */
230 std::vector<Coord> solve_cubic(Coord a, Coord b, Coord c, Coord d);
231
232 /** @brief Analytically solve quartic equation.
233 * The equation is given in the standard form: ax^4 + bx^3 + cx^2 + dx + e = 0.
234 * Only real roots are returned. */
235 std::vector<Coord> solve_quartic(Coord a, Coord b, Coord c, Coord d, Coord e);
236
237 inline std::ostream &operator<< (std::ostream &out_file, const Poly &in_poly) {
238 if(in_poly.size() == 0)
239 out_file << "0";
240 else {
241 for(int i = (int)in_poly.size()-1; i >= 0; --i) {
242 if(i == 1) {
243 out_file << "" << in_poly[i] << "*x";
244 out_file << " + ";
245 } else if(i) {
246 out_file << "" << in_poly[i] << "*x^" << i;
247 out_file << " + ";
248 } else
249 out_file << in_poly[i];
250
251 }
252 }
253 return out_file;
254 }
255
256 } // namespace Geom
257
258 #endif //LIB2GEOM_SEEN_POLY_H
259
260 /*
261 Local Variables:
262 mode:c++
263 c-file-style:"stroustrup"
264 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
265 indent-tabs-mode:nil
266 fill-column:99
267 End:
268 */
269 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
270