Skip to content

Commit

Permalink
Merge branch 'development' of github.com:bgrimstad/SPLINTER into deve…
Browse files Browse the repository at this point in the history
…lopment
  • Loading branch information
Anders Wenhaug committed Mar 25, 2016
2 parents 35ac9d0 + 17d125f commit 9f88cea
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 23 deletions.
8 changes: 4 additions & 4 deletions docs/basic_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ The B-spline may approximate any sampled multivariate function. The user may con

The following Python example shows how to build a quadratic B-spline:
```
bspline = splinter.BSplineBuilder(data, degree=2).build()
bspline = splinter.BSplineBuilder(x, y, degree=2).build()
```
In this example the B-spline `bspline` is built from a dataset called `data`, usually assumed to contain data points on a regular grid. Note that SPLINTER do accept datasets of points not lying on a regular grid, but the behavior is then experimental and currently not advised.
In this example the B-spline `bspline` is built from sample points (`x`, `y`), usually assumed to lie on a regular grid. Note that SPLINTER do accept datasets of points not lying on a regular grid, but the behavior is then experimental and currently not advised.

![Comparison of a linear, quadratic and cubic B-spline](../assets/bspline_degrees.png)

Figure: Comparison of a linear, quadratic and cubic B-spline. The linear B-spline is continuous, the quadratic B-spline is continuously differentiable, and the cubic B-spline has a continous second order derivative.

The user may create a penalized B-spline (P-spline) that smooths the data instead of interpolating it. The construction of a P-spline is more computationally demanding than the B-spline - a large least-square problem must be solved - bringing the practical limit on the number of samples down to about 10 000. The following Python example shows the construction of a P-spline with the smoothing parameter (alpha) set to 0.1.
```
bspline = splinter.BSplineBuilder(data, smoothing=splinter.BSplineBuilder.Smoothing.PSPLINE, alpha=0.1).build()
bspline = splinter.BSplineBuilder(x, y, smoothing=splinter.BSplineBuilder.Smoothing.PSPLINE, alpha=0.1).build()
```

