sbasis.h revision 04d58df841d9eb087eb99074f5c99235b801189b
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm/**
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * \file
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * \brief Defines S-power basis function class
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm *
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * Authors:
23d859f2ce09c04ed802cb4912cc9c50f512f0a2bgk * Nathan Hurst <njh@mail.csse.monash.edu.au>
23d859f2ce09c04ed802cb4912cc9c50f512f0a2bgk * Michael Sloan <mgsloan@gmail.com>
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm *
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * Copyright (C) 2006-2007 authors
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm *
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * This library is free software; you can redistribute it and/or
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * modify it either under the terms of the GNU Lesser General Public
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * License version 2.1 as published by the Free Software Foundation
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * (the "LGPL") or, at your option, under the terms of the Mozilla
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * Public License Version 1.1 (the "MPL"). If you do not alter this
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * notice, a recipient may use your version of this file under either
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * the MPL or the LGPL.
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm *
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * You should have received a copy of the LGPL along with this library
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * in the file COPYING-LGPL-2.1; if not, write to the Free Software
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * You should have received a copy of the MPL along with this library
23d859f2ce09c04ed802cb4912cc9c50f512f0a2bgk * in the file COPYING-MPL-1.1
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm *
ae22ad7adc4a7a418e71f5dbab8c1f0f7f464562johanengelen * The contents of this file are subject to the Mozilla Public License
ae22ad7adc4a7a418e71f5dbab8c1f0f7f464562johanengelen * Version 1.1 (the "License"); you may not use this file except in
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * compliance with the License. You may obtain a copy of the License at
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * http://www.mozilla.org/MPL/
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm *
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * OF ANY KIND, either express or implied. See the LGPL or the MPL for
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm * the specific language governing rights and limitations.
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm */
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm#ifndef SEEN_SBASIS_H
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm#define SEEN_SBASIS_H
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm#include <vector>
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm#include <cassert>
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm#include <iostream>
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm#include <2geom/linear.h>
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm#include <2geom/interval.h>
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm#include <2geom/utils.h>
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm#include <2geom/exception.h>
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm//#define USE_SBASISN 1
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm#if defined(USE_SBASIS_OF)
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm#include "sbasis-of.h"
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm#elif defined(USE_SBASISN)
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm#include "sbasisN.h"
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrmnamespace Geom{
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
963f23115db07f460bdd862b957f8bd9dba88b9bgustav_b/*** An empty SBasis is identically 0. */
963f23115db07f460bdd862b957f8bd9dba88b9bgustav_bclass SBasis : public SBasisN<1>;
963f23115db07f460bdd862b957f8bd9dba88b9bgustav_b
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm};
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm#else
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrmnamespace Geom{
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm/**
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm* \brief S-power basis function class
c5526a2c3001be486990d816757dd5ac028b3c3fjohanengelen*
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm* An empty SBasis is identically 0. */
963f23115db07f460bdd862b957f8bd9dba88b9bgustav_bclass SBasis{
963f23115db07f460bdd862b957f8bd9dba88b9bgustav_b std::vector<Linear> d;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm void push_back(Linear const&l) { d.push_back(l); }
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrmpublic:
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm // As part of our migration away from SBasis isa vector we provide this minimal set of vector interface methods.
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm size_t size() const {return d.size();}
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm Linear operator[](unsigned i) const {
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm return d[i];
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm }
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm Linear& operator[](unsigned i) { return d.at(i); }
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm Linear const* begin() const { return (Linear const*)&*d.begin();}
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm Linear const* end() const { return (Linear const*)&*d.end();}
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm Linear* begin() { return (Linear*)&*d.begin();}
b4998608f5fbde14c744b6ab8020664300e11f80jucablues Linear* end() { return (Linear*)&*d.end();}
b4998608f5fbde14c744b6ab8020664300e11f80jucablues bool empty() const {return d.empty();}
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm Linear &back() {return d.back();}
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm Linear const &back() const {return d.back();}
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm void pop_back() { d.pop_back();}
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm void resize(unsigned n) { d.resize(n);}
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm void resize(unsigned n, Linear const& l) { d.resize(n, l);}
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm void reserve(unsigned n) { d.reserve(n);}
42e99769805c14a5cc01c805faa3c3b03f9dd1c0johanengelen void clear() {d.clear();}
42e99769805c14a5cc01c805faa3c3b03f9dd1c0johanengelen void insert(Linear* before, const Linear* src_begin, const Linear* src_end) { d.insert(std::vector<Linear>::iterator(before), src_begin, src_end);}
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm //void insert(Linear* aa, Linear* bb, Linear* cc} { d.insert(aa, bb, cc);}
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm Linear& at(unsigned i) { return d.at(i);}
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm //void insert(Linear* before, int& n, Linear const &l) { d.insert(std::vector<Linear>::iterator(before), n, l);}
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm bool operator==(SBasis const&B) const { return d == B.d;}
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm bool operator!=(SBasis const&B) const { return d != B.d;}
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm operator std::vector<Linear>() { return d;}
c0cd5511d3b975ebe07d019c1f5528108725e438johanengelen
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm SBasis() {}
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm explicit SBasis(double a) {
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm push_back(Linear(a,a));
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm }
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm explicit SBasis(double a, double b) {
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm push_back(Linear(a,b));
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm }
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm SBasis(SBasis const & a) :
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm d(a.d)
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm {}
c5526a2c3001be486990d816757dd5ac028b3c3fjohanengelen SBasis(std::vector<Linear> const & ls) :
c5526a2c3001be486990d816757dd5ac028b3c3fjohanengelen d(ls)
c5526a2c3001be486990d816757dd5ac028b3c3fjohanengelen {}
c5526a2c3001be486990d816757dd5ac028b3c3fjohanengelen SBasis(Linear const & bo) {
c5526a2c3001be486990d816757dd5ac028b3c3fjohanengelen push_back(bo);
fdad0bc3aa765bdc61fe39e8c4da03f717525dccjohanengelen }
c5526a2c3001be486990d816757dd5ac028b3c3fjohanengelen SBasis(Linear* bo) {
c5526a2c3001be486990d816757dd5ac028b3c3fjohanengelen push_back(*bo);
c5526a2c3001be486990d816757dd5ac028b3c3fjohanengelen }
c5526a2c3001be486990d816757dd5ac028b3c3fjohanengelen explicit SBasis(size_t n, Linear const&l) : d(n, l) {}
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
c5526a2c3001be486990d816757dd5ac028b3c3fjohanengelen //IMPL: FragmentConcept
c5526a2c3001be486990d816757dd5ac028b3c3fjohanengelen typedef double output_type;
c5526a2c3001be486990d816757dd5ac028b3c3fjohanengelen inline bool isZero(double eps=EPSILON) const {
c5526a2c3001be486990d816757dd5ac028b3c3fjohanengelen if(empty()) return true;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm for(unsigned i = 0; i < size(); i++) {
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm if(!(*this)[i].isZero(eps)) return false;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm }
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm return true;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm }
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm inline bool isConstant(double eps=EPSILON) const {
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm if (empty()) return true;
c0cd5511d3b975ebe07d019c1f5528108725e438johanengelen if(!(*this)[0].isConstant(eps)) return false;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm for (unsigned i = 1; i < size(); i++) {
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm if(!(*this)[i].isZero(eps)) return false;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm }
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm return true;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm }
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm bool isFinite() const;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm inline double at0() const {
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm if(empty()) return 0; else return (*this)[0][0];
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm }
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm inline double at1() const{
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm if(empty()) return 0; else return (*this)[0][1];
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm }
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm int degreesOfFreedom() const { return size()*2;}
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm double valueAt(double t) const {
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm double s = t*(1-t);
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm double p0 = 0, p1 = 0;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm for(unsigned k = size(); k > 0; k--) {
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm const Linear &lin = (*this)[k-1];
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm p0 = p0*s + lin[0];
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm p1 = p1*s + lin[1];
23d859f2ce09c04ed802cb4912cc9c50f512f0a2bgk }
23d859f2ce09c04ed802cb4912cc9c50f512f0a2bgk return (1-t)*p0 + t*p1;
23d859f2ce09c04ed802cb4912cc9c50f512f0a2bgk }
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm //double valueAndDerivative(double t, double &der) const {
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm //}
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm double operator()(double t) const {
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm return valueAt(t);
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm }
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm std::vector<double> valueAndDerivatives(double t, unsigned n) const;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm SBasis toSBasis() const { return SBasis(*this); }
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm double tailError(unsigned tail) const;
23d859f2ce09c04ed802cb4912cc9c50f512f0a2bgk
23d859f2ce09c04ed802cb4912cc9c50f512f0a2bgk// compute f(g)
23d859f2ce09c04ed802cb4912cc9c50f512f0a2bgk SBasis operator()(SBasis const & g) const;
23d859f2ce09c04ed802cb4912cc9c50f512f0a2bgk
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm//MUTATOR PRISON
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm //remove extra zeros
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm void normalize() {
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm while(!empty() && 0 == back()[0] && 0 == back()[1])
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm pop_back();
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm }
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm void truncate(unsigned k) { if(k < size()) resize(k); }
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrmprivate:
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm void derive(); // in place version
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm};
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm//TODO: figure out how to stick this in linear, while not adding an sbasis dep
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrminline SBasis Linear::toSBasis() const { return SBasis(*this); }
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
1e7c268648bcbae15fc13b8c94dee677b401d9b3gustav_b//implemented in sbasis-roots.cpp
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrmOptInterval bounds_exact(SBasis const &a);
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrmOptInterval bounds_fast(SBasis const &a, int order = 0);
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrmOptInterval bounds_local(SBasis const &a, const OptInterval &t, int order = 0);
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm/** Returns a function which reverses the domain of a.
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm \param a sbasis function
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm \relates SBasis
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrmuseful for reversing a parameteric curve.
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm*/
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrminline SBasis reverse(SBasis const &a) {
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm SBasis result(a.size(), Linear());
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm for(unsigned k = 0; k < a.size(); k++)
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm result[k] = reverse(a[k]);
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm return result;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm}
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm//IMPL: ScalableConcept
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrminline SBasis operator-(const SBasis& p) {
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm if(p.isZero()) return SBasis();
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm SBasis result(p.size(), Linear());
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm for(unsigned i = 0; i < p.size(); i++) {
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm result[i] = -p[i];
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm }
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm return result;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm}
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrmSBasis operator*(SBasis const &a, double k);
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrminline SBasis operator*(double k, SBasis const &a) { return a*k; }
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrminline SBasis operator/(SBasis const &a, double k) { return a*(1./k); }
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrmSBasis& operator*=(SBasis& a, double b);
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrminline SBasis& operator/=(SBasis& a, double b) { return (a*=(1./b)); }
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm//IMPL: AddableConcept
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrmSBasis operator+(const SBasis& a, const SBasis& b);
23d859f2ce09c04ed802cb4912cc9c50f512f0a2bgkSBasis operator-(const SBasis& a, const SBasis& b);
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrmSBasis& operator+=(SBasis& a, const SBasis& b);
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrmSBasis& operator-=(SBasis& a, const SBasis& b);
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm//TODO: remove?
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm/*inline SBasis operator+(const SBasis & a, Linear const & b) {
ae22ad7adc4a7a418e71f5dbab8c1f0f7f464562johanengelen if(b.isZero()) return a;
ae22ad7adc4a7a418e71f5dbab8c1f0f7f464562johanengelen if(a.isZero()) return b;
ae22ad7adc4a7a418e71f5dbab8c1f0f7f464562johanengelen SBasis result(a);
ae22ad7adc4a7a418e71f5dbab8c1f0f7f464562johanengelen result[0] += b;
ae22ad7adc4a7a418e71f5dbab8c1f0f7f464562johanengelen return result;
ae22ad7adc4a7a418e71f5dbab8c1f0f7f464562johanengelen}
23d859f2ce09c04ed802cb4912cc9c50f512f0a2bgkinline SBasis operator-(const SBasis & a, Linear const & b) {
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm if(b.isZero()) return a;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm SBasis result(a);
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm result[0] -= b;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm return result;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm}
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrminline SBasis& operator+=(SBasis& a, const Linear& b) {
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm if(a.isZero())
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm a.push_back(b);
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm else
6dee13f1978edf532e28b55de684694f0b2e9449bgk a[0] += b;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm return a;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm}
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrminline SBasis& operator-=(SBasis& a, const Linear& b) {
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm if(a.isZero())
3ae3dd4eb0191cc986a035c790b8b97e6c6e4ee4johanengelen a.push_back(-b);
3ae3dd4eb0191cc986a035c790b8b97e6c6e4ee4johanengelen else
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm a[0] -= b;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm return a;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm }*/
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm//IMPL: OffsetableConcept
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrminline SBasis operator+(const SBasis & a, double b) {
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm if(a.isZero()) return Linear(b, b);
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm SBasis result(a);
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm result[0] += b;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm return result;
23d859f2ce09c04ed802cb4912cc9c50f512f0a2bgk}
23d859f2ce09c04ed802cb4912cc9c50f512f0a2bgkinline SBasis operator-(const SBasis & a, double b) {
b4998608f5fbde14c744b6ab8020664300e11f80jucablues if(a.isZero()) return Linear(-b, -b);
b4998608f5fbde14c744b6ab8020664300e11f80jucablues SBasis result(a);
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm result[0] -= b;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm return result;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm}
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrminline SBasis& operator+=(SBasis& a, double b) {
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm if(a.isZero())
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm a = SBasis(Linear(b,b));
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm else
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm a[0] += b;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm return a;
fd4b29a5cdef220804dfed85fec8acb5daceec5fpjrm}
ba885512446fff2803585a4aaec34e7742841f05cilixinline SBasis& operator-=(SBasis& a, double b) {
ba885512446fff2803585a4aaec34e7742841f05cilix if(a.isZero())
ba885512446fff2803585a4aaec34e7742841f05cilix a = SBasis(Linear(-b,-b));
ba885512446fff2803585a4aaec34e7742841f05cilix else
ba885512446fff2803585a4aaec34e7742841f05cilix a[0] -= b;
ba885512446fff2803585a4aaec34e7742841f05cilix return a;
ba885512446fff2803585a4aaec34e7742841f05cilix}
ba885512446fff2803585a4aaec34e7742841f05cilix
ba885512446fff2803585a4aaec34e7742841f05cilixSBasis shift(SBasis const &a, int sh);
ba885512446fff2803585a4aaec34e7742841f05cilixSBasis 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