Skip to content

Instantly share code, notes, and snippets.

@v0dro
Last active April 16, 2023 13:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save v0dro/7dd58bc13a708385162a21db1cb6b3a5 to your computer and use it in GitHub Desktop.
Save v0dro/7dd58bc13a708385162a21db1cb6b3a5 to your computer and use it in GitHub Desktop.
2D interpolation with GSL in C
#include <stdio.h>
#include <stdlib.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_interp2d.h>
#include <gsl/gsl_spline2d.h>
int main(int argc, char const *argv[])
{
const gsl_interp2d_type *T = gsl_interp2d_bicubic;
double xa[] = {-2.0, -1.0, 0.0, 1.0, 2.0};
double ya[] = {-2.0, -1.0, 0.0, 1.0, 2.0};
double za[] = {0.0, 3.0, 4.0, 3.0, 0.0, -3.0, 0.0, 1.0, 0.0, -3.0, -4.0, -1.0, 0.0, -1.0, -4.0, -3.0, 0.0, 1.0, 0.0, -3.0, 0.0, 3.0, 4.0, 3.0, 0.0};
// double xval[] = {1.0, 1.5, 2.0};
// double yval[] = {1.0, 1.5, 2.0};
// double zval[] = {1.2, 1.3, 1.4};
size_t nx = sizeof(xa) / sizeof(xa[0]);
size_t ny = sizeof(ya) / sizeof(ya[0]);
// size_t test_size = sizeof(xval) / sizeof(xval[0]);
gsl_spline2d *spline = gsl_spline2d_alloc(T, nx, ny);
gsl_interp_accel *xacc = gsl_interp_accel_alloc();
gsl_interp_accel *yacc = gsl_interp_accel_alloc();
gsl_spline2d_init(spline, xa, ya, za, nx, ny);
printf("-1.5 and 1.0 : %f\n", gsl_spline2d_eval(spline,-1.5,1.0,xacc, yacc));
printf("1.0 and -1.5 : %f\n", gsl_spline2d_eval(spline,1.0,-1.5,xacc, yacc));
return 0;
}
// OUTPUT
// -1.5 and 1.0 : -1.339286
// 1.0 and -1.5 : 1.339286
@Otabek92
Copy link

Otabek92 commented Dec 9, 2020

Dear Sir/Madam!
The example is cool. I am dealing with 2D Interpolation in GSL. Please, do you have another examples like above? I really need to learn them for my research. Now I am frozen at the gsl_spline2d_set function. otabek.umarov@science.unideb.hu. This is my email. (I am a PhD student at DU.) I will be very very happy if you can help or contact with me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment