JustInterp is the simple lightweight header-only C++17 interpolation library.
JustInterp is the single-header library, hence, you just need to copy JustInterp.hpp from include_single/JustInterp or release tab to your project.
#include "JustInterp/JustInterp.hpp"Set the necessary options to enable C++17, for example:
- Compiler flag:
-std=c++17for GCC and Clang,/std:c++17for MSVC - Using CMake:
- Specify compile features for specific target:
target_compile_features(<target> <PRIVATE|PUBLIC|INTERFACE> cxx_std_17) - Set global property:
set(CMAKE_CXX_STANDARD 17 CACHE STRING "The C++ standard to use")
- Specify compile features for specific target:
Piece-wise linear interpolation (https://en.wikipedia.org/wiki/Linear_interpolation).
RealContainer is real-type iterable containers i.e. should support begin(), end(), data(), size() methods, for example, std::vector or std::array.
RealContainer x: data pointsRealContainer y: function values
#include "JustInterp/JustInterp.hpp"
std::vector<double> x, y;
// ...
// set points x and values y
// ...
// Define linear interpolator and set data
JustInterp::LinearInterpolator<double> interpolator(x, y);
// Interpolate at the specific point
double result = interpolator(3.5);
// Interpolate at the array of points
std::vector<double> point_array{1.5, 2.5, 3.5};
std::vector<double> results = interpolator(point_array);- Using constructor:
JustInterp::LinearInterpolator<double> interpolator(x, y);
x.size()andy.size()must be equal. - Using
SetDatamethod:JustInterp::LinearInterpolator<double> interpolator; interpolator.SetData(x, y);x.size()andy.size()must be equal. - Using
SetDatamethod for data pointers:It is assumed that arraysJustInterp::LinearInterpolator<double> interpolator; interpolator.SetData(x.size(), x.data(), y.data());x.data()andy.data()of the same size ofx.size().
There are two avaliable types of extrapolation:
- (Default)
JustInterp::ConstantExtrapolationJustInterp::LinearInterpolator<double, JustInterp::ConstantExtrapolation> interpolator; /* or */ JustInterp::LinearInterpolator<double> interpolator;
JustInterp::LinearExtrapolationJustInterp::LinearInterpolator<double, JustInterp::LinearExtrapolation> interpolator;
Bilinear interpolation (https://en.wikipedia.org/wiki/Bilinear_interpolation).
RealContainer is real-type iterable containers i.e. should support begin(), end(), data(), size() methods, for example, std::vector or std::array.
RealContainer x_1d- grid coordinates along x-axisRealContainer y_1d- grid coordinates along y-axisRealContainer z_all- values at all grid points. 1D array of sizex_1d.size() * y_1d.size().
There are two avaliable storage orders for z_all:
YMajor- default storage order, see left figure.or equivalentJustInterp::BilinearInterpolator<double, JustInterp::YMajor> interpolator(x_1d, y_1d, z_ymajor);
JustInterp::BilinearInterpolator<double> interpolator(x_1d, y_1d, z_ymajor);
XMajor- see right figureJustInterp::BilinearInterpolator<double, JustInterp::XMajor> interpolator(x_1d, y_1d, z_xmajor);
#include "JustInterp/JustInterp.hpp"
std::vector<double> x_1d, y_1d;
std::vector<double> z_ymajor;
// ...
// set grid points x_1d, y_1d along corresponding direction
// ...
// ...
// set z_ymajor values according to YMajor order
// ...
// Define bilinear interpolator and set data
JustInterp::BilinearInterpolator<double> interpolator(x_1d, y_1d, z_major);
// Interpolate at the specific point
double result = interpolator(3.5, 2.0);
// Interpolate at the array of points
std::vector<double> points_x{ 0.3, 0.6, 1.5};
std::vector<double> points_y{-1.3, -0.4, 3.2};
// result is the array of size points_x.size() = points_y.size()
std::vector<double> results = interpolator(points_x, points_y);
// Interpolate to destination grid
std::vector<double> x_1d_dest, y_1d_dest;
// ...
// fill destination grid
// ...
// results_dest is the array of size x_1d_dest.size() * y_1d_dest.size()
// storage order is the same to interpolator's storage order
std::vector<double> results_dest = interpolator.GridInterpolation(x_1d_dest, y_1d_dest);- Using constructor:
or
JustInterp::BilinearInterpolator<double, JustInterp::YMajor> interpolator(x_1d, y_1d, z_ymajor);
Size ofJustInterp::BilinearInterpolator<double, JustInterp::XMajor> interpolator(x_1d, y_1d, z_xmajor);
z_ymajororz_xmajormust be equal tox_1d.size() * y_1d.size(). - Using
SetDatamethod:orJustInterp::BilinearInterpolator<double, JustInterp::YMajor> interpolator; interpolator.SetData(x_1d, y_1d, z_ymajor);JustInterp::BilinearInterpolator<double, JustInterp::XMajor> interpolator; interpolator.SetData(x_1d, y_1d, z_xmajor); - Using
SetDatamethod for data pointers:orJustInterp::BilinearInterpolator<double, JustInterp::YMajor> interpolator; interpolator.SetData(x_1d.size(), y_1d.size(), x_1d.data(), y_1d.data(), z_ymajor.data());JustInterp::BilinearInterpolator<double, JustInterp::XMajor> interpolator; interpolator.SetData(x_1d.size(), y_1d.size(), x_1d.data(), y_1d.data(), z_xmajor.data());
Only bilinear extrapolation based on data from the nearest grid cell is implemented.
The table interpolation is 2D bilinear interpolation for data where the rows do not have the same number of columns, i.e., when the data for interpolation does not constitute a two-dimensional rectangular grid (see figure). If the data is a rectangular grid, then the proper solution is to use BilinearInterpolator, even though in this case TableInterpolator and BilinearInterpolator give the same result.
The interpolation algorithm looks as follow:
- For given point
(x*, y*)find neighboringx_1d[i]andx_1d[i + 1]. - For given
y*perform linear interpolation along y-axis foriandi + 1to yield valuesf1andf2. - Use data
(x_1d[i], f1)and(x_1d[i + 1], f2)for linear interpolation along x-axis.
RealContainer is real-type iterable containers i.e. should support begin(), end(), data(), size() methods, for example, std::vector or std::array.
Input data should be stored as follows:
RealContainer x_1d: 1D array of data points along x-axis.std::vector<RealContainer> y_1d_arrays: vector of 1D arrays of data points along y-axis for each x-value fromx_1darray.y_1d_arrays.size()must be equalx_1d.size().std::vector<RealContainer> z_1d_arrays: vector of 1D arrays of function values along y-axis for each x-value fromx_1darray.z_1d_arrays.size()must be equalx_1d.size()andz_1d_arrays[i].size()must be equaly_1d_arrays[i].size().
#include "JustInterp/JustInterp.hpp"
std::vector<double> x_1d;
std::vector<std::vector<double>> y_1d_arrays;
std::vector<std::vector<double>> z_1d_arrays;
// ...
// set grid points x_1d, y_1d_arrays
// ...
// ...
// set z_1d_arrays
// ...
// Define table interpolator and set data
JustInterp::TableInterpolator<double> interpolator(x_1d, y_1d_arrays, z_1d_arrays);
// Interpolate at the specific point
double result = interpolator(3.5, 2.0);
// Interpolate at the array of points
std::vector<double> points_x{ 0.3, 0.6, 1.5};
std::vector<double> points_y{-1.3, -0.4, 3.2};
// result is the array of size points_x.size() = points_y.size()
std::vector<double> results = interpolator(points_x, points_y);- Using constructor:
JustInterp::TableInterpolator<double> interpolator(x_1d, y_1d_arrays, z_1d_arrays);
- Using
SetDatamethod:JustInterp::TableInterpolator<double> interpolator; interpolator.SetData(x_1d, y_1d_arrays, z_1d_arrays);
There are two avaliable types of extrapolation:
- (Default)
JustInterp::ConstantExtrapolationJustInterp::TableInterpolator<double, JustInterp::ConstantExtrapolation> interpolator; /* or */ JustInterp::TableInterpolator<double> interpolator;
JustInterp::LinearExtrapolationJustInterp::TableInterpolator<double, JustInterp::LinearExtrapolation> interpolator;
First, extrapolation is performed along the y-axis, then extrapolation is performed along the x-axis.


