rncarpio / linterp Goto Github PK
View Code? Open in Web Editor NEWN-dimensional linear interpolation on a rectangular grid in C++
N-dimensional linear interpolation on a rectangular grid in C++
#include <iostream>
using namespace std;
#include <ctime>
#include "linterp.h"
// return an evenly spaced 1-d grid of doubles.
std::vector<double> linspace(double first, double last, int len) {
std::vector<double> result(len);
double step = (last - first) / (len - 1);
for (int i = 0; i < len; i++) {
result[i] = first + i * step;
}
return result;
}
// the function to interpolate.
double fn(double x1, double x2, double x3) {
return sin(x1 + x2 + x3);
}
int main(int argc, char **argv) {
const int length = 10;
// construct the grid in each dimension.
// note that we will pass in a sequence of iterators pointing to the beginning of each grid
std::vector<double> grid1 = linspace(0.0, 3.0, length);
std::vector<double> grid2 = linspace(0.0, 3.0, length);
std::vector<double> grid3 = linspace(0.0, 3.0, length);
std::vector<std::vector<double>::iterator> grid_iter_list;
grid_iter_list.push_back(grid1.begin());
grid_iter_list.push_back(grid2.begin());
grid_iter_list.push_back(grid3.begin());
// the size of the grid in each dimension
array<int, 3> grid_sizes;
grid_sizes[0] = length;
grid_sizes[1] = length;
grid_sizes[2] = length;
// total number of elements
int num_elements = grid_sizes[0] * grid_sizes[1] * grid_sizes[2];
// fill in the values of f(x) at the gridpoints.
// we will pass in a contiguous sequence, values are assumed to be laid out C-style
std::vector<double> f_values(num_elements);
for (int i = 0; i < grid_sizes[0]; i++) {
for (int j = 0; j < grid_sizes[1]; j++) {
for (int k = 0; k < grid_sizes[2]; k++) {
f_values[i * grid_sizes[0] + j * grid_sizes[1] + k] = fn(
grid1[i], grid2[j], grid3[k]);
}
}
}
// construct the interpolator. the last two arguments are pointers to the underlying data
InterpMultilinear<3, double, false> interp_ML(grid_iter_list.begin(),
grid_sizes.begin(), f_values.data(),
f_values.data() + num_elements);
// interpolate one value
array<double, 3> args = { 1.5, 1.5, 1.5 };
printf("%f, %f, %f -> %f %f\n", args[0], args[1], args[2],
interp_ML.interp(args.begin()), fn(args[0], args[1], args[2]));
return 0;
}
The result is
1.500000, 1.500000, 1.500000 -> 0.000000 -0.977530
Is the interp method thread safe for multilinear class?
Regards
Ghansham
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.