Esempio n. 1
0
void 
test_dim (const size_t n, const double a, const double b,
          gsl_function * F, gsl_function * DF, gsl_function *IF)
{
  double tol = 100.0 * GSL_DBL_EPSILON;
  double x; 

  gsl_cheb_series * cs = gsl_cheb_alloc(n);
  gsl_cheb_series * csd = gsl_cheb_alloc(n);
  gsl_cheb_series * csi = gsl_cheb_alloc(n);

  gsl_cheb_init(cs, F, a, b);
  
  for(x=a; x<b; x += (b-a)/100.0) {
    double r = gsl_cheb_eval(cs, x);
    gsl_test_abs(r, GSL_FN_EVAL(F, x), tol, "gsl_cheb_eval, F(%.3g)", x);
  }

  /* Test derivative */

  gsl_cheb_calc_deriv(csd, cs);

  for(x=a; x<b; x += (b-a)/100.0) {
    double r = gsl_cheb_eval(csd, x);
    gsl_test_abs(r, GSL_FN_EVAL(DF, x), tol, "gsl_cheb_eval, deriv F(%.3g)", x);
  }
 
  /* Test integral */

  gsl_cheb_calc_integ(csi, cs);

  for(x=a; x<b; x += (b-a)/100.0) {
    double r = gsl_cheb_eval(csi, x);
    gsl_test_abs(r, GSL_FN_EVAL(IF, x), tol, "gsl_cheb_eval, integ F(%.3g)", x);
  }

  gsl_cheb_free(csi);
  gsl_cheb_free(csd);
  gsl_cheb_free(cs);
}
Esempio n. 2
0
static VALUE rb_gsl_cheb_eval(VALUE obj, VALUE xx)
{
  gsl_cheb_series *p = NULL;
  VALUE x, ary;
  size_t i, j, n;
  gsl_vector *v = NULL, *vnew = NULL;
  gsl_matrix *m = NULL, *mnew = NULL;
  Data_Get_Struct(obj, gsl_cheb_series, p);
  if (CLASS_OF(xx) == rb_cRange) xx = rb_gsl_range2ary(xx);
  switch (TYPE(xx)) {
  case T_FIXNUM:
  case T_BIGNUM:
  case T_FLOAT:
    return rb_float_new(gsl_cheb_eval(p, NUM2DBL(xx)));
    break;
  case T_ARRAY:
    //    n = RARRAY(xx)->len;
    n = RARRAY_LEN(xx);
    ary = rb_ary_new2(n);
    for (i = 0; i < n; i++) {
      x = rb_ary_entry(xx, i);
      Need_Float(xx);
      rb_ary_store(ary, i, rb_float_new(gsl_cheb_eval(p, NUM2DBL(x))));
    }
    return ary;
    break;
  default:
#ifdef HAVE_NARRAY_H
    if (NA_IsNArray(xx)) {
      struct NARRAY *na;
      double *ptr1, *ptr2;
      GetNArray(xx, na);
      ptr1 = (double*) na->ptr;
      n = na->total;
      ary = na_make_object(NA_DFLOAT, na->rank, na->shape, CLASS_OF(xx));
      ptr2 = NA_PTR_TYPE(ary,double*);
      for (i = 0; i < n; i++) ptr2[i] = gsl_cheb_eval(p, ptr1[i]);
      return ary;
    }
#endif
    if (VECTOR_P(xx)) {
      Data_Get_Struct(xx, gsl_vector, v);
      vnew = gsl_vector_alloc(v->size);
      for (i = 0; i < v->size; i++) {
        gsl_vector_set(vnew, i, gsl_cheb_eval(p, gsl_vector_get(v, i)));
      }
      return Data_Wrap_Struct(cgsl_vector, 0, gsl_vector_free, vnew);
    } else if (MATRIX_P(xx)) {
      Data_Get_Struct(xx, gsl_matrix, m);
      mnew = gsl_matrix_alloc(m->size1, m->size2);
      for (i = 0; i < m->size1; i++) {
        for (j = 0; j < m->size2; j++) {
          gsl_matrix_set(mnew, i, j, gsl_cheb_eval(p, gsl_matrix_get(m, i, j)));
        }
      }
      return Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, mnew);
    } else {
      rb_raise(rb_eTypeError, "wrong argument type");
    }
    break;
  }
  return Qnil;   /* never reach here */
}
Esempio n. 3
0
int 
main(void)
{
  double tol = 100.0 * GSL_DBL_EPSILON;
  double ftol = 20.0;
  double x; 
  size_t i;

  gsl_cheb_series * cs = gsl_cheb_alloc(40);
  gsl_cheb_series * csd = gsl_cheb_alloc(40);
  gsl_cheb_series * csi = gsl_cheb_alloc(40);

  gsl_function F_sin, F_T0, F_T1, F_T2, F_DP, F_P, F_IP1, F_IP2;

  F_sin.function = f_sin;
  F_sin.params = 0;

  F_T0.function = f_T0;
  F_T0.params = 0;

  F_T1.function = f_T1;
  F_T1.params = 0;

  F_T2.function = f_T2;
  F_T2.params = 0;

  F_P.function = f_P;
  F_P.params = 0;

  F_DP.function = f_DP;
  F_DP.params = 0;

  F_IP1.function = f_IP1;
  F_IP1.params = 0;

  F_IP2.function = f_IP2;
  F_IP2.params = 0;

  gsl_ieee_env_setup();

  gsl_cheb_init(cs, &F_T0, -1.0, 1.0);

  {
    size_t expected = 40;
    size_t order = gsl_cheb_order (cs);
    size_t size = gsl_cheb_size (cs);
    double * p = gsl_cheb_coeffs (cs);
    gsl_test(order != expected, "gsl_cheb_order");
    gsl_test(size != expected + 1, "gsl_cheb_size");
    gsl_test(p != cs->c, "gsl_cheb_coeffs");
  }

  for (i = 0; i<cs->order; i++)
    {
      double c_exp = (i == 0) ? 2.0 : 0.0;
      gsl_test_abs (cs->c[i], c_exp, tol, "c[%d] for T_0(x)", i);
    }

  gsl_cheb_init(cs, &F_T1, -1.0, 1.0);

  for (i = 0; i<cs->order; i++)
    {
      double c_exp = (i == 1) ? 1.0 : 0.0;
      gsl_test_abs (cs->c[i], c_exp, tol, "c[%d] for T_1(x)", i);
    }

  gsl_cheb_init(cs, &F_T2, -1.0, 1.0);

  for (i = 0; i<cs->order; i++)
    {
      double c_exp = (i == 2) ? 1.0 : 0.0;
      gsl_test_abs (cs->c[i], c_exp, tol, "c[%d] for T_2(x)", i);
    }

  gsl_cheb_init(cs, &F_sin, -M_PI, M_PI);

  gsl_test_abs (cs->c[0], 0.0, tol, "c[0] for F_sin(x)");
  gsl_test_abs (cs->c[1], 5.69230686359506e-01, tol, "c[1] for F_sin(x)");
  gsl_test_abs (cs->c[2], 0.0, tol, "c[2] for F_sin(x)");
  gsl_test_abs (cs->c[3], -6.66916672405979e-01, tol, "c[3] for F_sin(x)");
  gsl_test_abs (cs->c[4], 0.0, tol, "c[4] for F_sin(x)");
  gsl_test_abs (cs->c[5], 1.04282368734237e-01, tol, "c[5] for F_sin(x)");

  for(x=-M_PI; x<M_PI; x += M_PI/100.0) {
    double r = gsl_cheb_eval(cs, x);
    gsl_test_abs(r, sin(x), tol, "gsl_cheb_eval, sin(%.3g)", x);
  }
  
  for(x=-M_PI; x<M_PI; x += M_PI/100.0) {
    double r, e;
    gsl_cheb_eval_err(cs, x, &r, &e);
    gsl_test_abs(r, sin(x), tol, "gsl_cheb_eval_err, sin(%.3g)", x);
    gsl_test_factor(fabs(r-sin(x)) + GSL_DBL_EPSILON, e, ftol, 
                    "gsl_cheb_eval_err, error sin(%.3g)", x);
  }

  for(x=-M_PI; x<M_PI; x += M_PI/100.0) {
    double r = gsl_cheb_eval_n(cs, 25, x);
    gsl_test_abs(r, sin(x), tol, "gsl_cheb_eval_n, sin(%.3g)", x);
  }

  for(x=-M_PI; x<M_PI; x += M_PI/100.0) {
    double r, e;
    gsl_cheb_eval_n_err(cs, 25, x, &r, &e);
    gsl_test_abs(r, sin(x), 100.0 * tol, "gsl_cheb_eval_n_err, deriv sin(%.3g)", x);
    gsl_test_factor(fabs(r-sin(x)) + GSL_DBL_EPSILON, e, ftol, 
                    "gsl_cheb_eval_n_err, error sin(%.3g)", x);
  }

  /* Test derivative */

  gsl_cheb_calc_deriv(csd, cs);

  for(x=-M_PI; x<M_PI; x += M_PI/100.0) {
    double r = gsl_cheb_eval(csd, x);
    gsl_test_abs(r, cos(x), 1600 * tol, "gsl_cheb_eval, deriv sin(%.3g)", x);
  }
  
#ifdef TEST_DERIVATIVE_ERR
  for(x=-M_PI; x<M_PI; x += M_PI/100.0) {
    double r, e;
    gsl_cheb_eval_err(csd, x, &r, &e);
    gsl_test_abs(r, cos(x), tol, "gsl_cheb_eval_err, deriv sin(%.3g)", x);
    gsl_test_factor(fabs(r-cos(x)) + GSL_DBL_EPSILON, e, ftol, 
                    "gsl_cheb_eval_err, deriv error sin(%.3g)", x);
  }
#endif

  for(x=-M_PI; x<M_PI; x += M_PI/100.0) {
    double r = gsl_cheb_eval_n(csd, 25, x);
    gsl_test_abs(r, cos(x), 1600 * tol, "gsl_cheb_eval_n, deriv sin(%.3g)", x);
  }

#ifdef TEST_DERIVATIVE_ERR
  for(x=-M_PI; x<M_PI; x += M_PI/100.0) {
    double r, e;
    gsl_cheb_eval_n_err(csd, 25, x, &r, &e);
    gsl_test_abs(r, cos(x), 100.0 * tol, "gsl_cheb_eval_n_err, deriv sin(%.3g)", x);
    gsl_test_factor(fabs(r-cos(x)) + GSL_DBL_EPSILON, e, ftol, 
                    "gsl_cheb_eval_n_err, deriv error sin(%.3g)", x);
  }
#endif

  /* Test integral */

  gsl_cheb_calc_integ(csi, cs);

  for(x=-M_PI; x<M_PI; x += M_PI/100.0) {
    double r = gsl_cheb_eval(csi, x);
    gsl_test_abs(r, -(1+cos(x)), tol, "gsl_cheb_eval, integ sin(%.3g)", x);
  }
  
#ifdef TEST_INTEGRAL_ERR
  for(x=-M_PI; x<M_PI; x += M_PI/100.0) {
    double r, e;
    gsl_cheb_eval_err(csi, x, &r, &e);
    gsl_test_abs(r, -(1+cos(x)), tol, "gsl_cheb_eval_err, integ sin(%.3g)", x);
    gsl_test_factor(fabs(r-(-1-cos(x))) + GSL_DBL_EPSILON, e, ftol, 
                    "gsl_cheb_eval_err, integ error sin(%.3g)", x);
  }
#endif

  for(x=-M_PI; x<M_PI; x += M_PI/100.0) {
    double r = gsl_cheb_eval_n(csi, 25, x);
    gsl_test_abs(r, -(1+cos(x)), tol, "gsl_cheb_eval_n, integ sin(%.3g)", x);
  }

#ifdef TEST_INTEGRAL_ERR
  for(x=-M_PI; x<M_PI; x += M_PI/100.0) {
    double r, e;
    gsl_cheb_eval_n_err(csi, 25, x, &r, &e);
    gsl_test_abs(r, -(1+cos(x)), 100.0 * tol, "gsl_cheb_eval_n_err, integ sin(%.3g)", x);
    gsl_test_factor(fabs(r-(-1-cos(x))) + GSL_DBL_EPSILON, e, ftol, 
                    "gsl_cheb_eval_n_err, integ error sin(%.3g)", x);
  }
#endif

  gsl_cheb_free(csi);
  gsl_cheb_free(csd);
  gsl_cheb_free(cs);
  
  /* Test low order cases */
  test_dim (2, -5.0, 5.0, &F_P, &F_DP, &F_IP2);
  test_dim (1, -5.0, 5.0, &F_P, &F_DP, &F_IP1);

  exit (gsl_test_summary());
}