gprc/src/kernels.c
2024-09-11 13:10:41 +01:00

56 lines
1.4 KiB
C

#include <math.h>
#include <gsl/gsl_matrix.h>
#include "kernels.h"
/* estimation of sqrt(3) */
#define SQRT3 1.73205080757
int squared_exp ( double sigmaf, double ell, gsl_matrix *A, gsl_matrix *B,
gsl_matrix *result )
{
int i, j, n = A->size2, m = B->size2;
/* ensure that the matrix result is the same size as the matrixs */
if ( result->size1 != n || result->size2 != m )
return 1;
gsl_matrix_set_zero(result);
double r, rval, x, y;
for ( i = 0 ; i < n ; i++ ) {
for ( j = 0 ; j < m ; j++ ) {
x = gsl_matrix_get(A, 0, i);
y = gsl_matrix_get(B, 0, j);
r = sqrt((x - y)*(x - y));
rval = sigmaf*sigmaf*exp(-(r*r)/(2*ell*ell));
gsl_matrix_set(result, i, j, rval);
}
}
return 0;
}
int matern32 ( double sigmaf, double ell, gsl_matrix *A, gsl_matrix *B,
gsl_matrix *result )
{
int i, j, n = A->size2, m = B->size2;
/* ensure that the matrix result is the same size as the matrixs */
if ( result->size1 != n || result->size2 != m )
return 1;
double r, rval, x, y;
for ( i = 0 ; i < n ; i++ ) {
for ( j = 0 ; j < m ; j++ ) {
x = gsl_matrix_get(A, 0, i);
y = gsl_matrix_get(B, 0, j);
r = sqrt((x - y)*(x - y));
/* sigmaf^2*(1+sqrt(3)*r/ell)*exp(-sqrt(3)*r/ell) */
rval = sigmaf*sigmaf*(1+SQRT3*r/ell)*exp(-SQRT3*r/ell);
gsl_matrix_set(result, i, j, rval);
}
}
return 0;
}