sbasis.h revision 07bda0b13ae048815f53f21ad1edbe3cc1b7e4e8
01d27eab5fca2dcb8e883011f8be77ae6b78a11cTed Gould/**
01d27eab5fca2dcb8e883011f8be77ae6b78a11cTed Gould * \file
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * \brief Defines S-power basis function class
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński *
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Authors:
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Nathan Hurst <njh@mail.csse.monash.edu.au>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Michael Sloan <mgsloan@gmail.com>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen *
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Copyright (C) 2006-2007 authors
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen *
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * This library is free software; you can redistribute it and/or
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * modify it either under the terms of the GNU Lesser General Public
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * License version 2.1 as published by the Free Software Foundation
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * (the "LGPL") or, at your option, under the terms of the Mozilla
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Public License Version 1.1 (the "MPL"). If you do not alter this
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * notice, a recipient may use your version of this file under either
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * the MPL or the LGPL.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen *
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * You should have received a copy of the LGPL along with this library
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * in the file COPYING-LGPL-2.1; if not, write to the Free Software
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * You should have received a copy of the MPL along with this library
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * in the file COPYING-MPL-1.1
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen *
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * The contents of this file are subject to the Mozilla Public License
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Version 1.1 (the "License"); you may not use this file except in
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * compliance with the License. You may obtain a copy of the License at
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * http://www.mozilla.org/MPL/
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen *
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * OF ANY KIND, either express or implied. See the LGPL or the MPL for
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * the specific language governing rights and limitations.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen */
8001ba81cb851b38d86650a2fef5817facffb763johanengelen
8001ba81cb851b38d86650a2fef5817facffb763johanengelen#ifndef SEEN_SBASIS_H
8001ba81cb851b38d86650a2fef5817facffb763johanengelen#define SEEN_SBASIS_H
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include <vector>
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński#include <cassert>
29684a16b6c92bee28a94fdc2607bcc143950fa8johanengelen#include <iostream>
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński
6c3e745a94ef6b25a4ef9f018d350a7535aa45afTed Gould#include <2geom/linear.h>
e4369b05aaa20df73a37f4d319ce456865cc74f3Krzysztof Kosiński#include <2geom/interval.h>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include <2geom/utils.h>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include <2geom/exception.h>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen//#define USE_SBASISN 1
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#if defined(USE_SBASIS_OF)
6c3e745a94ef6b25a4ef9f018d350a7535aa45afTed Gould
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "sbasis-of.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#elif defined(USE_SBASISN)
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
6c3e745a94ef6b25a4ef9f018d350a7535aa45afTed Gould#include "sbasisN.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelennamespace Geom{
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen/*** An empty SBasis is identically 0. */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenclass SBasis : public SBasisN<1>;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen};
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#else
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelennamespace Geom{
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen/**
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen* \brief S-power basis function class
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen*
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen* An empty SBasis is identically 0. */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenclass SBasis{
29684a16b6c92bee28a94fdc2607bcc143950fa8johanengelen std::vector<Linear> d;
29684a16b6c92bee28a94fdc2607bcc143950fa8johanengelen void push_back(Linear const&l) { d.push_back(l); }
29684a16b6c92bee28a94fdc2607bcc143950fa8johanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenpublic:
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // As part of our migration away from SBasis isa vector we provide this minimal set of vector interface methods.
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński size_t size() const {return d.size();}
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński Linear operator[](unsigned i) const {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return d[i];
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen Linear& operator[](unsigned i) { return d.at(i); }
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński Linear const* begin() const { return (Linear const*)&*d.begin();}
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński Linear const* end() const { return (Linear const*)&*d.end();}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen Linear* begin() { return (Linear*)&*d.begin();}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen Linear* end() { return (Linear*)&*d.end();}
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen bool empty() const {return d.empty();}
e4369b05aaa20df73a37f4d319ce456865cc74f3Krzysztof Kosiński Linear &back() {return d.back();}
e4369b05aaa20df73a37f4d319ce456865cc74f3Krzysztof Kosiński Linear const &back() const {return d.back();}
29684a16b6c92bee28a94fdc2607bcc143950fa8johanengelen void pop_back() { d.pop_back();}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen void resize(unsigned n) { d.resize(n);}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen void resize(unsigned n, Linear const& l) { d.resize(n, l);}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen void reserve(unsigned n) { d.reserve(n);}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen void clear() {d.clear();}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen void insert(Linear* before, const Linear* src_begin, const Linear* src_end) { d.insert(std::vector<Linear>::iterator(before), src_begin, src_end);}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen //void insert(Linear* aa, Linear* bb, Linear* cc} { d.insert(aa, bb, cc);}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen Linear& at(unsigned i) { return d.at(i);}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen //void insert(Linear* before, int& n, Linear const &l) { d.insert(std::vector<Linear>::iterator(before), n, l);}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen bool operator==(SBasis const&B) const { return d == B.d;}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen bool operator!=(SBasis const&B) const { return d != B.d;}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen operator std::vector<Linear>() { return d;}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen SBasis() {}
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński explicit SBasis(double a) {
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński push_back(Linear(a,a));
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński }
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński explicit SBasis(double a, double b) {
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński push_back(Linear(a,b));
e4369b05aaa20df73a37f4d319ce456865cc74f3Krzysztof Kosiński }
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński SBasis(SBasis const & a) :
e4369b05aaa20df73a37f4d319ce456865cc74f3Krzysztof Kosiński d(a.d)
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński {}
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński SBasis(std::vector<Linear> const & ls) :
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński d(ls)
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński {}
e4369b05aaa20df73a37f4d319ce456865cc74f3Krzysztof Kosiński SBasis(Linear const & bo) {
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński push_back(bo);
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński }
e4369b05aaa20df73a37f4d319ce456865cc74f3Krzysztof Kosiński SBasis(Linear* bo) {
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński push_back(*bo);
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński }
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński explicit SBasis(size_t n, Linear const&l) : d(n, l) {}
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński
e4369b05aaa20df73a37f4d319ce456865cc74f3Krzysztof Kosiński //IMPL: FragmentConcept
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński typedef double output_type;
e4369b05aaa20df73a37f4d319ce456865cc74f3Krzysztof Kosiński inline bool isZero() const {
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński if(empty()) return true;
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński for(unsigned i = 0; i < size(); i++) {
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński if(!(*this)[i].isZero()) return false;
e4369b05aaa20df73a37f4d319ce456865cc74f3Krzysztof Kosiński }
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński return true;
e4369b05aaa20df73a37f4d319ce456865cc74f3Krzysztof Kosiński }
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński inline bool isConstant() const {
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński if (empty()) return true;
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński if(!(*this)[0].isConstant()) return false;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen for (unsigned i = 1; i < size(); i++) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if(!(*this)[i].isZero()) return false;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
00f9ca0b3aa57e09f3c3f3632c5427fc03499df5Krzysztof Kosiński return true;
00f9ca0b3aa57e09f3c3f3632c5427fc03499df5Krzysztof Kosiński }
00f9ca0b3aa57e09f3c3f3632c5427fc03499df5Krzysztof Kosiński
00f9ca0b3aa57e09f3c3f3632c5427fc03499df5Krzysztof Kosiński bool isFinite() const;
00f9ca0b3aa57e09f3c3f3632c5427fc03499df5Krzysztof Kosiński inline double at0() const {
00f9ca0b3aa57e09f3c3f3632c5427fc03499df5Krzysztof Kosiński if(empty()) return 0; else return (*this)[0][0];
00f9ca0b3aa57e09f3c3f3632c5427fc03499df5Krzysztof Kosiński }
00f9ca0b3aa57e09f3c3f3632c5427fc03499df5Krzysztof Kosiński inline double at1() const{
00f9ca0b3aa57e09f3c3f3632c5427fc03499df5Krzysztof Kosiński if(empty()) return 0; else return (*this)[0][1];
00f9ca0b3aa57e09f3c3f3632c5427fc03499df5Krzysztof Kosiński }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen int degreesOfFreedom() const { return size()*2;}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen double valueAt(double t) const {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen double s = t*(1-t);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen double p0 = 0, p1 = 0;
e6bdf746e2d9e775704a475a29cc1bb167ec271cjohanengelen for(unsigned k = size(); k > 0; k--) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen const Linear &lin = (*this)[k-1];
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen p0 = p0*s + lin[0];
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen p1 = p1*s + lin[1];
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return (1-t)*p0 + t*p1;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen //double valueAndDerivative(double t, double &der) const {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen //}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen double operator()(double t) const {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return valueAt(t);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen std::vector<double> valueAndDerivatives(double t, unsigned n) const;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen SBasis toSBasis() const { return SBasis(*this); }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen double tailError(unsigned tail) const;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen// compute f(g)
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen SBasis operator()(SBasis const & g) const;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen//MUTATOR PRISON
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen //remove extra zeros
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen void normalize() {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński while(!empty() && 0 == back()[0] && 0 == back()[1])
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen pop_back();
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen void truncate(unsigned k) { if(k < size()) resize(k); }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenprivate:
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen void derive(); // in place version
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen};
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński//TODO: figure out how to stick this in linear, while not adding an sbasis dep
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis Linear::toSBasis() const { return SBasis(*this); }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen//implemented in sbasis-roots.cpp
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenOptInterval bounds_exact(SBasis const &a);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenOptInterval bounds_fast(SBasis const &a, int order = 0);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenOptInterval bounds_local(SBasis const &a, const OptInterval &t, int order = 0);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński/** Returns a function which reverses the domain of a.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen \param a sbasis function
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński \relates SBasis
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelenuseful for reversing a parameteric curve.
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen*/
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengeleninline SBasis reverse(SBasis const &a) {
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen SBasis result(a.size(), Linear());
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen for(unsigned k = 0; k < a.size(); k++)
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen result[k] = reverse(a[k]);
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen return result;
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen}
a4030d5ca449e7e384bc699cd249ee704faaeab0Chris Morgan
//IMPL: ScalableConcept
inline SBasis operator-(const SBasis& p) {
if(p.isZero()) return SBasis();
SBasis result(p.size(), Linear());
for(unsigned i = 0; i < p.size(); i++) {
result[i] = -p[i];
}
return result;
}
SBasis operator*(SBasis const &a, double k);
inline SBasis operator*(double k, SBasis const &a) { return a*k; }
inline SBasis operator/(SBasis const &a, double k) { return a*(1./k); }
SBasis& operator*=(SBasis& a, double b);
inline SBasis& operator/=(SBasis& a, double b) { return (a*=(1./b)); }
//IMPL: AddableConcept
SBasis operator+(const SBasis& a, const SBasis& b);
SBasis operator-(const SBasis& a, const SBasis& b);
SBasis& operator+=(SBasis& a, const SBasis& b);
SBasis& operator-=(SBasis& a, const SBasis& b);
//TODO: remove?
/*inline SBasis operator+(const SBasis & a, Linear const & b) {
if(b.isZero()) return a;
if(a.isZero()) return b;
SBasis result(a);
result[0] += b;
return result;
}
inline SBasis operator-(const SBasis & a, Linear const & b) {
if(b.isZero()) return a;
SBasis result(a);
result[0] -= b;
return result;
}
inline SBasis& operator+=(SBasis& a, const Linear& b) {
if(a.isZero())
a.push_back(b);
else
a[0] += b;
return a;
}
inline SBasis& operator-=(SBasis& a, const Linear& b) {
if(a.isZero())
a.push_back(-b);
else
a[0] -= b;
return a;
}*/
//IMPL: OffsetableConcept
inline SBasis operator+(const SBasis & a, double b) {
if(a.isZero()) return Linear(b, b);
SBasis result(a);
result[0] += b;
return result;
}
inline SBasis operator-(const SBasis & a, double b) {
if(a.isZero()) return Linear(-b, -b);
SBasis result(a);
result[0] -= b;
return result;
}
inline SBasis& operator+=(SBasis& a, double b) {
if(a.isZero())
a = SBasis(Linear(b,b));
else
a[0] += b;
return a;
}
inline SBasis& operator-=(SBasis& a, double b) {
if(a.isZero())
a = SBasis(Linear(-b,-b));
else
a[0] -= b;
return a;
}
SBasis shift(SBasis const &a, int sh);
SBasis shift(Linear const &a, int sh);
inline SBasis truncate(SBasis const &a, unsigned terms) {
SBasis c;
c.insert(c.begin(), a.begin(), a.begin() + std::min(terms, (unsigned)a.size()));
return c;
}
SBasis multiply(SBasis const &a, SBasis const &b);
// This performs a multiply and accumulate operation in about the same time as multiply. return a*b + c
SBasis multiply_add(SBasis const &a, SBasis const &b, SBasis c);
SBasis integral(SBasis const &c);
SBasis derivative(SBasis const &a);
SBasis sqrt(SBasis const &a, int k);
// return a kth order approx to 1/a)
SBasis reciprocal(Linear const &a, int k);
SBasis divide(SBasis const &a, SBasis const &b, int k);
inline SBasis operator*(SBasis const & a, SBasis const & b) {
return multiply(a, b);
}
inline SBasis& operator*=(SBasis& a, SBasis const & b) {
a = multiply(a, b);
return a;
}
/** Returns the degree of the first non zero coefficient.
\param a sbasis function
\param tol largest abs val considered 0
\return first non zero coefficient
\relates SBasis
*/
inline unsigned
valuation(SBasis const &a, double tol=0){
unsigned val=0;
while( val<a.size() &&
fabs(a[val][0])<tol &&
fabs(a[val][1])<tol )
val++;
return val;
}
// a(b(t))
SBasis compose(SBasis const &a, SBasis const &b);
SBasis compose(SBasis const &a, SBasis const &b, unsigned k);
SBasis inverse(SBasis a, int k);
//compose_inverse(f,g)=compose(f,inverse(g)), but is numerically more stable in some good cases...
//TODO: requires g(0)=0 & g(1)=1 atm. generalization should be obvious.
SBasis compose_inverse(SBasis const &f, SBasis const &g, unsigned order=2, double tol=1e-3);
/** Returns the sbasis on domain [0,1] that was t on [from, to]
\param t sbasis function
\param from,to interval
\return sbasis
\relates SBasis
*/
inline SBasis portion(const SBasis &t, double from, double to) { return compose(t, Linear(from, to)); }
inline SBasis portion(const SBasis &t, Interval ivl) { return compose(t, Linear(ivl.min(), ivl.max())); }
// compute f(g)
inline SBasis
SBasis::operator()(SBasis const & g) const {
return compose(*this, g);
}
inline std::ostream &operator<< (std::ostream &out_file, const Linear &bo) {
out_file << "{" << bo[0] << ", " << bo[1] << "}";
return out_file;
}
inline std::ostream &operator<< (std::ostream &out_file, const SBasis & p) {
for(unsigned i = 0; i < p.size(); i++) {
out_file << p[i] << "s^" << i << " + ";
}
return out_file;
}
// These are deprecated, use sbasis-math.h versions if possible
SBasis sin(Linear bo, int k);
SBasis cos(Linear bo, int k);
std::vector<double> roots(SBasis const & s);
std::vector<double> roots(SBasis const & s, Interval const inside);
std::vector<std::vector<double> > multi_roots(SBasis const &f,
std::vector<double> const &levels,
double htol=1e-7,
double vtol=1e-7,
double a=0,
double b=1);
//--------- Levelset like functions -----------------------------------------------------
/** Solve f(t) = v +/- tolerance. The collection of intervals where
* v - vtol <= f(t) <= v+vtol
* is returned (with a precision tol on the boundaries).
\param f sbasis function
\param level the value of v.
\param vtol: error tolerance on v.
\param a, b limit search on domain [a,b]
\param tol: tolerance on the result bounds.
\returns a vector of intervals.
*/
std::vector<Interval> level_set (SBasis const &f,
double level,
double vtol = 1e-5,
double a=0.,
double b=1.,
double tol = 1e-5);
/** Solve f(t)\in I=[u,v], which defines a collection of intervals (J_k). More precisely,
* a collection (J'_k) is returned with J'_k = J_k up to a given tolerance.
\param f sbasis function
\param level: the given interval of deisred values for f.
\param a, b limit search on domain [a,b]
\param tol: tolerance on the bounds of the result.
\returns a vector of intervals.
*/
std::vector<Interval> level_set (SBasis const &f,
Interval const &level,
double a=0.,
double b=1.,
double tol = 1e-5);
/** 'Solve' f(t) = v +/- tolerance for several values of v at once.
\param f sbasis function
\param levels vector of values, that should be sorted.
\param vtol: error tolerance on v.
\param a, b limit search on domain [a,b]
\param tol: the bounds of the returned intervals are exact up to that tolerance.
\returns a vector of vectors of intervals.
*/
std::vector<std::vector<Interval> > level_sets (SBasis const &f,
std::vector<double> const &levels,
double a=0.,
double b=1.,
double vtol = 1e-5,
double tol = 1e-5);
/** 'Solve' f(t)\in I=[u,v] for several intervals I at once.
\param f sbasis function
\param levels vector of 'y' intervals, that should be disjoints and sorted.
\param a, b limit search on domain [a,b]
\param tol: the bounds of the returned intervals are exact up to that tolerance.
\returns a vector of vectors of intervals.
*/
std::vector<std::vector<Interval> > level_sets (SBasis const &f,
std::vector<Interval> const &levels,
double a=0.,
double b=1.,
double tol = 1e-5);
}
#endif
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
#endif