Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ support.
|:--:|:--:|
| *2D Linear interpolation* | *2D Monotonic cubic spline interpolation* |

| ![2D Akima spline interpolation](docs/images/akima_2D.png) | ![2D Natural spline interpolation](docs/images/natural_spline_2D.png) |
| ![2D Modfied Akima spline interpolation](docs/images/makima_2D.png) | ![2D Natural spline interpolation](docs/images/natural_spline_2D.png) |
|:--:|:--:|
| *2D Akima spline interpolation* | *2D Natural spline interpolation* |

Expand All @@ -24,7 +24,7 @@ interpolation in `N` dimensions.
For cubic piecewise interpolation, the library features three types:

- Monotone cubic interpolation
- Akima spline interpolation
- Mdofied Akima spline interpolation
- Natural cubic spline interpolation

All classes are templatized and support the STL's vector types.
Expand Down
26 changes: 13 additions & 13 deletions cubinterpp/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import cubinterpp.cubinterpp_py as cubinterpp # cubinterpp_py is a pybind11 module


def get_test_data(case='akima', start=1.0, end=5.0, size=8):
""" Generates test input data for Akima Spline tests """
if case == 'akima':
def get_test_data(case='makima', start=1.0, end=5.0, size=8):
""" Generates test input data for Modified Akima Spline tests """
if case == 'makima':
return np.array([1, 2, 3, 4.0, 5.0, 5.5, 7.0, 8.0, 9.0, 9.5, 10]), \
np.array([0, 0, 0, 0.5, 0.4, 1.2, 1.2, 0.1, 0.0, 0.3, 0.6])

