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

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

To run the test

  rake run     # on linux and osx
  rake run_win # on windows

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.