sbasis.h revision 6c3e745a94ef6b25a4ef9f018d350a7535aa45af
01d27eab5fca2dcb8e883011f8be77ae6b78a11cTed Gould/**
01d27eab5fca2dcb8e883011f8be77ae6b78a11cTed Gould * \file
01d27eab5fca2dcb8e883011f8be77ae6b78a11cTed Gould * \brief Defines S-power basis function class
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen *
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
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * OF ANY KIND, either express or implied. See the LGPL or the MPL for
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * the specific language governing rights and limitations.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#ifndef SEEN_SBASIS_H
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#define SEEN_SBASIS_H
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include <vector>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include <cassert>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include <iostream>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
8001ba81cb851b38d86650a2fef5817facffb763johanengelen#include <2geom/linear.h>
8001ba81cb851b38d86650a2fef5817facffb763johanengelen#include <2geom/interval.h>
8001ba81cb851b38d86650a2fef5817facffb763johanengelen#include <2geom/utils.h>
8001ba81cb851b38d86650a2fef5817facffb763johanengelen#include <2geom/exception.h>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
6c3e745a94ef6b25a4ef9f018d350a7535aa45afTed Gould
6c3e745a94ef6b25a4ef9f018d350a7535aa45afTed Gould#ifdef USE_SBASIS_OF
6c3e745a94ef6b25a4ef9f018d350a7535aa45afTed Gould
6c3e745a94ef6b25a4ef9f018d350a7535aa45afTed Gould#include "sbasis-of.h"
6c3e745a94ef6b25a4ef9f018d350a7535aa45afTed Gould
6c3e745a94ef6b25a4ef9f018d350a7535aa45afTed Gould#else
6c3e745a94ef6b25a4ef9f018d350a7535aa45afTed Gould
6c3e745a94ef6b25a4ef9f018d350a7535aa45afTed Gouldnamespace Geom{
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen/*** An empty SBasis is identically 0. */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenclass SBasis : public std::vector<Linear>{
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenpublic:
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen SBasis() {}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen explicit SBasis(double a) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen push_back(Linear(a,a));
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen SBasis(SBasis const & a) :
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen std::vector<Linear>(a)
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen {}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen SBasis(Linear const & bo) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen push_back(bo);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
d02f89424319afe28fe2cc6696c28108448de3acmcecchetti SBasis(Linear* bo) {
d02f89424319afe28fe2cc6696c28108448de3acmcecchetti push_back(*bo);
d02f89424319afe28fe2cc6696c28108448de3acmcecchetti }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen //IMPL: FragmentConcept
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen typedef double output_type;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen inline bool isZero() const {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if(empty()) return true;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen for(unsigned i = 0; i < size(); i++) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if(!(*this)[i].isZero()) return false;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return true;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
bacf092e612ad1911c5a4db58261865e54953b34johanengelen inline bool isConstant() const {
bacf092e612ad1911c5a4db58261865e54953b34johanengelen if (empty()) return true;
bacf092e612ad1911c5a4db58261865e54953b34johanengelen for (unsigned i = 0; i < size(); i++) {
bacf092e612ad1911c5a4db58261865e54953b34johanengelen if(!(*this)[i].isConstant()) return false;
bacf092e612ad1911c5a4db58261865e54953b34johanengelen }
bacf092e612ad1911c5a4db58261865e54953b34johanengelen return true;
bacf092e612ad1911c5a4db58261865e54953b34johanengelen }
bacf092e612ad1911c5a4db58261865e54953b34johanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen bool isFinite() const;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen inline double at0() const {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if(empty()) return 0; else return (*this)[0][0];
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen inline double at1() const{
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if(empty()) return 0; else return (*this)[0][1];
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen double valueAt(double t) const {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen double s = t*(1-t);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen double p0 = 0, p1 = 0;
d02f89424319afe28fe2cc6696c28108448de3acmcecchetti for(unsigned k = size(); k > 0; k--) {
d02f89424319afe28fe2cc6696c28108448de3acmcecchetti const Linear &lin = (*this)[k-1];
d02f89424319afe28fe2cc6696c28108448de3acmcecchetti p0 = p0*s + lin[0];
d02f89424319afe28fe2cc6696c28108448de3acmcecchetti p1 = p1*s + lin[1];
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return (1-t)*p0 + t*p1;
e6bdf746e2d9e775704a475a29cc1bb167ec271cjohanengelen }
d02f89424319afe28fe2cc6696c28108448de3acmcecchetti //double valueAndDerivative(double t, double &der) const {
d02f89424319afe28fe2cc6696c28108448de3acmcecchetti //}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen double operator()(double t) const {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return valueAt(t);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
29684a16b6c92bee28a94fdc2607bcc143950fa8johanengelen
d8fa3c4faade9a5a8e7f79450544b1925e1ade41johanengelen std::vector<double> valueAndDerivatives(double t, unsigned n) const;
29684a16b6c92bee28a94fdc2607bcc143950fa8johanengelen
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 Linear operator[](unsigned i) const {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen assert(i < size());
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return std::vector<Linear>::operator[](i);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen//MUTATOR PRISON
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen Linear& operator[](unsigned i) { return this->at(i); }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen //remove extra zeros
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen void normalize() {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen while(!empty() && 0 == back()[0] && 0 == back()[1])
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen pop_back();
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
d02f89424319afe28fe2cc6696c28108448de3acmcecchetti
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen void truncate(unsigned k) { if(k < size()) resize(k); }
d02f89424319afe28fe2cc6696c28108448de3acmcecchettiprivate:
d02f89424319afe28fe2cc6696c28108448de3acmcecchetti void derive(); // in place version
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen};
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen//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
6c3e745a94ef6b25a4ef9f018d350a7535aa45afTed GouldOptInterval bounds_exact(SBasis const &a);
6c3e745a94ef6b25a4ef9f018d350a7535aa45afTed GouldOptInterval bounds_fast(SBasis const &a, int order = 0);
6c3e745a94ef6b25a4ef9f018d350a7535aa45afTed GouldOptInterval bounds_local(SBasis const &a, const OptInterval &t, int order = 0);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
01d27eab5fca2dcb8e883011f8be77ae6b78a11cTed Gould/** Returns a function which reverses the domain of a.
01d27eab5fca2dcb8e883011f8be77ae6b78a11cTed Gould \param a sbasis function
01d27eab5fca2dcb8e883011f8be77ae6b78a11cTed Gould
01d27eab5fca2dcb8e883011f8be77ae6b78a11cTed Goulduseful for reversing a parameteric curve.
01d27eab5fca2dcb8e883011f8be77ae6b78a11cTed Gould*/
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis reverse(SBasis const &a) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen SBasis result;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen result.reserve(a.size());
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen for(unsigned k = 0; k < a.size(); k++)
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen result.push_back(reverse(a[k]));
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return result;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen//IMPL: ScalableConcept
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis operator-(const SBasis& p) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if(p.isZero()) return SBasis();
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen SBasis result;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen result.reserve(p.size());
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen for(unsigned i = 0; i < p.size(); i++) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen result.push_back(-p[i]);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return result;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenSBasis operator*(SBasis const &a, double k);
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis operator*(double k, SBasis const &a) { return a*k; }
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis operator/(SBasis const &a, double k) { return a*(1./k); }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenSBasis& operator*=(SBasis& a, double b);
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis& operator/=(SBasis& a, double b) { return (a*=(1./b)); }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen//IMPL: AddableConcept
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenSBasis operator+(const SBasis& a, const SBasis& b);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenSBasis operator-(const SBasis& a, const SBasis& b);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenSBasis& operator+=(SBasis& a, const SBasis& b);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenSBasis& operator-=(SBasis& a, const SBasis& b);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen//TODO: remove?
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis operator+(const SBasis & a, Linear const & b) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if(b.isZero()) return a;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if(a.isZero()) return b;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen SBasis result(a);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen result[0] += b;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return result;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis operator-(const SBasis & a, Linear const & b) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if(b.isZero()) return a;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen SBasis result(a);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen result[0] -= b;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return result;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis& operator+=(SBasis& a, const Linear& b) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if(a.isZero())
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen a.push_back(b);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen else
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen a[0] += b;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return a;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis& operator-=(SBasis& a, const Linear& b) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if(a.isZero())
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen a.push_back(-b);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen else
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen a[0] -= b;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return a;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen//IMPL: OffsetableConcept
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis operator+(const SBasis & a, double b) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if(a.isZero()) return Linear(b, b);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen SBasis result(a);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen result[0] += b;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return result;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis operator-(const SBasis & a, double b) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if(a.isZero()) return Linear(-b, -b);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen SBasis result(a);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen result[0] -= b;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return result;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis& operator+=(SBasis& a, double b) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if(a.isZero())
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen a.push_back(Linear(b,b));
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen else
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen a[0] += b;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return a;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis& operator-=(SBasis& a, double b) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if(a.isZero())
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen a.push_back(Linear(-b,-b));
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen else
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen a[0] -= b;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return a;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenSBasis shift(SBasis const &a, int sh);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenSBasis shift(Linear const &a, int sh);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis truncate(SBasis const &a, unsigned terms) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen SBasis c;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen c.insert(c.begin(), a.begin(), a.begin() + std::min(terms, (unsigned)a.size()));
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return c;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenSBasis multiply(SBasis const &a, SBasis const &b);
d02f89424319afe28fe2cc6696c28108448de3acmcecchetti// This performs a multiply and accumulate operation in about the same time as multiply. return a*b + c
d02f89424319afe28fe2cc6696c28108448de3acmcecchettiSBasis multiply_add(SBasis const &a, SBasis const &b, SBasis c);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenSBasis integral(SBasis const &c);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenSBasis derivative(SBasis const &a);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenSBasis sqrt(SBasis const &a, int k);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen// return a kth order approx to 1/a)
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenSBasis reciprocal(Linear const &a, int k);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenSBasis divide(SBasis const &a, SBasis const &b, int k);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis operator*(SBasis const & a, SBasis const & b) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return multiply(a, b);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis& operator*=(SBasis& a, SBasis const & b) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen a = multiply(a, b);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return a;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
01d27eab5fca2dcb8e883011f8be77ae6b78a11cTed Gould/** Returns the degree of the first non zero coefficient.
01d27eab5fca2dcb8e883011f8be77ae6b78a11cTed Gould \param a sbasis function
01d27eab5fca2dcb8e883011f8be77ae6b78a11cTed Gould \param tol largest abs val considered 0
01d27eab5fca2dcb8e883011f8be77ae6b78a11cTed Gould \returns first non zero coefficient
01d27eab5fca2dcb8e883011f8be77ae6b78a11cTed Gould*/
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline unsigned
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenvaluation(SBasis const &a, double tol=0){
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen unsigned val=0;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen while( val<a.size() &&
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen fabs(a[val][0])<tol &&
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen fabs(a[val][1])<tol )
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen val++;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return val;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen// a(b(t))
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenSBasis compose(SBasis const &a, SBasis const &b);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenSBasis compose(SBasis const &a, SBasis const &b, unsigned k);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenSBasis inverse(SBasis a, int k);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen//compose_inverse(f,g)=compose(f,inverse(g)), but is numerically more stable in some good cases...
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen//TODO: requires g(0)=0 & g(1)=1 atm. generalization should be obvious.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenSBasis compose_inverse(SBasis const &f, SBasis const &g, unsigned order=2, double tol=1e-3);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
01d27eab5fca2dcb8e883011f8be77ae6b78a11cTed Gould/** Returns the sbasis on domain [0,1] that was t on [from, to]
01d27eab5fca2dcb8e883011f8be77ae6b78a11cTed Gould \param a sbasis function
01d27eab5fca2dcb8e883011f8be77ae6b78a11cTed Gould \param from,to interval
01d27eab5fca2dcb8e883011f8be77ae6b78a11cTed Gould \returns sbasis
01d27eab5fca2dcb8e883011f8be77ae6b78a11cTed Gould
01d27eab5fca2dcb8e883011f8be77ae6b78a11cTed Gould*/
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis portion(const SBasis &t, double from, double to) { return compose(t, Linear(from, to)); }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen// compute f(g)
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenSBasis::operator()(SBasis const & g) const {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return compose(*this, g);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline std::ostream &operator<< (std::ostream &out_file, const Linear &bo) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen out_file << "{" << bo[0] << ", " << bo[1] << "}";
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return out_file;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline std::ostream &operator<< (std::ostream &out_file, const SBasis & p) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen for(unsigned i = 0; i < p.size(); i++) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen out_file << p[i] << "s^" << i << " + ";
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return out_file;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
01d27eab5fca2dcb8e883011f8be77ae6b78a11cTed Gould// These are deprecated, use sbasis-math.h versions if possible
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenSBasis sin(Linear bo, int k);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenSBasis cos(Linear bo, int k);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenstd::vector<double> roots(SBasis const & s);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenstd::vector<std::vector<double> > multi_roots(SBasis const &f,
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen std::vector<double> const &levels,
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen double htol=1e-7,
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen double vtol=1e-7,
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen double a=0,
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen double b=1);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
6c3e745a94ef6b25a4ef9f018d350a7535aa45afTed Gould#endif
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen/*
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen Local Variables:
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen mode:c++
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen c-file-style:"stroustrup"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen indent-tabs-mode:nil
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen fill-column:99
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen End:
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen*/
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#endif