/* 
   Filters the Function through interpolation. _params_ holds a 
   hash with the following values:
   * ??

   It returns a hash.
*/
static VALUE function_spline_approximation(VALUE self, VALUE params)
{
  long len = function_sanity_check(self);
  const double *x = Dvector_Data_for_Read(get_x_vector(self),NULL);
  const double *y = Dvector_Data_for_Read(get_y_vector(self),NULL);
  VALUE xiret, yiret, y2iret, yintret,ret;
  double * xi, *yi, *y2i, *yint;
  long nbavg = 9;  
  long nbmax = 20;
  if(RTEST(rb_hash_aref(params, rb_str_new2("number"))))
    nbmax = NUM2LONG(rb_hash_aref(params, rb_str_new2("number")));
  if(RTEST(rb_hash_aref(params, rb_str_new2("average"))))
    nbavg = NUM2LONG(rb_hash_aref(params, rb_str_new2("average")));

  /* TODO: add checks that monotonic and growing. */
  
  xiret = rb_funcall(cDvector, idNew, 1, INT2NUM(nbmax)); 
  xi = Dvector_Data_for_Write(xiret, NULL);
  yiret = rb_funcall(cDvector, idNew, 1, INT2NUM(nbmax)); 
  yi = Dvector_Data_for_Write(yiret, NULL);
  y2iret = rb_funcall(cDvector, idNew, 1, INT2NUM(nbmax)); 
  y2i = Dvector_Data_for_Write(y2iret, NULL);
  yintret = rb_funcall(cDvector, idNew, 1, INT2NUM(len)); 
  yint = Dvector_Data_for_Write(yintret, NULL);

  internal_spline_approximation(x, y, len, xi, yi, y2i,
                                nbmax, nbavg, yint);
  ret = rb_hash_new();
  rb_hash_aset(ret, rb_str_new2("xi"), xiret);
  rb_hash_aset(ret, rb_str_new2("yi"), yiret);
  rb_hash_aset(ret, rb_str_new2("y2i"), y2iret);
  rb_hash_aset(ret, rb_str_new2("y"), yintret);
  return ret;
}