Multivariate Splines is a function approximation library implementing various multivariate splines in C++.
You can find here the Multivariate splines original github page.
The following example demonstrates the use of Multivariate Splines. Note that there are no restrictions to the dimension of x (except that it has to be >= 1, of course).
main_muparser.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | #include <iostream>
#include "lasote/splines/include/datatable.h"
#include "lasote/splines/include/bspline.h"
#include "lasote/splines/include/pspline.h"
#include "lasote/splines/include/rbfspline.h"
using std::cout;
using std::endl;
using namespace MultivariateSplines;
// Six-hump camelback function
double f(DenseVector x)
{
assert(x.rows() == 2);
return (4 - 2.1*x(0)*x(0)
+ (1/3.)*x(0)*x(0)*x(0)*x(0))*x(0)*x(0)
+ x(0)*x(1)
+ (-4 + 4*x(1)*x(1))*x(1)*x(1);
}
int main(int argc, char *argv[])
{
// Create new DataTable to manage samples
DataTable samples;
// Sample function
DenseVector x(2);
double y;
for(int i = 0; i < 20; i++)
{
for(int j = 0; j < 20; j++)
{
// Sample function at x
x(0) = i*0.1;
x(1) = j*0.1;
y = f(x);
// Store sample
samples.addSample(x,y);
}
}
// Build B-splines that interpolate the samples
BSpline bspline1(samples, BSplineType::LINEAR);
BSpline bspline3(samples, BSplineType::CUBIC_FREE);
// Build penalized B-spline (P-spline) that smooths the samples
PSpline pspline(samples, 0.03);
// Build radial basis function spline that interpolate the samples
RBFSpline rbfspline(samples, RadialBasisFunctionType::THIN_PLATE_SPLINE);
// Evaluate the splines at x = (1,1)
x(0) = 1; x(1) = 1;
cout << "-------------------------------------------------" << endl;
cout << "Function at x: \t\t\t" << f(x) << endl;
cout << "Linear B-spline at x: \t\t" << bspline1.eval(x) << endl;
cout << "Cubic B-spline at x: \t\t" << bspline3.eval(x) << endl;
cout << "P-spline at x: \t\t\t" << pspline.eval(x) << endl;
cout << "Thin-plate spline at x:\t\t" << rbfspline.eval(x) << endl;
cout << "-------------------------------------------------" << endl;
return 0;
}
|
Project should be compiled with C++11 so you also need the CMakeLists which specify it:
CMakeLists.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # This CMakeLists.txt file helps defining your block building and compiling
# Include the main biicode macros and functions
# To learn more about the CMake use with biicode, visit http://docs.biicode.com/c++/building.html
# Or check the examples below
include(${CMAKE_HOME_DIRECTORY}/biicode.cmake)
# ACTIVATING C++11 FLAG
IF(APPLE)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++")
ELSEIF (WIN32 OR UNIX)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
ENDIF(APPLE)
ADD_BII_TARGETS()
|
This example is already in biicode, it’s simple to run, just open the block and build it like this:
$ bii init my_project $ cd my_project $ bii open examples/multivariate_splines $ bii build
Only if you want to do it manually, create a cpp file in your block, copy the code above and resolve all the dependencies of the main.cpp
. Create a CMakeLists.txt
file with the content above too. Then you can compile it:
$ bii init my_project $ cd my_project $ bii new my_user/my_block $ # Create a main.cpp file in my_user/my_block and copy the code $ # Create a CMakeLists.txt file in my_user/my_block an copy the code $ bii find $ bii build
You will see next console output after executing the command:
$ cd bin
$ bin $ ././examples_multivariate_splines_splines_main
Computing B-spline control points using dense solver.
Computing B-spline control points using dense solver.
Computing B-spline control points using dense solver.
Computing RBF weights using dense solver.
Error: 6.33755e-14
-------------------------------------------------
Function at x: 3.23333
Linear B-spline at x: 3.23333
Cubic B-spline at x: 3.23333
P-spline at x: 3.23285
Thin-plate spline at x: 3.23333
-------------------------------------------------