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 }
61 //IMPL: FragmentConcept
62 typedef double output_type;
63 inline bool isZero() const {
64 if(empty()) return true;
65 for(unsigned i = 0; i < size(); i++) {
66 if(!(*this)[i].isZero()) return false;
67 }
68 return true;
69 }
70 inline bool isConstant() const {
71 if (empty()) return true;
72 for (unsigned i = 0; i < size(); i++) {
73 if(!(*this)[i].isConstant()) return false;
74 }
75 return true;
76 }
78 bool isFinite() const;
79 inline double at0() const {
80 if(empty()) return 0; else return (*this)[0][0];
81 }
82 inline double at1() const{
83 if(empty()) return 0; else return (*this)[0][1];
84 }
86 double valueAt(double t) const {
87 double s = t*(1-t);
88 double p0 = 0, p1 = 0;
89 double sk = 1;
90 //TODO: rewrite as horner
91 for(unsigned k = 0; k < size(); k++) {
92 p0 += sk*(*this)[k][0];
93 p1 += sk*(*this)[k][1];
94 sk *= s;
95 }
96 return (1-t)*p0 + t*p1;
97 }
98 double valueAndDerivative(double t, double &der) const {
99 double s = t*(1-t);
100 double p0 = 0, p1 = 0;
101 double sk = 1;
102 //TODO: rewrite as horner
103 for(unsigned k = 0; k < size(); k++) {
104 p0 += sk*(*this)[k][0];
105 p1 += sk*(*this)[k][1];
106 sk *= s;
107 }
108 // p0 and p1 at this point form a linear approximation at t
109 der = p1 - p0;
110 return (1-t)*p0 + t*p1;
111 }
112 double operator()(double t) const {
113 return valueAt(t);
114 }
116 std::vector<double> valueAndDerivatives(double t, unsigned n) const {
117 std::vector<double> ret;
118 if(n==1) {
119 ret.push_back(valueAt(t));
120 return ret;
121 }
122 if(n==2) {
123 double der;
124 ret.push_back(valueAndDerivative(t, der));
125 ret.push_back(der);
126 return ret;
127 }
128 //TODO
129 throwNotImplemented();
130 }
132 SBasis toSBasis() const { return SBasis(*this); }
134 double tailError(unsigned tail) const;
136 // compute f(g)
137 SBasis operator()(SBasis const & g) const;
139 Linear operator[](unsigned i) const {
140 assert(i < size());
141 return std::vector<Linear>::operator[](i);
142 }
144 //MUTATOR PRISON
145 Linear& operator[](unsigned i) { return this->at(i); }
147 //remove extra zeros
148 void normalize() {
149 while(!empty() && 0 == back()[0] && 0 == back()[1])
150 pop_back();
151 }
152 void truncate(unsigned k) { if(k < size()) resize(k); }
153 };
155 //TODO: figure out how to stick this in linear, while not adding an sbasis dep
156 inline SBasis Linear::toSBasis() const { return SBasis(*this); }
158 //implemented in sbasis-roots.cpp
159 Interval bounds_exact(SBasis const &a);
160 Interval bounds_fast(SBasis const &a, int order = 0);
161 Interval bounds_local(SBasis const &a, const Interval &t, int order = 0);
163 inline SBasis reverse(SBasis const &a) {
164 SBasis result;
165 result.reserve(a.size());
166 for(unsigned k = 0; k < a.size(); k++)
167 result.push_back(reverse(a[k]));
168 return result;
169 }
171 //IMPL: ScalableConcept
172 inline SBasis operator-(const SBasis& p) {
173 if(p.isZero()) return SBasis();
174 SBasis result;
175 result.reserve(p.size());
177 for(unsigned i = 0; i < p.size(); i++) {
178 result.push_back(-p[i]);
179 }
180 return result;
181 }
182 SBasis operator*(SBasis const &a, double k);
183 inline SBasis operator*(double k, SBasis const &a) { return a*k; }
184 inline SBasis operator/(SBasis const &a, double k) { return a*(1./k); }
185 SBasis& operator*=(SBasis& a, double b);
186 inline SBasis& operator/=(SBasis& a, double b) { return (a*=(1./b)); }
188 //IMPL: AddableConcept
189 SBasis operator+(const SBasis& a, const SBasis& b);
190 SBasis operator-(const SBasis& a, const SBasis& b);
191 SBasis& operator+=(SBasis& a, const SBasis& b);
192 SBasis& operator-=(SBasis& a, const SBasis& b);
194 //TODO: remove?
195 inline SBasis operator+(const SBasis & a, Linear const & b) {
196 if(b.isZero()) return a;
197 if(a.isZero()) return b;
198 SBasis result(a);
199 result[0] += b;
200 return result;
201 }
202 inline SBasis operator-(const SBasis & a, Linear const & b) {
203 if(b.isZero()) return a;
204 SBasis result(a);
205 result[0] -= b;
206 return result;
207 }
208 inline SBasis& operator+=(SBasis& a, const Linear& b) {
209 if(a.isZero())
210 a.push_back(b);
211 else
212 a[0] += b;
213 return a;
214 }
215 inline SBasis& operator-=(SBasis& a, const Linear& b) {
216 if(a.isZero())
217 a.push_back(-b);
218 else
219 a[0] -= b;
220 return a;
221 }
223 //IMPL: OffsetableConcept
224 inline SBasis operator+(const SBasis & a, double b) {
225 if(a.isZero()) return Linear(b, b);
226 SBasis result(a);
227 result[0] += b;
228 return result;
229 }
230 inline SBasis operator-(const SBasis & a, double b) {
231 if(a.isZero()) return Linear(-b, -b);
232 SBasis result(a);
233 result[0] -= b;
234 return result;
235 }
236 inline SBasis& operator+=(SBasis& a, double b) {
237 if(a.isZero())
238 a.push_back(Linear(b,b));
239 else
240 a[0] += b;
241 return a;
242 }
243 inline SBasis& operator-=(SBasis& a, double b) {
244 if(a.isZero())
245 a.push_back(Linear(-b,-b));
246 else
247 a[0] -= b;
248 return a;
249 }
251 SBasis shift(SBasis const &a, int sh);
252 SBasis shift(Linear const &a, int sh);
254 inline SBasis truncate(SBasis const &a, unsigned terms) {
255 SBasis c;
256 c.insert(c.begin(), a.begin(), a.begin() + std::min(terms, (unsigned)a.size()));
257 return c;
258 }
260 SBasis multiply(SBasis const &a, SBasis const &b);
262 SBasis integral(SBasis const &c);
263 SBasis derivative(SBasis const &a);
265 SBasis sqrt(SBasis const &a, int k);
267 // return a kth order approx to 1/a)
268 SBasis reciprocal(Linear const &a, int k);
269 SBasis divide(SBasis const &a, SBasis const &b, int k);
271 inline SBasis operator*(SBasis const & a, SBasis const & b) {
272 return multiply(a, b);
273 }
275 inline SBasis& operator*=(SBasis& a, SBasis const & b) {
276 a = multiply(a, b);
277 return a;
278 }
280 //valuation: degree of the first non zero coefficient.
281 inline unsigned
282 valuation(SBasis const &a, double tol=0){
283 unsigned val=0;
284 while( val<a.size() &&
285 fabs(a[val][0])<tol &&
286 fabs(a[val][1])<tol )
287 val++;
288 return val;
289 }
291 // a(b(t))
292 SBasis compose(SBasis const &a, SBasis const &b);
293 SBasis compose(SBasis const &a, SBasis const &b, unsigned k);
294 SBasis inverse(SBasis a, int k);
295 //compose_inverse(f,g)=compose(f,inverse(g)), but is numerically more stable in some good cases...
296 //TODO: requires g(0)=0 & g(1)=1 atm. generalization should be obvious.
297 SBasis compose_inverse(SBasis const &f, SBasis const &g, unsigned order=2, double tol=1e-3);
299 inline SBasis portion(const SBasis &t, double from, double to) { return compose(t, Linear(from, to)); }
301 // compute f(g)
302 inline SBasis
303 SBasis::operator()(SBasis const & g) const {
304 return compose(*this, g);
305 }
307 inline std::ostream &operator<< (std::ostream &out_file, const Linear &bo) {
308 out_file << "{" << bo[0] << ", " << bo[1] << "}";
309 return out_file;
310 }
312 inline std::ostream &operator<< (std::ostream &out_file, const SBasis & p) {
313 for(unsigned i = 0; i < p.size(); i++) {
314 out_file << p[i] << "s^" << i << " + ";
315 }
316 return out_file;
317 }
319 SBasis sin(Linear bo, int k);
320 SBasis cos(Linear bo, int k);
322 std::vector<double> roots(SBasis const & s);
323 std::vector<std::vector<double> > multi_roots(SBasis const &f,
324 std::vector<double> const &levels,
325 double htol=1e-7,
326 double vtol=1e-7,
327 double a=0,
328 double b=1);
330 }
332 /*
333 Local Variables:
334 mode:c++
335 c-file-style:"stroustrup"
336 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
337 indent-tabs-mode:nil
338 fill-column:99
339 End:
340 */
341 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
342 #endif