Giter Site home page Giter Site logo

cxx-spline's Introduction

Cubic splines for C++

C++11 Test status Boost License

Header-only cubic spline interpolator for C++11 and above.

Usage

Copy spline.hpp into your include directory. Use cubic_spline class like this:

#include <vector>
#include <spline.hpp>

int main()
{
    // Some points (t,y) on a curve y = f(t)
    std::vector<double> t = { 0, 1, 2, 3, 4, 5 };
    std::vector<double> y = { 1, 2, 3, 2, 1, 2 };

    // Spline interpolation (and extrapolation) of the points
    cubic_spline spline(t, y);

    spline(0.5); // 1.44976
    spline(1.5); // 2.65072
    spline(6.0); // 3
}

To interpolate a 2D (or higher dimensional) curve, just create splines for each coordinate values. Example:

#include <iostream>
#include <vector>
#include <spline.hpp>

int main()
{
    // Samples from a circle.
    std::vector<double> t = { 0, 0.25, 0.5, 0.75, 1 };
    std::vector<double> x = { 1, 0, -1, 0, 1 };
    std::vector<double> y = { 0, 1, 0, -1, 0 };

    // Spline interpolation of each coordinate.
    cubic_spline spline_x(t, x);
    cubic_spline spline_y(t, y);

    for (int i = 0; i <= 100; i++) {
        double sx = spline_x(i / 100.0);
        double sy = spline_y(i / 100.0);
        std::cout << sx << '\t' << sy << '\n';
    }
}

Boundary conditions

The cubic_spline class supports natural and not-a-knot boundary conditions. Pass cubic_spline::natural or cubic_spline::not_a_knot to the constructor to choose the boundary conditions. Natural is the default.

cubic_spline natural_spline(t, x, cubic_spline::natural);
cubic_spline notaknot_spline(t, x, cubic_spline::not_a_knot);

The natural (or free-end) spline gives the minimum-energy curve in a certain physical sense. The not-a-knot spline gives a smoother-looking curve when used for visualization.

Testing

git clone https://github.com/snsinfu/cxx-spline.git
cd cxx-spline/test
make

License

Boost Software License, Version 1.0.

cxx-spline's People

Contributors

snsinfu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.