Linear interpolation¶
Linear interpolation is implemented in GammaLib through the GNodeArray
class. This class contains a collection of nodes examples/cpp/interpolate/interpolate.cpp
for the source code):
C++
1double x_i[] = {1.0, 4.0, 6.0};
2double y_i[] = {8.0, 7.0, 2.0};
3GNodeArray nodes(3, x_i);
4for (double x = 0; x < 10.0; x += 0.5) {
5 nodes.set_value(x);
6 double y = y_i[nodes.inx_left()] * nodes.wgt_left() + y_i[nodes.inx_right()] * nodes.wgt_right();
7 std::cout << "x=" << x << " : y=" << y << std::endl;
8}
Python
1x_i = [1.0, 4.0, 6.0]
2y_i = [8.0, 7.0, 2.0]
3nodes = gammalib.GNodeArray(x_i)
4for x in range(20):
5 nodes.set_value(x*0.5)
6 y = y_i[nodes.inx_left()] * nodes.wgt_left() + y_i[nodes.inx_right()] * nodes.wgt_right()
7 print('x=%f : y=%f' % (x, y))
In line 1, the nodes
In lines 4-8, the function is interpolated at a number of values in the
interval
where
The method also works for extrapolation.
For
It is obvious that GNodeArray needs at least 2 node values to operate.