sbasis.h revision 981b809bc6ed10a21e89444d9447e5475801874f
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * sbasis.h - S-power basis function class
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Nathan Hurst <njh@mail.csse.monash.edu.au>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Michael Sloan <mgsloan@gmail.com>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Copyright (C) 2006-2007 authors
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 * 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 * 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 * 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/*** An empty SBasis is identically 0. */
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 return true;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen bool isFinite() const;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen inline double at0() const {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if(empty()) return 0; else return (*this)[0][0];
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen inline double at1() const{
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if(empty()) return 0; else return (*this)[0][1];
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen double valueAt(double t) const {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen double s = t*(1-t);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen//TODO: rewrite as horner
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen for(unsigned k = 0; k < size(); k++) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen double operator()(double t) const {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen SBasis toSBasis() const { return SBasis(*this); }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen// compute f(g)
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen//MUTATOR PRISON
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen Linear& operator[](unsigned i) { return this->at(i); }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen //remove extra zeros
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen while(!empty() && 0 == back()[0] && 0 == back()[1])
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen void truncate(unsigned k) { if(k < size()) resize(k); }
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//implemented in sbasis-roots.cpp
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenInterval bounds_fast(SBasis const &a, int order = 0);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenInterval bounds_local(SBasis const &a, const Interval &t, int order = 0);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen for(unsigned k = 0; k < a.size(); k++)
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen//IMPL: ScalableConcept
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen for(unsigned i = 0; i < p.size(); i++) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis operator*(double k, SBasis const &a) { return a*k; }
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis operator/(SBasis const &a, double k) { return a*(1./k); }
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis& operator/=(SBasis& a, double b) { return (a*=(1./b)); }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen//IMPL: AddableConcept
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenSBasis operator+(const SBasis& a, const SBasis& b);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenSBasis operator-(const SBasis& a, const SBasis& b);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen//TODO: remove?
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis operator+(const SBasis & a, Linear const & b) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if(b.isZero()) return a;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if(a.isZero()) return b;
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis operator-(const SBasis & a, Linear const & b) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if(b.isZero()) return a;
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis& operator+=(SBasis& a, const Linear& b) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis& operator-=(SBasis& a, const Linear& b) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen//IMPL: OffsetableConcept
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis operator+(const SBasis & a, double b) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis operator-(const SBasis & a, double b) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis& operator+=(SBasis& a, double b) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis& operator-=(SBasis& a, double b) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis truncate(SBasis const &a, unsigned terms) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen c.insert(c.begin(), a.begin(), a.begin() + std::min(terms, (unsigned)a.size()));
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenSBasis multiply(SBasis const &a, SBasis const &b);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen// return a kth order approx to 1/a)
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenSBasis divide(SBasis const &a, SBasis const &b, int k);
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis operator*(SBasis const & a, SBasis const & b) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return multiply(a, b);
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis& operator*=(SBasis& a, SBasis const & b) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen//valuation: degree of the first non zero coefficient.
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline unsigned
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenSBasis compose(SBasis const &a, SBasis const &b);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenSBasis compose(SBasis const &a, SBasis const &b, unsigned 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);
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline SBasis portion(const SBasis &t, double from, double to) { return compose(t, Linear(from, to)); }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen// compute f(g)
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline std::ostream &operator<< (std::ostream &out_file, const Linear &bo) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen out_file << "{" << bo[0] << ", " << bo[1] << "}";
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline std::ostream &operator<< (std::ostream &out_file, const SBasis & p) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen for(unsigned i = 0; i < p.size(); i++) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenstd::vector<std::vector<double> > multi_roots(SBasis const &f,
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen double b=1);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen Local Variables:
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// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :