Code

update 2geom
[inkscape.git] / src / 2geom / sbasis.h
1 /*
2  *  sbasis.h - S-power basis function class
3  *
4  *  Authors:
5  *   Nathan Hurst <njh@mail.csse.monash.edu.au>
6  *   Michael Sloan <mgsloan@gmail.com>
7  *
8  * Copyright (C) 2006-2007 authors
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it either under the terms of the GNU Lesser General Public
12  * License version 2.1 as published by the Free Software Foundation
13  * (the "LGPL") or, at your option, under the terms of the Mozilla
14  * Public License Version 1.1 (the "MPL"). If you do not alter this
15  * notice, a recipient may use your version of this file under either
16  * the MPL or the LGPL.
17  *
18  * You should have received a copy of the LGPL along with this library
19  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  * You should have received a copy of the MPL along with this library
22  * in the file COPYING-MPL-1.1
23  *
24  * The contents of this file are subject to the Mozilla Public License
25  * Version 1.1 (the "License"); you may not use this file except in
26  * compliance with the License. You may obtain a copy of the License at
27  * http://www.mozilla.org/MPL/
28  *
29  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
30  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
31  * the specific language governing rights and limitations.
32  */
34 #ifndef SEEN_SBASIS_H
35 #define SEEN_SBASIS_H
36 #include <vector>
37 #include <cassert>
38 #include <iostream>
40 #include "linear.h"
41 #include "interval.h"
42 #include "utils.h"
43 #include "exception.h"
45 namespace Geom {
47 /*** An empty SBasis is identically 0. */
48 class SBasis : public std::vector<Linear>{
49 public:
50     SBasis() {}
51     explicit SBasis(double a) {
52         push_back(Linear(a,a));
53     }
54     SBasis(SBasis const & a) :
55         std::vector<Linear>(a)
56     {}
57     SBasis(Linear const & bo) {
58         push_back(bo);
59     }
60     SBasis(Linear* bo) {
61         push_back(*bo);
62     }
64     //IMPL: FragmentConcept
65     typedef double output_type;
66     inline bool isZero() const {
67         if(empty()) return true;
68         for(unsigned i = 0; i < size(); i++) {
69             if(!(*this)[i].isZero()) return false;
70         }
71         return true;
72     }
73     inline bool isConstant() const {
74         if (empty()) return true;
75         for (unsigned i = 0; i < size(); i++) {
76             if(!(*this)[i].isConstant()) return false;
77         }
78         return true;
79     }
81     bool isFinite() const;
82     inline double at0() const { 
83         if(empty()) return 0; else return (*this)[0][0];
84     }
85     inline double at1() const{
86         if(empty()) return 0; else return (*this)[0][1];
87     }
89     double valueAt(double t) const {
90         double s = t*(1-t);
91         double p0 = 0, p1 = 0;
92         for(unsigned k = size(); k > 0; k--) {
93             const Linear &lin = (*this)[k-1];
94             p0 = p0*s + lin[0];
95             p1 = p1*s + lin[1];
96         }
97         return (1-t)*p0 + t*p1;
98     }
99     //double valueAndDerivative(double t, double &der) const {
100     //}
101     double operator()(double t) const {
102         return valueAt(t);
103     }
105     std::vector<double> valueAndDerivatives(double t, unsigned n) const;
107     SBasis toSBasis() const { return SBasis(*this); }
109     double tailError(unsigned tail) const;
111 // compute f(g)
112     SBasis operator()(SBasis const & g) const;
114     Linear operator[](unsigned i) const {
115         assert(i < size());
116         return std::vector<Linear>::operator[](i);
117     }
119 //MUTATOR PRISON
120     Linear& operator[](unsigned i) { return this->at(i); }
122     //remove extra zeros
123     void normalize() {
124         while(!empty() && 0 == back()[0] && 0 == back()[1])
125             pop_back();
126     }
128     void truncate(unsigned k) { if(k < size()) resize(k); }
129 private:
130     void derive(); // in place version
131 };
133 //TODO: figure out how to stick this in linear, while not adding an sbasis dep
134 inline SBasis Linear::toSBasis() const { return SBasis(*this); }
136 //implemented in sbasis-roots.cpp
137 Interval bounds_exact(SBasis const &a);
138 Interval bounds_fast(SBasis const &a, int order = 0);
139 Interval bounds_local(SBasis const &a, const Interval &t, int order = 0);
141 inline SBasis reverse(SBasis const &a) {
142     SBasis result;
143     result.reserve(a.size());
144     for(unsigned k = 0; k < a.size(); k++)
145        result.push_back(reverse(a[k]));
146     return result;
149 //IMPL: ScalableConcept
150 inline SBasis operator-(const SBasis& p) {
151     if(p.isZero()) return SBasis();
152     SBasis result;
153     result.reserve(p.size());
154         
155     for(unsigned i = 0; i < p.size(); i++) {
156         result.push_back(-p[i]);
157     }
158     return result;
160 SBasis operator*(SBasis const &a, double k);
161 inline SBasis operator*(double k, SBasis const &a) { return a*k; }
162 inline SBasis operator/(SBasis const &a, double k) { return a*(1./k); }
163 SBasis& operator*=(SBasis& a, double b);
164 inline SBasis& operator/=(SBasis& a, double b) { return (a*=(1./b)); }
166 //IMPL: AddableConcept
167 SBasis operator+(const SBasis& a, const SBasis& b);
168 SBasis operator-(const SBasis& a, const SBasis& b);
169 SBasis& operator+=(SBasis& a, const SBasis& b);
170 SBasis& operator-=(SBasis& a, const SBasis& b);
172 //TODO: remove?
173 inline SBasis operator+(const SBasis & a, Linear const & b) {
174     if(b.isZero()) return a;
175     if(a.isZero()) return b;
176     SBasis result(a);
177     result[0] += b;
178     return result;
180 inline SBasis operator-(const SBasis & a, Linear const & b) {
181     if(b.isZero()) return a;
182     SBasis result(a);
183     result[0] -= b;
184     return result;
186 inline SBasis& operator+=(SBasis& a, const Linear& b) {
187     if(a.isZero())
188         a.push_back(b);
189     else
190         a[0] += b;
191     return a;
193 inline SBasis& operator-=(SBasis& a, const Linear& b) {
194     if(a.isZero())
195         a.push_back(-b);
196     else
197         a[0] -= b;
198     return a;
201 //IMPL: OffsetableConcept
202 inline SBasis operator+(const SBasis & a, double b) {
203     if(a.isZero()) return Linear(b, b);
204     SBasis result(a);
205     result[0] += b;
206     return result;
208 inline SBasis operator-(const SBasis & a, double b) {
209     if(a.isZero()) return Linear(-b, -b);
210     SBasis result(a);
211     result[0] -= b;
212     return result;
214 inline SBasis& operator+=(SBasis& a, double b) {
215     if(a.isZero())
216         a.push_back(Linear(b,b));
217     else
218         a[0] += b;
219     return a;
221 inline SBasis& operator-=(SBasis& a, double b) {
222     if(a.isZero())
223         a.push_back(Linear(-b,-b));
224     else
225         a[0] -= b;
226     return a;
229 SBasis shift(SBasis const &a, int sh);
230 SBasis shift(Linear const &a, int sh);
232 inline SBasis truncate(SBasis const &a, unsigned terms) {
233     SBasis c;
234     c.insert(c.begin(), a.begin(), a.begin() + std::min(terms, (unsigned)a.size()));
235     return c;
238 SBasis multiply(SBasis const &a, SBasis const &b);
239 // This performs a multiply and accumulate operation in about the same time as multiply.  return a*b + c
240 SBasis multiply_add(SBasis const &a, SBasis const &b, SBasis c);
242 SBasis integral(SBasis const &c);
243 SBasis derivative(SBasis const &a);
245 SBasis sqrt(SBasis const &a, int k);
247 // return a kth order approx to 1/a)
248 SBasis reciprocal(Linear const &a, int k);
249 SBasis divide(SBasis const &a, SBasis const &b, int k);
251 inline SBasis operator*(SBasis const & a, SBasis const & b) {
252     return multiply(a, b);
255 inline SBasis& operator*=(SBasis& a, SBasis const & b) {
256     a = multiply(a, b);
257     return a;
260 //valuation: degree of the first non zero coefficient.
261 inline unsigned 
262 valuation(SBasis const &a, double tol=0){
263     unsigned val=0;
264     while( val<a.size() &&
265            fabs(a[val][0])<tol &&
266            fabs(a[val][1])<tol ) 
267         val++;
268     return val;
271 // a(b(t))
272 SBasis compose(SBasis const &a, SBasis const &b);
273 SBasis compose(SBasis const &a, SBasis const &b, unsigned k);
274 SBasis inverse(SBasis a, int k);
275 //compose_inverse(f,g)=compose(f,inverse(g)), but is numerically more stable in some good cases...
276 //TODO: requires g(0)=0 & g(1)=1 atm. generalization should be obvious.
277 SBasis compose_inverse(SBasis const &f, SBasis const &g, unsigned order=2, double tol=1e-3);
279 inline SBasis portion(const SBasis &t, double from, double to) { return compose(t, Linear(from, to)); }
281 // compute f(g)
282 inline SBasis
283 SBasis::operator()(SBasis const & g) const {
284     return compose(*this, g);
286  
287 inline std::ostream &operator<< (std::ostream &out_file, const Linear &bo) {
288     out_file << "{" << bo[0] << ", " << bo[1] << "}";
289     return out_file;
292 inline std::ostream &operator<< (std::ostream &out_file, const SBasis & p) {
293     for(unsigned i = 0; i < p.size(); i++) {
294         out_file << p[i] << "s^" << i << " + ";
295     }
296     return out_file;
299 // These are deprecated, use sbasis-math versions if possible
300 SBasis sin(Linear bo, int k);
301 SBasis cos(Linear bo, int k);
303 std::vector<double> roots(SBasis const & s);
304 std::vector<std::vector<double> > multi_roots(SBasis const &f,
305                                  std::vector<double> const &levels,
306                                  double htol=1e-7,
307                                  double vtol=1e-7,
308                                  double a=0,
309                                  double b=1);
310     
313 /*
314   Local Variables:
315   mode:c++
316   c-file-style:"stroustrup"
317   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
318   indent-tabs-mode:nil
319   fill-column:99
320   End:
321 */
322 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
323 #endif