Splines

Introduction

Splines is a set of C++ classes (with MATLAB mex interface) which implements various spline interpolation.

Matlab Toolbox

To use in MATLAB install the toolbox Splines.mltbx then compile the necessary mex files running CompileSplinesLib.

C++ Usage

The usage is simple:

#include "Splines.hh"
using namespace SplinesLoad;

// ....

CubicSpline spline;
double x[] = {1,2,3,4};
double y[] = {3,1,1,3};
spline.build(x,y,4); // build a cubic spline with 4 points

cout << spline(1.1)     << '\n'; // spline at x = 1.1
cout << spline.D(1.1)   << '\n'; // spline first derivative at x = 1.1
cout << spline.DD(1.1)  << '\n'; // spline second derivative at x = 1.1
cout << spline.DDD(1.1) << '\n'; // spline third derivative at x = 1.1

splines can be built incrementally

#include "Splines.hh"
using namespace SplinesLoad;

// ....

CubicSpline spline;

spline.pushBack( 1, 3 );
spline.pushBack( 2, 1 );
spline.pushBack( 3, 1 );
spline.pushBack( 4, 3 );
spline.build();

cout << spline(1.1)     << '\n'; // spline at x = 1.1
cout << spline.D(1.1)   << '\n'; // spline first derivative at x = 1.1
cout << spline.DD(1.1)  << '\n'; // spline second derivative at x = 1.1
cout << spline.DDD(1.1) << '\n'; // spline third derivative at x = 1.1

or by using standard vector

#include "Splines.hh"
#include <vector>
using namespace SplinesLoad;
using namespace std;

// ....

CubicSpline spline;
std::vector x, y;
x.push_back(1); y.push_back(3);
x.push_back(2); y.push_back(1);
x.push_back(3); y.push_back(1);
x.push_back(4); y.push_back(3);
spline.build(x,y);

cout << spline(1.1)     << '\n'; // spline at x = 1.1
cout << spline.D(1.1)   << '\n'; // spline first derivative at x = 1.1
cout << spline.DD(1.1)  << '\n'; // spline second derivative at x = 1.1
cout << spline.DDD(1.1) << '\n'; // spline third derivative at x = 1.1

Compile and tests

Using makefile

Edit makefile file to match compiler of your OS and do:

make

Using rakefile

rake build_win    # on windows
rake build_linux  # on linux
rake build_osx    # on mac

To run the test

make run     # using makefile
rake run     # using rake on linux and osx
rake run_win # using rake on windows

Contents

Author

Enrico Bertolazzi
Dipartimento di Ingegneria Industriale
Università degli Studi di Trento
email:

References

  • F.N. Fritsch and R.E. Carlson,
    Monotone Piecewise Cubic Interpolation,
    SIAM Journal of Numerical Analysis, Vol.17, No. 2, pp. 238-246, 1980.
  • Hiroshi Akima,
    Journal of the ACM,
    Vol.17, No. 4, 589-602, 1970.
  • Hiroshi Akima,
    A Method of Bivariate Interpolation and Smooth Surface Fitting for Irregularly Distributed Data Points,
    ACM Transactions on Mathematical Software, Vol.4, 148-164, 1978.

License

Copyright (c) 2020, Enrico Bertolazzi
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in
      the documentation and/or other materials provided with the distribution

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.