/*
Computes spline data and caches it inside the object. Both X and Y vectors
are cleared (see Dvector#clear) to make sure the cache is kept up-to-date.
If the function is not sorted, sorts it.
*/
static VALUE function_compute_spline_data(VALUE self)
{
VALUE x_vec = get_x_vector(self);
VALUE y_vec = get_y_vector(self);
VALUE cache = get_spline_vector(self);
long size = DVECTOR_SIZE(x_vec);
if(DVECTOR_SIZE(y_vec) != size)
rb_raise(rb_eRuntimeError,
"x and y should have the same size !");
if(! IS_A_DVECTOR(cache)) /* create it -- and silently ignores
its previous values */
cache = rb_funcall(cDvector, idNew,
1, LONG2NUM(size));
if(DVECTOR_SIZE(cache) != size) /* switch to the required size for cache */
Dvector_Data_Resize(cache, size);
/* we make sure that the X values are sorted */
if(! RTEST(function_is_sorted(self)))
function_sort(self);
double * x, *y, *spline;
x = Dvector_Data_for_Read(x_vec, NULL);
y = Dvector_Data_for_Read(y_vec, NULL);
spline = Dvector_Data_for_Write(cache, NULL);
function_fill_second_derivatives(size, x, y, spline,1.0/0.0, 1.0/0.0);
set_spline_vector(self, cache);
/* now, we clear both X and Y */
DVECTOR_CLEAR(x_vec);
DVECTOR_CLEAR(y_vec);
return self;
}