An alternative to the P-spline, that also may reduce the variation in the B-spline is to use [Tikhonov regularization](http://en.wikipedia.org/wiki/Tikhonov_regularization). A quadratic penalty on the coefficients is then added to the OLS objective function. Regularization may be used to decrease the effect of overfitting. Currently, SPLINTER uses an identity matrix as the Tikhonov matrix. The smoothing parameter (alpha) can be set as shown in the following Python example where a cubic B-spline is built.
```
bspline = splinter.BSplineBuilder(data, smoothing=splinter.BSplineBuilder.Smoothing.IDENTITY, alpha=0.1).build()
bspline = splinter.BSplineBuilder(x, y, smoothing=splinter.BSplineBuilder.Smoothing.IDENTITY, alpha=0.1).build()
```

![Comparison of an interpolating B-spline, regularized B-spline, and P-spline](../assets/bspline_regularization.png)
Expand Down
41 changes: 29 additions & 12 deletions docs/c_interface.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
##C interface
As part of making the MATLAB and Python interface we have also made a C interface to the library. The MATLAB and Python interfaces make all their calls through this interface, but you can still use it if you want to. The function names are subject to change (and probably will, because we want to camelCase the function names), and the interface currently pollutes your namespace quite a bit. If you do want to use it, however, note that almost all functions emulate object oriented languages by taking a splinter_obj_ptr as the first argument, and then the rest of the arguments after that.
As part of making the MATLAB and Python interface we have also made a C interface to the library. The MATLAB and Python interfaces make all their calls through this interface, but you can still use it if you want to. Note that almost all functions emulate object oriented languages by taking a splinter_obj_ptr as the first argument, and then the rest of the arguments after that.
splinter_obj_ptr is currently defined as
```c
typedef void *splinter_obj_ptr;
Expand All @@ -8,7 +8,7 @@ typedef void *splinter_obj_ptr;
```c
#include <stdio.h>
#include <stdlib.h>
#include <cinterface.h>
#include <cinterface/cinterface.h>

double f(double x, double y)
{
Expand All @@ -17,8 +17,8 @@ double f(double x, double y)

int main(int argc, char **argv)
{
splinter_obj_ptr datatable = datatable_init();
printf("%s\n", get_error_string());
splinter_obj_ptr datatable = splinter_datatable_init();
printf("%s\n", splinter_get_error_string());

int x_grid = 10;
int y_grid = 10;
Expand All @@ -36,15 +36,32 @@ int main(int argc, char **argv)
}
}

datatable_add_samples_row_major(datatable, samples, n_samples, 2);
printf("%s\n", get_error_string());
splinter_datatable_add_samples_row_major(datatable, samples, n_samples, 2);
if (splinter_get_error()) {
printf("%s\n", splinter_get_error_string());
}

splinter_obj_ptr bspline_builder = splinter_bspline_builder_init(datatable);
if (splinter_get_error()) {
printf("%s\n", splinter_get_error_string());
}
unsigned int degrees[2] = {3, 3};
splinter_bspline_builder_set_degree(bspline_builder, degrees, 2);
if (splinter_get_error()) {
printf("%s\n", splinter_get_error_string());
}

splinter_obj_ptr bspline = bspline_init(datatable, 3);
printf("%s\n", get_error_string());
splinter_obj_ptr bspline = splinter_bspline_builder_build(bspline_builder);
if (splinter_get_error()) {
printf("%s\n", splinter_get_error_string());
}

double x_eval[] = {0.1, 0.5};
double *val = eval_row_major(bspline, x_eval, 2);
printf("%s\n", get_error_string());
double *val = splinter_bspline_eval_row_major(bspline, x_eval, 2);
if (splinter_get_error()) {
printf("%s\n", splinter_get_error_string());
}

printf("Approximated value at (%f, %f): %f\n", x_eval[0], x_eval[1], val[0]);
printf("Exact value at (%f, %f): %f\n", x_eval[0], x_eval[1], f(x_eval[0], x_eval[1]));
Expand All @@ -61,7 +78,7 @@ Approximated value at (0.100000, 0.500000): 0.130000
Exact value at (0.100000, 0.500000): 0.130000
*/
```
Example compiled with: c99 test.c -I../../include/ -L. -lsplinter-1-4 && ./a.out
from ~/SPLINTER/build/release, where libsplinter-1-4.so was located.
Example compiled with: c99 test.c -I../../include/ -L. -lsplinter-3-0 && ./a.out
from ~/SPLINTER/build/release, where libsplinter-3-0.so was located.
If you run into problems with running the produced executable, you should have a look [here](http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html).
2 changes: 1 addition & 1 deletion docs/compile.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Note: This script compiles the library for all architectures with all compilers
* [CMake](http://www.cmake.org/)
* [Git](http://git-scm.com/)
* UNIX: [GCC](https://gcc.gnu.org/) and/or [Clang](http://clang.llvm.org/)
* Windows: [MinGW](http://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/mingw-builds/installer/mingw-w64-install.exe/download) and/or [Visual Studio Community](https://www.visualstudio.com/en-us/downloads/download-visual-studio-vs.aspx)
* Windows: [MinGW](https://sourceforge.net/projects/mingw-w64/) and/or [Visual Studio Community](https://www.visualstudio.com/en-us/downloads/download-visual-studio-vs.aspx)
* Ubuntu: build-essentials (`apt-get install build-essentials`)

####MinGW note:
Expand Down
4 changes: 2 additions & 2 deletions docs/matlab_interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ First, head to the [releases tab](https://github.com/bgrimstad/splinter/releases
- include
- cinterface.h
- matlab
- All files from the MATLAB directory in the repository
- All files from the matlab directory in the repository

- Note that you only need the folders that correspond to your platform.
- 32 bit MATLAB only exist on Windows.
- The numbers in the file name (x-y) corresponds to the SPLINTER version, where x is the major and y is the minor version.
- The numbers in the filename (x-y) corresponds to the SPLINTER version, where x is the major and y is the minor version.

Run setup() (defined in setup.m), which should load SPLINTER for you automatically. You must run this once for each time you start MATLAB.

Expand Down
7 changes: 3 additions & 4 deletions docs/python_interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ First, head to the [releases tab](https://github.com/bgrimstad/splinter/releases
- cinterface.h
- python
- splinter
- All files from the matlab directory in the repository
- All files from the python directory in the repository

- The numbers in the file name (x-y) corresponds to the SPLINTER version, where x is the major and y is the minor version.
- The numbers in the filename (x-y) corresponds to the SPLINTER version, where x is the major and y is the minor version.

Make sure the folder called python is in your path, or that that folder is your current working directory. Then you can do
`import splinter`
and it should automatically load all classes along with the binary for you.

### Basic usage
You can then start using the library by making a DataTable and populate it with samples using addSample().

```python
import splinter
Expand Down Expand Up @@ -62,7 +61,7 @@ bspline.save("myfile.myextension")
# Load BSpline from file:
loadedBSpline = splinter.BSpline("myfile.myextension")
```
Notice that if you are going to evaluate the approximant in more than one point, it is preferred to call eval once, instead of n times.
Notice that if you are going to evaluate the BSpline in more than one point, it is preferred to call eval once, instead of n times.

More examples can be found in the directory `/python/examples`.

Expand Down

0 comments on commit 9f88cea

Please sign in to comment.