Expand Down Expand Up @@ -41,7 +41,7 @@ def get_test_data_2d(case='standard'):
f = np.array([[1.0, 2.0, 2.0],
[2.0, 3.0, 3.0],
[3.0, 3.0, 4.0]])
case 'akima':
case 'makima':
x = np.array([1, 2, 3, 4.0, 5.0, 5.5, 7.0, 8.0, 9.0, 9.5, 10])
y = np.array([1, 2, 3, 4.0, 5.0, 5.5, 7.0, 8.0, 9.0, 9.5, 10])
f = np.array([[0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
Expand Down Expand Up @@ -79,16 +79,16 @@ def scipy_linear_interp(x, y, f, x_fine, y_fine):
return interp2((x_grid, y_grid))


def cubinterpp_interp2(interp_type='linear', data_case='akima', refinement=50):
def cubinterpp_interp2(interp_type='linear', data_case='makima', refinement=50):
""" Short hand for 2D interpolatino with cubinterpp """
x, y, f = get_test_data_2d(case=data_case)
match interp_type:
case 'linear':
interp2 = cubinterpp.LinearInterp2D(x, y, f)
case 'monotonic':
interp2 = cubinterpp.MonotonicSpline2D(x, y, f)
case 'akima':
interp2 = cubinterpp.AkimaSpline2D(x, y, f)
case 'makima':
interp2 = cubinterpp.MakimaSpline2D(x, y, f)
case 'natural_spline':
interp2 = cubinterpp.NaturalSpline2D(x, y, f)
x_fine, y_fine = refine_grid(x, refinement), refine_grid(y, refinement)
Expand All @@ -103,7 +103,7 @@ def cubinterpp_interp2(interp_type='linear', data_case='akima', refinement=50):
def main():
""" Tets Cubic spline interpolation """

x, y = get_test_data(case='akima')
x, y = get_test_data(case='makima')
x_fine = refine_grid(x)

spline = cubinterpp.LinearInterp1D(x, y)
Expand All @@ -112,23 +112,23 @@ def main():
spline = cubinterpp.NaturalSpline1D(x, y)
y_fine_natural = spline.evaln(x_fine)

spline = cubinterpp.AkimaSpline1D(x, y)
y_fine_akima = spline.evaln(x_fine)
spline = cubinterpp.MakimaSpline1D(x, y)
y_fine_makima = spline.evaln(x_fine)

spline = cubinterpp.MonotonicSpline1D(x, y)
y_fine_monotonic = spline.evaln(x_fine)

mpg.figure(title='Test figure')
mpg.plot(x_fine, y_fine_linear)
mpg.plot(x_fine, y_fine_monotonic)
mpg.plot(x_fine, y_fine_akima)
mpg.plot(x_fine, y_fine_makima)
mpg.plot(x_fine, y_fine_natural)
mpg.plot(x, y, width=0, symbol='o', symbol_color='r', symbol_size=6)
mpg.gca().grid = True
mpg.legend(
'Linear interpolation',
'Monotonic cubic interpolation',
'Akima spline',
'Modified Akima spline',
'Natural cubic spline',
'data points'
)
Expand All @@ -138,7 +138,7 @@ def main():
yp = np.tile(y, x.size)
zp = f.flatten()

for interp_type in ('linear', 'monotonic', 'akima', 'natural_spline'):
for interp_type in ('linear', 'monotonic', 'makima', 'natural_spline'):
x_fine, y_fine, z_fine = cubinterpp_interp2(
interp_type=interp_type,
data_case='three_bumps',
Expand Down
1,115 changes: 371 additions & 744 deletions docs/images/comparison_1D.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
2 changes: 1 addition & 1 deletion docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interpolation.
For cubic piecewise interpolation, the library features three types:

- Monotone cubic interpolation
- Akima spline interpolation
- Modified Akima spline interpolation
- Natural cubic spline interpolation

Linear interpolation is supported for `N`-dimensional data, whereas cubic
Expand Down
8 changes: 4 additions & 4 deletions include/cubic_splines_1d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ class MonotonicSpline1D : public CubicInterpND<T, N>


template <typename T, std::size_t N=1>
class AkimaSpline1D : public CubicInterpND<T, N>
class MakimaSpline1D : public CubicInterpND<T, N>
{
using Vector = std::vector<T>;
public:
AkimaSpline1D(const Vector &x, const Vector &f)
MakimaSpline1D(const Vector &x, const Vector &f)
: CubicInterpND<T, N>(x, f)
{
this->build(f);
}

~AkimaSpline1D() {}
~MakimaSpline1D() {}

Vector calc_slopes(const Vector &x, const Vector &f) const override {
return akima_slopes<T>(x, f);
return makima_slopes<T>(x, f);
}

};
Expand Down
8 changes: 4 additions & 4 deletions include/cubic_splines_2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ MonotonicSpline2D(const Vector &_x, const Vector &_y, const VectorN &_f)


template <typename T, std::size_t N=2>
class AkimaSpline2D : public CubicInterpND<T, N>
class MakimaSpline2D : public CubicInterpND<T, N>
{
using Vector = std::vector<T>;
using Mdspan1D = std::mdspan<T, std::dextents<std::size_t, 1>, std::layout_stride>;
using VectorN = cip::VectorN<T, N>;
public:
AkimaSpline2D(const Vector &_x, const Vector &_y, const VectorN &_f)
MakimaSpline2D(const Vector &_x, const Vector &_y, const VectorN &_f)
: CubicInterpND<T, N>(_f, _x, _y)
{
this->build(_f, _x, _y);
}
~AkimaSpline2D() {}
~MakimaSpline2D() {}

Vector calc_slopes(const Vector &x, const Mdspan1D &f) const override
{
return akima_slopes<T>(x, f);
return makima_slopes<T>(x, f);
}
};

Expand Down
4 changes: 2 additions & 2 deletions include/slopes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ std::vector<T> monotonic_slopes(const Tx x, const Tf f)


template <typename T, typename Tx, typename Tf>
std::vector<T> akima_slopes(const Tx x, const Tf f)
std::vector<T> makima_slopes(const Tx x, const Tf f)
{
/*
Derivative values for Akima cubic Hermite interpolation
Derivative values for Modified Akima cubic Hermite interpolation

Akima's derivative estimate at grid node x(i) requires the four finite
differences corresponding to the five grid nodes x(i-2:i+2).
Expand Down
10 changes: 5 additions & 5 deletions src/cubinterpp_py_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ PYBIND11_MODULE(cubinterpp_py, m) {
.def("eval", &cip::MonotonicSpline1D<double>::eval, py::return_value_policy::reference_internal)
.def("evaln", &cip::MonotonicSpline1D<double>::evaln, py::return_value_policy::reference_internal);

py::class_<cip::AkimaSpline1D<double>>(m, "AkimaSpline1D")
py::class_<cip::MakimaSpline1D<double>>(m, "MakimaSpline1D")
.def(py::init<DoubleVector, DoubleVector>())
.def("eval", &cip::AkimaSpline1D<double>::eval, py::return_value_policy::reference_internal)
.def("evaln", &cip::AkimaSpline1D<double>::evaln, py::return_value_policy::reference_internal);
.def("eval", &cip::MakimaSpline1D<double>::eval, py::return_value_policy::reference_internal)
.def("evaln", &cip::MakimaSpline1D<double>::evaln, py::return_value_policy::reference_internal);

py::class_<cip::NaturalSpline1D<double>>(m, "NaturalSpline1D")
.def(py::init<DoubleVector, DoubleVector>())
Expand All @@ -52,9 +52,9 @@ PYBIND11_MODULE(cubinterpp_py, m) {
.def(py::init<DoubleVector, DoubleVector, DoubleVector2>())
.def("eval", &cip::MonotonicSpline2D<double>::eval<double, double>, py::return_value_policy::reference_internal);

py::class_<cip::AkimaSpline2D<double>>(m, "AkimaSpline2D")
py::class_<cip::MakimaSpline2D<double>>(m, "MakimaSpline2D")
.def(py::init<DoubleVector, DoubleVector, DoubleVector2>())
.def("eval", &cip::AkimaSpline2D<double>::eval<double, double>, py::return_value_policy::reference_internal);
.def("eval", &cip::MakimaSpline2D<double>::eval<double, double>, py::return_value_policy::reference_internal);

py::class_<cip::NaturalSpline2D<double>>(m, "NaturalSpline2D")
.def(py::init<DoubleVector, DoubleVector, DoubleVector2>())
Expand Down
6 changes: 3 additions & 3 deletions tests/test_cubic_splines_1d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

using Vector = std::vector<double>;
using MonotonicSpline = cip::MonotonicSpline1D<double>;
using AkimaSpline = cip::AkimaSpline1D<double>;
using MakimaSpline = cip::MakimaSpline1D<double>;
using NaturalSpline = cip::NaturalSpline1D<double, 1, cip::BoundaryConditionType::Natural>;
using NaturalSplineNotAKnot = cip::NaturalSpline1D<double, 1, cip::BoundaryConditionType::NotAKnot>;
using NaturalSplineClamped = cip::NaturalSpline1D<double, 1, cip::BoundaryConditionType::Clamped>;
Expand All @@ -20,12 +20,12 @@ TEST(TestCubicSpline1D, test_monotonic_spline_1d) {
}


TEST(TestCubicSpline1D, test_akima_spline_1d) {
TEST(TestCubicSpline1D, test_makima_spline_1d) {
cip::Vector x = { 1, 2, 3, 4.0, 5.0, 5.5, 7.0, 8.0, 9.0, 9.5, 10 };
cip::Vector f = { 0, 0, 0, 0.5, 0.4, 1.2, 1.2, 0.1, 0.0, 0.3, 0.6 };
cip::Vector x_fine = { 1.0, 1.375, 1.75, 2.125, 2.5, 2.875, 3.25, 3.625, 4.0, 4.375, 4.75, 5.125, 5.5, 5.875, 6.25, 6.625, 7.0, 7.375, 7.75, 8.125, 8.5, 8.875, 9.25, 9.625, 10.0 };
cip::Vector f_fine = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.061279296875, 0.289154052734375, 0.5, 0.4924377441406449, 0.38676757812498863, 0.532760099085408, 1.1999999999999993, 1.3822294207317043, 1.4444817073170721, 1.3844931402439027, 1.2000000000000455, 0.7961763822113426, 0.30638221153833456, 0.04557132320800861, -0.05930944055945275, -0.0412170836975676, 0.1380681818177436, 0.3749999999999991, 0.5999999999999988 };
ASSERT_TRUE(cip::Interp1DAssertions<AkimaSpline>(x, f, x_fine, f_fine));
ASSERT_TRUE(cip::Interp1DAssertions<MakimaSpline>(x, f, x_fine, f_fine));
}


Expand Down
2 changes: 1 addition & 1 deletion tests/test_linear_interp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ TEST(TestLinearCell1D, test_linear_cell_1d) {
}


TEST(TestInterp1D, test_linear_interp_1d_akima) {
TEST(TestInterp1D, test_linear_interp_1d_makima) {
cip::Vector x = { 1.0, 2.0, 3.0, 4.0, 5.0, 5.5, 7.0, 8.0, 9.0, 9.5, 10.0 };
cip::Vector f = { 0.0, 0.0, 0.0, 0.5, 0.4, 1.2, 1.2, 0.1, 0.0, 0.3, 0.6 };
cip::Vector x_fine = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 };
Expand Down
12 changes: 6 additions & 6 deletions tests/utils/generate_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from scipy.interpolate import RegularGridInterpolator, CubicSpline


def get_test_data(case='akima', start=1.0, end=5.0, size=8):
""" Generates test input data for Akima Spline tests """
if case == 'akima':
def get_test_data(case='makima', start=1.0, end=5.0, size=8):
""" Generates test input data for Modified Akima Spline tests """
if case == 'makima':
return np.array([1, 2, 3, 4.0, 5.0, 5.5, 7.0, 8.0, 9.0, 9.5, 10]), \
np.array([0, 0, 0, 0.5, 0.4, 1.2, 1.2, 0.1, 0.0, 0.3, 0.6])

Expand Down Expand Up @@ -38,7 +38,7 @@ def get_test_data_2d(case='standard'):
f = np.array([[1.0, 2.0, 2.0],
[2.0, 3.0, 3.0],
[3.0, 3.0, 4.0]])
case 'akima':
case 'makima':
x = np.array([1, 2, 3, 4.0, 5.0, 5.5, 7.0, 8.0, 9.0, 9.5, 10])
y = x
f = np.array([[0, 0, 0, 0.5, 0.4, 1.2, 1.2, 0.1, 0.0, 0.3, 0.6],
Expand Down Expand Up @@ -153,7 +153,7 @@ def print_cpp_vector(vector_type, name, array):


def generate_1d_example(
case='akima',
case='makima',
size_fine=15,
method='linear',
bc_type='natural',
Expand Down Expand Up @@ -206,7 +206,7 @@ def generate_3d_example(case='normalized', size_fine=5, method='linear'):

def main():
# method = 'cubic_spline'
# data_case = 'akima'
# data_case = 'makima'
method = 'cubic'
data_case = 'normalized'
# generate_1d_example(case=data_case, size_fine=20, method=method,
Expand Down