I tried to bind interpolation function code from here: https://people.math.sc.edu/Burkardt/c_src/interp/interp.html
to TCL to do fast interpolation for large arrays.
I use interp.i file with next content:
%module interp
%include carrays.i
%array_functions(double, doubleArray);
extern double* interp_linear ( int m, int data_num, double t_data[], double p_data[], int interp_num, double t_interp[] );
Build script is:
swig -tcl interp.i
gcc -c interp.c -I/usr/include/tcl/ -fPIC
gcc -c interp_wrap.c -I/usr/include/tcl/ -fPIC
gcc -shared interp.o interp_wrap.o -o interp.so
The code that from which I run interpolation is:
load interp.so interp
set dimension 1
set data_num 10
set interp_num 5
set xPoints [list 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0]
set yPoints [list 1.0 4.0 9.0 16.0 25.0 36.0 49.0 64.0 81.0 100.0]
set xInterpPoints [list 1.5 2.5 3.5 4.5 5.5]
set xPointsVec [new_doubleArray $data_num]
for {set i 0} {$i < $data_num} {incr i} {
doubleArray_setitem $xPointsVec $i [lindex $xPoints $i]
}
set yPointsVec [new_doubleArray $data_num]
for {set i 0} {$i < $data_num} {incr i} {
doubleArray_setitem $yPointsVec $i [lindex $yPoints $i]
}
set xInterpPointsVec [new_doubleArray $interp_num]
for {set i 0} {$i < $interp_num} {incr i} {
doubleArray_setitem $xInterpPointsVec $i [lindex $xInterpPoints $i]
}
puts [doubleArray_getitem [interp_linear 1 $data_num $xPointsVec $yPointsVec $interp_num $xInterpPointsVec] 0]
Script crashes at last command when I try to apply doubleArray_getitem to the result.
If I run just interp_linear 1 $data_num $xPointsVec $yPointsVec $interp_num $xInterpPointsVec
no error. I assume that there is some access to wrong memory cell that cause segfault, but I don’t understand how to debug that, this is a new area for me. Thank you in advance.
Георгий is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.