Ejemplo n.º 1
0
int gsl_bspline_deriv(double *x,
                      int *n,
                      int *degree,
                      int *nbreak,
                      int *order,
											int *order_max, 
                      double *x_min,
                      double *x_max,
                      double *quantile_vector,
                      int *knots_int,
                      double *Bx)
{

  int k = *degree + 1; /* k in gsl */
  int ncoeffs;
  size_t i, j;

  gsl_bspline_workspace *bw = gsl_bspline_alloc(k, *nbreak);
  ncoeffs = (int)gsl_bspline_ncoeffs(bw);
  gsl_vector *dBorder = gsl_vector_alloc(ncoeffs);
  gsl_bspline_deriv_workspace *derivWS = gsl_bspline_deriv_alloc(k);
	gsl_matrix *dB = gsl_matrix_alloc(ncoeffs, *order_max+1);

	gsl_vector *quantile_vec = gsl_vector_alloc(*nbreak);

	/* 7/12/10 added support for quantile knots */

	if(*knots_int == 0) {
			gsl_bspline_knots_uniform(*x_min, *x_max, bw);
	} else {
			for(i = 0; i < *nbreak; i++) gsl_vector_set(quantile_vec, i, quantile_vector[i]);
			gsl_bspline_knots(quantile_vec, bw);
	}

	for (i = 0; i < *n; ++i)
	{

			/* compute B_j(xi) for all j */
			gsl_bspline_deriv_eval(x[i], order[i], dB, bw, derivWS);

			/* fill in row i of Bx */
			gsl_matrix_get_col(dBorder, dB, order[i]);

			for (j = 0; j < ncoeffs; ++j)
			{
					double Bj = gsl_vector_get(dBorder, j);
					Bx[i*ncoeffs+j] = Bj;
			}
	}

	gsl_bspline_free(bw);
	gsl_vector_free(dBorder);
	gsl_matrix_free(dB);
	/*  gsl_vector_free(quantile_vec);*/
	gsl_bspline_deriv_free(derivWS);

	return(0);

} /* main() */
Ejemplo n.º 2
0
void BSplineInterpolation::fitFromData(const Samples &samples) {

    /* preprocess samples and extract some info */
    Samples ssamples = samples;
    std::sort(ssamples.begin(), ssamples.end());
    const int numSamples = ssamples.size();
    const float minSampleX = ssamples[0].first;
    const float maxSampleX = ssamples.back().first;

    /* prepare fitting data */
    gsl_vector *x = gsl_vector_alloc(ssamples.size());
    gsl_vector *y = gsl_vector_alloc(ssamples.size());

    for (int i=0; i<ssamples.size(); i++) {
        gsl_vector_set(x, i, ssamples[i].first);
        gsl_vector_set(y, i, ssamples[i].second);
    }

    /* uniform knots distributed in sample range */
    gsl_bspline_knots_uniform(minSampleX, maxSampleX, bSplineWorkspace);

    /* construct a fit matrix */
    gsl_matrix *fitMatrix = gsl_matrix_alloc(numSamples, nCoeffs);
    for (int i=0; i<numSamples; i++) {
        /* compute B_j(xi) for all j */
        double xi = gsl_vector_get(x, i);
        gsl_bspline_eval(xi, bSpline, bSplineWorkspace);

        /* fill in row i */
        for (int j=0; j<nCoeffs; j++) {
            double Bj = gsl_vector_get(bSpline, j);
            gsl_matrix_set(fitMatrix, i, j, Bj);
        }
    }

    /* fit spline to data */
    gsl_multifit_linear_workspace *mws = 
        gsl_multifit_linear_alloc(numSamples, nCoeffs);
    double chisq;
    size_t rank;
    double tol = 0.1;
    gsl_multifit_linear(fitMatrix, y, cParameters, covMatrix, &chisq, mws);
    //gsl_multifit_linear_svd(fitMatrix, y, tol,
    //        &rank, cParameters, covMatrix, &chisq, mws);

    splineMinX = minSampleX;
    splineMaxX = maxSampleX;

    /* clean up */
    gsl_vector_free(x);
    gsl_vector_free(y);
    gsl_matrix_free(fitMatrix);
    gsl_multifit_linear_free(mws);
    
}
Ejemplo n.º 3
0
int gsl_bspline(double *x,
                int *n,
                int *degree,
                int *nbreak,
                double *x_min,
                double *x_max,
                double *quantile_vector,
                int *knots_int,
                double *Bx)
{

  int k = *degree + 1; /* k in gsl */
  int ncoeffs;
  int i, j;

  gsl_bspline_workspace *bw = gsl_bspline_alloc(k, *nbreak);
  ncoeffs = (int)gsl_bspline_ncoeffs(bw);  /* *nbreak+k-2 */
  gsl_vector *B = gsl_vector_alloc(ncoeffs);
  gsl_vector *quantile_vec = gsl_vector_alloc(*nbreak);

  /* 7/12/10 added support for quantile knots */

  if(*knots_int == 0) {
    gsl_bspline_knots_uniform(*x_min, *x_max, bw);
  } else {
    for(i = 0; i < *nbreak; i++) gsl_vector_set(quantile_vec, i, quantile_vector[i]);
    gsl_bspline_knots(quantile_vec, bw);
  }

  for (i = 0; i < *n; ++i)
    {

      /* compute B_j(xi) for all j */
      gsl_bspline_eval(x[i], B, bw);
      
      /* fill in row i of Bx */
      for (j = 0; j < ncoeffs; ++j)
        {
          double Bj = gsl_vector_get(B, j);
          Bx[i*ncoeffs+j] = Bj; /* Bx:*n-by-(*nbreak+*degree-1) */
        }
    }
  
  gsl_bspline_free(bw);
  gsl_vector_free(B);
  gsl_vector_free(quantile_vec);

  return(0);

} /* main() */
Ejemplo n.º 4
0
int
main (int argc, char **argv)
{
  int status = 0;
  size_t order, breakpoints, i;

  gsl_ieee_env_setup ();

  argc = 0;                     /* prevent warnings about unused parameters */
  argv = 0;

  for (order = 1; order < 10; order++)
    {
      for (breakpoints = 2; breakpoints < 100; breakpoints++)
        {
          double a = -1.23 * order, b = 45.6 * order;
          gsl_bspline_workspace *bw = gsl_bspline_alloc (order, breakpoints);
          gsl_bspline_knots_uniform (a, b, bw);
          test_bspline (bw);
          gsl_bspline_free (bw);
        }
    }


  for (order = 1; order < 10; order++)
    {
      for (breakpoints = 2; breakpoints < 100; breakpoints++)
        {
          double a = -1.23 * order, b = 45.6 * order;
          gsl_bspline_workspace *bw = gsl_bspline_alloc (order, breakpoints);
          gsl_vector *k = gsl_vector_alloc (breakpoints);
          for (i = 0; i < breakpoints; i++)
            {
              double f, x;
              f = sqrt (i / (breakpoints - 1.0));
              x = (1 - f) * a + f * b;
              gsl_vector_set (k, i, x);
            };
          gsl_bspline_knots (k, bw);
          test_bspline (bw);
          gsl_vector_free (k);
          gsl_bspline_free (bw);
        }
    }

  exit (gsl_test_summary ());
}
Ejemplo n.º 5
0
    /**
     * Recalculate the B-spline knots
     */
    void BSpline::resetKnots()
    {
        bool isUniform = getAttribute("Uniform").asBool();

        std::vector<double> breakPoints;
        if ( isUniform )
        {
            // create uniform knots in the interval [StartX, EndX]
            double startX = getAttribute("StartX").asDouble();
            double endX = getAttribute("EndX").asDouble();
            gsl_bspline_knots_uniform( startX, endX, m_bsplineWorkspace.get() );
            getGSLBreakPoints( breakPoints );
            storeAttributeValue( "BreakPoints", Attribute(breakPoints) );
        }
        else
        {
            // set the break points from BreakPoints vector attribute, update other attributes
            breakPoints = getAttribute( "BreakPoints" ).asVector();
            // check that points are in ascending order
            double prev = breakPoints[0];
            for(size_t i = 1; i < breakPoints.size(); ++i)
            {
                double next = breakPoints[i];
                if ( next <= prev )
                {
                    throw std::invalid_argument("BreakPoints must be in ascending order.");
                }
                prev = next;
            }
            int nbreaks = getAttribute( "NBreak" ).asInt();
            // if number of break points change do necessary updates
            if ( static_cast<size_t>(nbreaks) != breakPoints.size() )
            {
                storeAttributeValue("NBreak", Attribute(static_cast<int>(breakPoints.size())) );
                resetGSLObjects();
                resetParameters();
            }
            GSLVector bp = breakPoints;
            gsl_bspline_knots( bp.gsl(), m_bsplineWorkspace.get() );
            storeAttributeValue( "StartX", Attribute(breakPoints.front()) );
            storeAttributeValue( "EndX", Attribute(breakPoints.back()) );
        }
    }
Ejemplo n.º 6
0
static VALUE rb_gsl_bspline_knots_uniform(int argc, VALUE *argv, VALUE obj)
{
  gsl_bspline_workspace *w;
  int argc2;
  switch (TYPE(obj)) {
  case T_MODULE:
  case T_CLASS:
  case T_OBJECT:
    if (!rb_obj_is_kind_of(argv[argc-1], cBSWS)) {
      rb_raise(rb_eTypeError, "Wrong argument type %s (GSL::BSpline expected)",
               rb_class2name(CLASS_OF(argv[argc-1])));
    }
    Data_Get_Struct(argv[argc-1], gsl_bspline_workspace, w);
    argc2 = argc-1;
    break;
  default:
    Data_Get_Struct(obj, gsl_bspline_workspace, w);
    argc2 = argc;
  }
  if (argc2 != 2) rb_raise(rb_eArgError, "Wrong number of arguments.");
  gsl_bspline_knots_uniform(NUM2DBL(argv[0]), NUM2DBL(argv[1]), w);
  return Data_Wrap_Struct(cgsl_vector_view_ro, 0, NULL, w->knots);
}
Ejemplo n.º 7
0
int
main (void)
{
  const size_t n = N;
  const size_t ncoeffs = NCOEFFS;
  const size_t nbreak = NBREAK;
  size_t i, j;
  gsl_bspline_workspace *bw;
  gsl_vector *B;
  double dy;
  gsl_rng *r;
  gsl_vector *c, *w;
  gsl_vector *x, *y;
  gsl_matrix *X, *cov;
  gsl_multifit_linear_workspace *mw;
  double chisq;
  double Rsq;
  double dof;

  gsl_rng_env_setup();
  r = gsl_rng_alloc(gsl_rng_default);

  /* allocate a cubic bspline workspace (k = 4) */
  bw = gsl_bspline_alloc(4, nbreak);
  B = gsl_vector_alloc(ncoeffs);

  x = gsl_vector_alloc(n);
  y = gsl_vector_alloc(n);
  X = gsl_matrix_alloc(n, ncoeffs);
  c = gsl_vector_alloc(ncoeffs);
  w = gsl_vector_alloc(n);
  cov = gsl_matrix_alloc(ncoeffs, ncoeffs);
  mw = gsl_multifit_linear_alloc(n, ncoeffs);

  printf("#m=0,S=0\n");
  /* this is the data to be fitted */
  for (i = 0; i < n; ++i)
    {
      double sigma;
      double xi = (15.0 / (N - 1)) * i;
      double yi = cos(xi) * exp(-0.1 * xi);

      sigma = 0.1 * yi;
      dy = gsl_ran_gaussian(r, sigma);
      yi += dy;

      gsl_vector_set(x, i, xi);
      gsl_vector_set(y, i, yi);
      gsl_vector_set(w, i, 1.0 / (sigma * sigma));

      printf("%f %f\n", xi, yi);
    }

  /* use uniform breakpoints on [0, 15] */
  gsl_bspline_knots_uniform(0.0, 15.0, bw);

  /* construct the fit matrix X */
  for (i = 0; i < n; ++i)
    {
      double xi = gsl_vector_get(x, i);

      /* compute B_j(xi) for all j */
      gsl_bspline_eval(xi, B, bw);

      /* fill in row i of X */
      for (j = 0; j < ncoeffs; ++j)
        {
          double Bj = gsl_vector_get(B, j);
          gsl_matrix_set(X, i, j, Bj);
        }
    }

  /* do the fit */
  gsl_multifit_wlinear(X, w, y, c, cov, &chisq, mw);

  dof = n - ncoeffs;
  Rsq = 1.0 - chisq / gsl_stats_wtss(w->data, 1, y->data, 1, y->size);

  fprintf(stderr, "chisq/dof = %e, Rsq = %f\n", chisq / dof, Rsq);

  /* output the smoothed curve */
  {
    double xi, yi, yerr;

    printf("#m=1,S=0\n");
    for (xi = 0.0; xi < 15.0; xi += 0.1)
      {
        gsl_bspline_eval(xi, B, bw);
        gsl_multifit_linear_est(B, c, cov, &yi, &yerr);
        printf("%f %f\n", xi, yi);
      }
  }

  gsl_rng_free(r);
  gsl_bspline_free(bw);
  gsl_vector_free(B);
  gsl_vector_free(x);
  gsl_vector_free(y);
  gsl_matrix_free(X);
  gsl_vector_free(c);
  gsl_vector_free(w);
  gsl_matrix_free(cov);
  gsl_multifit_linear_free(mw);

  return 0;
} /* main() */
Ejemplo n.º 8
0
/** Executes the algorithm
 *
 */
void SplineBackground::exec()
{

  API::MatrixWorkspace_sptr inWS = getProperty("InputWorkspace");
  int spec = getProperty("WorkspaceIndex");

  if (spec > static_cast<int>(inWS->getNumberHistograms()))
    throw std::out_of_range("WorkspaceIndex is out of range.");

  const MantidVec& X = inWS->readX(spec);
  const MantidVec& Y = inWS->readY(spec);
  const MantidVec& E = inWS->readE(spec);
  const bool isHistogram = inWS->isHistogramData();

  const int ncoeffs = getProperty("NCoeff");
  const int k = 4; // order of the spline + 1 (cubic)
  const int nbreak = ncoeffs - (k - 2);

  if (nbreak <= 0)
    throw std::out_of_range("Too low NCoeff");

  gsl_bspline_workspace *bw;
  gsl_vector *B;

  gsl_vector *c, *w, *x, *y;
  gsl_matrix *Z, *cov;
  gsl_multifit_linear_workspace *mw;
  double chisq;

  int n = static_cast<int>(Y.size());
  bool isMasked = inWS->hasMaskedBins(spec);
  std::vector<int> masked(Y.size());
  if (isMasked)
  {
    for(API::MatrixWorkspace::MaskList::const_iterator it=inWS->maskedBins(spec).begin();it!=inWS->maskedBins(spec).end();++it)
      masked[it->first] = 1;
    n -= static_cast<int>(inWS->maskedBins(spec).size());
  }

  if (n < ncoeffs)
  {
    g_log.error("Too many basis functions (NCoeff)");
    throw std::out_of_range("Too many basis functions (NCoeff)");
  }

  /* allocate a cubic bspline workspace (k = 4) */
  bw = gsl_bspline_alloc(k, nbreak);
  B = gsl_vector_alloc(ncoeffs);

  x = gsl_vector_alloc(n);
  y = gsl_vector_alloc(n);
  Z = gsl_matrix_alloc(n, ncoeffs);
  c = gsl_vector_alloc(ncoeffs);
  w = gsl_vector_alloc(n);
  cov = gsl_matrix_alloc(ncoeffs, ncoeffs);
  mw = gsl_multifit_linear_alloc(n, ncoeffs);

  /* this is the data to be fitted */
  int j = 0;
  for (MantidVec::size_type i = 0; i < Y.size(); ++i)
  {
    if (isMasked && masked[i]) continue;
    gsl_vector_set(x, j, (isHistogram ? (0.5*(X[i]+X[i+1])) : X[i])); // Middle of the bins, if a histogram
    gsl_vector_set(y, j, Y[i]);
    gsl_vector_set(w, j, E[i]>0.?1./(E[i]*E[i]):0.);

    ++j;
  }

  if (n != j)
  {
    gsl_bspline_free(bw);
    gsl_vector_free(B);
    gsl_vector_free(x);
    gsl_vector_free(y);
    gsl_matrix_free(Z);
    gsl_vector_free(c);
    gsl_vector_free(w);
    gsl_matrix_free(cov);
    gsl_multifit_linear_free(mw);

    throw std::runtime_error("Assertion failed: n != j");
  }

  double xStart = X.front();
  double xEnd =   X.back();

  /* use uniform breakpoints */
  gsl_bspline_knots_uniform(xStart, xEnd, bw);

  /* construct the fit matrix X */
  for (int i = 0; i < n; ++i)
  {
    double xi=gsl_vector_get(x, i);

    /* compute B_j(xi) for all j */
    gsl_bspline_eval(xi, B, bw);

    /* fill in row i of X */
    for (j = 0; j < ncoeffs; ++j)
    {
      double Bj = gsl_vector_get(B, j);
      gsl_matrix_set(Z, i, j, Bj);
    }
  }

  /* do the fit */
  gsl_multifit_wlinear(Z, w, y, c, cov, &chisq, mw);

  /* output the smoothed curve */
  API::MatrixWorkspace_sptr outWS = WorkspaceFactory::Instance().create(inWS,1,X.size(),Y.size());
  {
    outWS->getAxis(1)->setValue(0, inWS->getAxis(1)->spectraNo(spec));
    double xi, yi, yerr;
    for (MantidVec::size_type i=0;i<Y.size();i++)
    {
      xi = X[i];
      gsl_bspline_eval(xi, B, bw);
      gsl_multifit_linear_est(B, c, cov, &yi, &yerr);
      outWS->dataY(0)[i] = yi;
      outWS->dataE(0)[i] = yerr;
    }
    outWS->dataX(0) = X;
  }

  gsl_bspline_free(bw);
  gsl_vector_free(B);
  gsl_vector_free(x);
  gsl_vector_free(y);
  gsl_matrix_free(Z);
  gsl_vector_free(c);
  gsl_vector_free(w);
  gsl_matrix_free(cov);
  gsl_multifit_linear_free(mw);

  setProperty("OutputWorkspace",outWS);

}
Ejemplo n.º 9
0
// [[Rcpp::export]]
Rcpp::List fitData(Rcpp::DataFrame ds) {

    const size_t ncoeffs = NCOEFFS;
    const size_t nbreak = NBREAK;

    const size_t n = N;
    size_t i, j;

    Rcpp::DataFrame D(ds);    		// construct the data.frame object
    RcppGSL::vector<double> y = D["y"];	// access columns by name, 
    RcppGSL::vector<double> x = D["x"];	// assigning to GSL vectors
    RcppGSL::vector<double> w = D["w"];

    gsl_bspline_workspace *bw;
    gsl_vector *B;
    gsl_vector *c; 
    gsl_matrix *X, *cov;
    gsl_multifit_linear_workspace *mw;
    double chisq, Rsq, dof, tss;

    bw = gsl_bspline_alloc(4, nbreak);	    // allocate a cubic bspline workspace (k = 4)
    B = gsl_vector_alloc(ncoeffs);

    X = gsl_matrix_alloc(n, ncoeffs);
    c = gsl_vector_alloc(ncoeffs);
    cov = gsl_matrix_alloc(ncoeffs, ncoeffs);
    mw = gsl_multifit_linear_alloc(n, ncoeffs);

    gsl_bspline_knots_uniform(0.0, 15.0, bw);	// use uniform breakpoints on [0, 15] 

    for (i = 0; i < n; ++i) {			// construct the fit matrix X 
        double xi = gsl_vector_get(x, i);

        gsl_bspline_eval(xi, B, bw);		// compute B_j(xi) for all j 

        for (j = 0; j < ncoeffs; ++j) {		// fill in row i of X 
            double Bj = gsl_vector_get(B, j);
            gsl_matrix_set(X, i, j, Bj);
        }
    }

    gsl_multifit_wlinear(X, w, y, c, cov, &chisq, mw);  // do the fit 
    
    dof = n - ncoeffs;
    tss = gsl_stats_wtss(w->data, 1, y->data, 1, y->size);
    Rsq = 1.0 - chisq / tss;
    
    Rcpp::NumericVector FX(151), FY(151);	// output the smoothed curve 
    double xi, yi, yerr;
    for (xi = 0.0, i=0; xi < 15.0; xi += 0.1, i++) {
        gsl_bspline_eval(xi, B, bw);
        gsl_multifit_linear_est(B, c, cov, &yi, &yerr);
        FX[i] = xi;
        FY[i] = yi;
    }

    Rcpp::List res =
      Rcpp::List::create(Rcpp::Named("X")=FX,
                         Rcpp::Named("Y")=FY,
			 Rcpp::Named("chisqdof")=Rcpp::wrap(chisq/dof),
			 Rcpp::Named("rsq")=Rcpp::wrap(Rsq));

    gsl_bspline_free(bw);
    gsl_vector_free(B);
    gsl_matrix_free(X);
    gsl_vector_free(c);
    gsl_matrix_free(cov);
    gsl_multifit_linear_free(mw);
    
    y.free();
    x.free();
    w.free();

    return(res);   
}
Ejemplo n.º 10
0
int
gsl_bspline_knots_greville (const gsl_vector *abscissae,
                            gsl_bspline_workspace *w,
                            double *abserr)
{
  int s;

  /* Check incoming arguments satisfy mandatory algorithmic assumptions */
  if (w->k < 2)
    GSL_ERROR ("w->k must be at least 2", GSL_EINVAL);
  else if (abscissae->size < 2)
    GSL_ERROR ("abscissae->size must be at least 2", GSL_EINVAL);
  else if (w->nbreak != abscissae->size - w->k + 2)
    GSL_ERROR ("w->nbreak must equal abscissae->size - w->k + 2", GSL_EINVAL);

  if (w->nbreak == 2)
    {
      /* No flexibility in abscissae values possible in this degenerate case */
      s = gsl_bspline_knots_uniform (
              gsl_vector_get (abscissae, 0),
              gsl_vector_get (abscissae, abscissae->size - 1), w);
    }
  else
    {
      double * storage;
      gsl_matrix_view A;
      gsl_vector_view tau, b, x, r;
      size_t i, j;

      /* Constants derived from the B-spline workspace and abscissae details */
      const size_t km2    = w->k - 2;
      const size_t M      = abscissae->size - 2;
      const size_t N      = w->nbreak - 2;
      const double invkm1 = 1.0 / w->km1;

      /* Allocate working storage and prepare multiple, zero-filled views */
      storage = (double *) calloc (M*N + 2*N + 2*M, sizeof (double));
      if (storage == 0)
        GSL_ERROR ("failed to allocate working storage", GSL_ENOMEM);
      A   = gsl_matrix_view_array (storage, M, N);
      tau = gsl_vector_view_array (storage + M*N,             N);
      b   = gsl_vector_view_array (storage + M*N + N,         M);
      x   = gsl_vector_view_array (storage + M*N + N + M,     N);
      r   = gsl_vector_view_array (storage + M*N + N + M + N, M);

      /* Build matrix from interior breakpoints to interior Greville abscissae.
       * For example, when w->k = 4 and w->nbreak = 7 the matrix is
       *   [   1,      0,      0,      0,      0;
       *     2/3,    1/3,      0,      0,      0;
       *     1/3,    1/3,    1/3,      0,      0;
       *       0,    1/3,    1/3,    1/3,      0;
       *       0,      0,    1/3,    1/3,    1/3;
       *       0,      0,      0,    1/3,    2/3;
       *       0,      0,      0,      0,      1  ]
       * but only center formed as first/last breakpoint is known.
       */
      for (j = 0; j < N; ++j)
        for (i = 0; i <= km2; ++i)
          gsl_matrix_set (&A.matrix, i+j, j, invkm1);

      /* Copy interior collocation points from abscissae into b */
      for (i = 0; i < M; ++i)
        gsl_vector_set (&b.vector, i, gsl_vector_get (abscissae, i+1));

      /* Adjust b to account for constraint columns not stored in A */
      for (i = 0; i < km2; ++i)
        {
          double * const v = gsl_vector_ptr (&b.vector, i);
          *v -= (1 - (i+1)*invkm1) * gsl_vector_get (abscissae, 0);
        }
      for (i = 0; i < km2; ++i)
        {
          double * const v = gsl_vector_ptr (&b.vector, M - km2 + i);
          *v -= (i+1)*invkm1 * gsl_vector_get (abscissae, abscissae->size - 1);
        }

      /* Perform linear least squares to determine interior breakpoints */
      s =  gsl_linalg_QR_decomp (&A.matrix, &tau.vector)
        || gsl_linalg_QR_lssolve (&A.matrix, &tau.vector,
                                  &b.vector, &x.vector, &r.vector);
      if (s)
        {
          free (storage);
          return s;
        }

      /* "Expand" solution x by adding known first and last breakpoints. */
      x = gsl_vector_view_array_with_stride (
          gsl_vector_ptr (&x.vector, 0) - x.vector.stride,
          x.vector.stride, x.vector.size + 2);
      gsl_vector_set (&x.vector, 0, gsl_vector_get (abscissae, 0));
      gsl_vector_set (&x.vector, x.vector.size - 1,
                      gsl_vector_get (abscissae, abscissae->size - 1));

      /* Finally, initialize workspace knots using the now-known breakpoints */
      s = gsl_bspline_knots (&x.vector, w);
      free (storage);
    }

  /* Sum absolute errors in the resulting vs requested interior abscissae */
  /* Provided as a fit quality metric which may be monitored by callers */
  if (!s && abserr)
    {
      size_t i;
      *abserr = 0;
      for (i = 1; i < abscissae->size - 1; ++i)
        *abserr += fabs (   gsl_bspline_greville_abscissa (i, w)
                          - gsl_vector_get (abscissae, i) );
    }

  return s;
}
Ejemplo n.º 11
0
  TransformationModelBSpline::TransformationModelBSpline(
    const TransformationModel::DataPoints & data, const Param & params)
  {
    params_ = params;
    Param defaults;
    getDefaultParameters(defaults);
    params_.setDefaults(defaults);

    if (data.size() < 4)     // TODO: check number
    {
      throw Exception::IllegalArgument(__FILE__, __LINE__, __PRETTY_FUNCTION__, "'b_spline' model needs at least four data points");
    }
    Size num_breakpoints = params_.getValue("num_breakpoints");
    String break_positions = params_.getValue("break_positions");
    if ((break_positions != "uniform") && (break_positions != "quantiles"))
    {
      throw Exception::IllegalArgument(__FILE__, __LINE__, __PRETTY_FUNCTION__, "parameter 'break_positions' for 'b_spline' model must be 'uniform' or 'quantiles'");
    }

    size_ = data.size();
    x_ = gsl_vector_alloc(size_);
    y_ = gsl_vector_alloc(size_);
    w_ = gsl_vector_alloc(size_);
    for (size_t i = 0; i < size_; ++i)
    {
      gsl_vector_set(x_, i, data[i].first);
      gsl_vector_set(y_, i, data[i].second);
      gsl_vector_set(w_, i, 1.0);       // TODO: non-uniform weights
    }
    gsl_vector_minmax(x_, &xmin_, &xmax_);

    // set up cubic (k = 4) spline workspace:
    if (num_breakpoints < 2)
    {
      num_breakpoints = 2;
      LOG_WARN << "Warning: Increased parameter 'num_breakpoints' to 2 (minimum)." << endl;
    }
    else if (num_breakpoints > size_ - 2)
    {
      num_breakpoints = size_ - 2;
      LOG_WARN << "Warning: Decreased parameter 'num_breakpoints' to " + String(num_breakpoints) + " (maximum for this number of data points)." << endl;
    }
    workspace_ = gsl_bspline_alloc(4, num_breakpoints);
    if (break_positions == "uniform")
    {
      gsl_bspline_knots_uniform(xmin_, xmax_, workspace_);
    }
    else
    {
      vector<double> quantiles(num_breakpoints, 1.0);
      double step = 1.0 / (num_breakpoints - 1);
      for (Size i = 0; i < num_breakpoints - 1; ++i)
      {
        quantiles[i] = i * step;
      }
      gsl_vector * breakpoints;
      breakpoints = gsl_vector_alloc(num_breakpoints);
      getQuantiles_(x_, quantiles, breakpoints);
      gsl_bspline_knots(breakpoints, workspace_);
      gsl_vector_free(breakpoints);
    }
    ncoeffs_ = gsl_bspline_ncoeffs(workspace_);
    gsl_vector_minmax(workspace_->knots, &xmin_, &xmax_);
    computeFit_();
  }
Ejemplo n.º 12
0
void bSplineGSLOriginalDemo ()
{
	  const size_t n = N;
	  const size_t ncoeffs = NCOEFFS;
	  const size_t nbreak = NBREAK;
	  size_t i, j;
	  gsl_bspline_workspace *bw;
	  gsl_vector *B;
	  double dy;
	  gsl_rng *r;
	  gsl_vector *c, *w;
	  gsl_vector *x, *y;
	  gsl_matrix *X, *cov;
	  gsl_multifit_linear_workspace *mw;
	  double chisq, Rsq, dof, tss;
	  vector<double> xControl, yControl, xFit, yFit;

	  gsl_rng_env_setup();
	  r = gsl_rng_alloc(gsl_rng_default);

	  /* allocate a cubic bspline workspace (k = 4) */
	  bw = gsl_bspline_alloc(4, nbreak);
	  B = gsl_vector_alloc(ncoeffs);

	  x = gsl_vector_alloc(n);
	  y = gsl_vector_alloc(n);
	  X = gsl_matrix_alloc(n, ncoeffs);
	  c = gsl_vector_alloc(ncoeffs);
	  w = gsl_vector_alloc(n);
	  cov = gsl_matrix_alloc(ncoeffs, ncoeffs);
	  mw = gsl_multifit_linear_alloc(n, ncoeffs);

	  printf("#m=0,S=0\n");
	  /* this is the data to be fitted */
	  for (i = 0; i < n; ++i)
		 {
		   double sigma;
		   double xi = (15.0 / (N - 1)) * i;
		   double yi = cos(xi) * exp(-0.1 * xi);

		   sigma = 0.1 * yi;
		   dy = gsl_ran_gaussian(r, sigma);
		   yi += dy;

		   gsl_vector_set(x, i, xi);
			xControl.push_back(xi);
		   gsl_vector_set(y, i, yi);
			yControl.push_back(yi);
		   gsl_vector_set(w, i, 1.0 / (sigma * sigma));

		   printf("%f %f\n", xi, yi);
		 }

	  /* use uniform breakpoints on [0, 15] */
	  gsl_bspline_knots_uniform(0.0, 15.0, bw);

	  /* construct the fit matrix X */
	  for (i = 0; i < n; ++i)
		 {
		   double xi = gsl_vector_get(x, i);

		   /* compute B_j(xi) for all j */
		   gsl_bspline_eval(xi, B, bw);

		   /* fill in row i of X */
		   for (j = 0; j < ncoeffs; ++j)
		     {
		       double Bj = gsl_vector_get(B, j);
		       gsl_matrix_set(X, i, j, Bj);
		     }
		 }

	  /* do the fit */
	  gsl_multifit_wlinear(X, w, y, c, cov, &chisq, mw);

	  dof = n - ncoeffs;
	  tss = gsl_stats_wtss(w->data, 1, y->data, 1, y->size);
	  Rsq = 1.0 - chisq / tss;

	  fprintf(stderr, "chisq/dof = %e, Rsq = %f\n", 
		                chisq / dof, Rsq);

	  /* output the smoothed curve */
	  {
		 double xi, yi, yerr;

		 printf("#m=1,S=0\n");
		 for (xi = 0.0; xi < 15.0; xi += 0.1)
		   {
		     gsl_bspline_eval(xi, B, bw);
		     gsl_multifit_linear_est(B, c, cov, &yi, &yerr);

			  xFit.push_back(xi);
			  yFit.push_back(yi);
		     printf("%f %f\n", xi, yi);
		   }
	  }

	  gsl_rng_free(r);
	  gsl_bspline_free(bw);
	  gsl_vector_free(B);
	  gsl_vector_free(x);
	  gsl_vector_free(y);
	  gsl_matrix_free(X);
	  gsl_vector_free(c);
	  gsl_vector_free(w);
	  gsl_matrix_free(cov);
	  gsl_multifit_linear_free(mw);

     TGraph *gr = LoadGraphFromVectors(xControl, yControl);
	  TGraph *grFit = LoadGraphFromVectors(xFit, yFit);
     gr->SetMarkerColor(kRed);
	  TCanvas *c1 = new TCanvas("c1", "Graph", 200, 10, 700, 500);
     gr->Draw("apz");
	  grFit->Draw("SAME");

     c1->Update();
} 
Ejemplo n.º 13
0
Archivo: fit.c Proyecto: savila/HALOGEN
/*=============================================================================
 *                              FITTING CODE
 *=============================================================================*/
int find_best_alpha(){
	
	//double alpha_2pcf[num_alpha][nr], trials_2pcf[num_alpha][ntrials][total_nr], alpha_err[num_alpha][nr];
	double **alpha_2pcf, ***trials_2pcf, **alpha_err;
	double low, high, da;
	long i,nend,k,j,ir,jk;

	double chi2;

	double yi,yerr,xi,minerr;
	int nbreak, iii,ind,min_ind,max_ind;

	long thisseed[Nalpha*num_alpha*ntrials];
#ifdef ALLOUT
	char Output2PCF[LINELENGTH];
	char ThisFile[15];
#endif

	ncoeffs = num_alpha-1;
	nbreak =ncoeffs-2;
	// Allocate alphavec, which is our result
	//alphavec = (double *) calloc(Nalpha,sizeof(double));
	//betavec = (double *) calloc(Nalpha,sizeof(double));


	alpha_2pcf = (double **) calloc(num_alpha,sizeof(double *));
	alpha_err = (double **) calloc(num_alpha,sizeof(double *));
	trials_2pcf = (double ***) calloc(num_alpha,sizeof(double **));
	for (i=0;i<num_alpha;i++){
		alpha_2pcf[i] = (double *) calloc(nr,sizeof(double));
		alpha_err[i] = (double *) calloc(nr,sizeof(double));
		trials_2pcf[i] = (double **) calloc(ntrials,sizeof(double*));
		for (j=0;j<ntrials;j++)
			trials_2pcf[i][j] = (double *) calloc(total_nr,sizeof(double));

	}

	// allocate a cubic bspline workspace (k = 4) 
	bw = gsl_bspline_alloc(4, nbreak);
	B = gsl_vector_alloc(ncoeffs);
	alpha_grid = gsl_vector_alloc(num_alpha);
	chi2_alpha = gsl_vector_alloc(num_alpha);
	X = gsl_matrix_alloc(num_alpha,ncoeffs);

	c = gsl_vector_alloc(ncoeffs);
	weights = gsl_vector_alloc(num_alpha);
	cov = gsl_matrix_alloc(ncoeffs, ncoeffs);
	mw = gsl_multifit_linear_alloc(num_alpha, ncoeffs);


	// Loop through each mass bin
	nend = 0;
	iii  = 0;

	seed = time(NULL);
	for (i=0;i<Nalpha;i++){
		for(j=0;j<num_alpha;j++){
			for(k=0;k<ntrials;k++){
				thisseed[iii] = seed + iii;
				iii++;
			}
		}
	}
	fprintf(stderr,"STARTING LOOPS...\n");
	iii = 0;
	for (i=0;i<Nalpha;i++){
		// Get where to place halos to
		nend += ncuts[i];
		
		// Calculate a more optimum alpha_grid for this bin
		if (i < 2)
			low = alpha_ratio_1*best_alpha;
		else{
			low = alpha_ratio*best_alpha;
		}
		high = 1.05*best_alpha;
		da = (high-low)/(num_alpha-1);

		fprintf(stderr,"STARTING THREADS\n");
	#ifndef NO_PAR_FIT
		#pragma omp parallel for  num_threads(nthreads) private(jk,j,k) \
		shared(num_alpha,ntrials,i,alpha_grid,low,da,stderr,Nalpha,alphavec,iii,mcuts,ListOfParticles,\
		NPartPerCell,x,y,z,Lbox,Npart,Nlin,HaloMass,nend,mpart,recalc_frac,betavec,thisseed,trials_2pcf,rho,maxr,\
		total_nr,Nhalos) default(none)
	#endif
		for(jk=0;jk<num_alpha*ntrials;jk++){
  			fprintf(stderr,"Thread %d/%d\n",omp_get_thread_num(),omp_get_num_threads());
			
			float *hx,*hy,*hz,*hR;			
			double *thisalphavec;
			double *ercorr;
			unsigned long long *DD;
			hx = (float *) calloc(Nhalos,sizeof(float));
			hy = (float *) calloc(Nhalos,sizeof(float));
			hz = (float *) calloc(Nhalos,sizeof(float));
			hR = (float *) calloc(Nhalos,sizeof(float));
			thisalphavec = (double *) calloc(Nalpha,sizeof(double));
			DD = (unsigned long long *) calloc(total_nr,sizeof(unsigned long long));
			ercorr = (double *) calloc(total_nr,sizeof(double));

			k=jk%ntrials;
			j=jk/ntrials;

			fprintf(stderr,"MOVED MEMORY\n");
			fprintf(stderr,"GOT k,j: %ld, %ld\n",k,j);
			
			fprintf(stderr,"sizeof M : %f MB\n",(Nlin*Nlin*Nlin*sizeof(double)/1024./1024.));
			//define the alpha grid for this mass bin
			gsl_vector_set(alpha_grid,j,low+j*da);
			

			// create a local alphavec, to which we add the current gridded alpha
			int itemp;
			for (itemp=0;itemp<Nalpha;itemp++)
				thisalphavec[itemp] = alphavec[itemp];


			//fprintf(stderr,"mem copied\n");
			thisalphavec[i] = gsl_vector_get(alpha_grid,j);

			// Place the halos

			#ifdef NO_PAR_FIT
			place_halos(nend,HaloMass, Nlin, Npart, x, y, z, NULL,NULL,NULL,Lbox,
					rho,thisseed[jk+i*num_alpha*ntrials],
					mpart,nthreads,thisalphavec, betavec, mcuts, Nalpha, recalc_frac,hx, hy, hz,
					NULL,NULL,NULL,hR,ListOfParticles,NPartPerCell);
			#else 
			place_halos(nend,HaloMass, Nlin, Npart, x, y, z, NULL,NULL,NULL,Lbox,
					rho,thisseed[jk+i*num_alpha*ntrials],
					mpart,1,thisalphavec, betavec, mcuts, Nalpha, recalc_frac,hx, hy, hz,
					NULL,NULL,NULL,hR,ListOfParticles,NPartPerCell);

			#endif			
			fprintf(stderr,"correlating...\n");
			//Get the 2PCF
			correlate(nend, Lbox,hx,hy,hz,trials_2pcf[j][k], ercorr, DD,total_nr,maxr,1);
			fprintf(stderr,"...correlated\n");
			
			free(hx);
			free(hy);
			free(hz);
			free(hR);
			free(thisalphavec);
			free(ercorr);
			free(DD);

		}

		for (jk=0;jk<num_alpha*ntrials;jk++){
			k=jk%ntrials;
			j=jk/ntrials;
			fprintf(stderr,"RAWCORR %ld, %ld: %e\n",j,k,trials_2pcf[j][k][0]);
		}

	//	#pragma omp parallel for private(ir,j,chi2,k) shared(num_alpha,stderr,i,nr,alpha_2pcf) default(none)
		fprintf(stderr,"GOT CORRELATIONS , GETTING CHI2...\n");
		for(j=0;j<num_alpha;j++){
			//Get mean and stdev of trials_2pcf
			chi2 = 0.0;
			for (ir=0;ir<nr;ir++){
				alpha_2pcf[j][ir] = 0.0;
				for(k=0;k<ntrials;k++){
					alpha_2pcf[j][ir] += trials_2pcf[j][k][ir+total_nr-nr-2];
				}
				alpha_2pcf[j][ir] = alpha_2pcf[j][ir]/ntrials; 
				alpha_err[j][ir] = 0.0;
				for(k=0;k<ntrials;k++){
					alpha_err[j][ir] += pow((trials_2pcf[j][k][ir+total_nr-nr-2]-alpha_2pcf[j][ir]),2);
				}
				alpha_err[j][ir] = pow((alpha_err[j][ir]/(ntrials-1)),0.5);

				// Now get chi^2 values
				#ifdef REL_CHI2
				chi2 += pow(((alpha_2pcf[j][ir]-nbody_2pcf[i][ir+total_nr-nr-2])/nbody_2pcf[i][ir+total_nr-nr-2]),2);
				#else
				chi2 += pow(((alpha_2pcf[j][ir]-nbody_2pcf[i][ir+total_nr-nr-2])/alpha_err[j][ir]),2);
				#endif

				fprintf(stderr,"%ld, %ld, %e, %e, %e, %e\n",j,ir,alpha_2pcf[j][ir],nbody_2pcf[i][ir+total_nr-nr-2],alpha_err[j][ir],chi2);
			}
			gsl_vector_set(chi2_alpha,j,chi2/nr);
			gsl_vector_set(weights,j,nr/chi2);
		}
	//	#endif //NO_PARALLEL_FIT
//*/

#ifdef ALLOUT
		//OUTPUT SOME THINGS FOR CHECKING
		sprintf(ThisFile,"/raw.2pcf.%ld",i);
		strcpy(Output2PCF,OutputDir);
		strcat(Output2PCF,ThisFile);
		FILE* output_2pcf;
		output_2pcf = fopen(Output2PCF,"w");

		//header
		fprintf(output_2pcf,"# r, ");
		for (k=0;k<num_alpha;k++){
			fprintf(output_2pcf,"%e\t",gsl_vector_get(alpha_grid,k));
		}
		fprintf(output_2pcf,"\n");

		//table
		for (ir=0;ir<nr;ir++){
			fprintf(output_2pcf,"%e\t",(ir+total_nr-nr+0.5)*maxr/total_nr);
			for(k=0;k<num_alpha-1;k++){
				fprintf(output_2pcf,"%e\t",alpha_2pcf[k][ir]);
			}
			fprintf(output_2pcf,"%e\n",alpha_2pcf[num_alpha-1][ir]);
		}
		fclose(output_2pcf);

		sprintf(ThisFile,"/raw.err.%ld",i);
		strcpy(Output2PCF,OutputDir);
		strcat(Output2PCF,ThisFile);
		FILE* output_err;
		output_err = fopen(Output2PCF,"w");

		//header
		fprintf(output_err,"# r, ");
		for (k=0;k<num_alpha;k++){
			fprintf(output_err,"%e\t",gsl_vector_get(alpha_grid,k));
		}
		fprintf(output_err,"\n");

		//table
		for (ir=0;ir<nr;ir++){
			fprintf(output_err,"%e\t",(ir+total_nr-nr+0.5)*maxr/total_nr);
			for(k=0;k<num_alpha-1;k++){
				fprintf(output_err,"%e\t",alpha_err[k][ir]);
			}
			fprintf(output_err,"%e\n",alpha_err[num_alpha-1][ir]);
		}
		fclose(output_err);

		sprintf(ThisFile,"/chi2.%ld",i);
		strcpy(Output2PCF,OutputDir);
		strcat(Output2PCF,ThisFile);
		FILE* chifile;
		chifile = fopen(Output2PCF,"w");
		fprintf(chifile,"# alpha, chi2, weight\n");
		for (k=0;k<num_alpha;k++){
			fprintf(chifile,"%e\t%e\t%e\n",gsl_vector_get(alpha_grid,k),gsl_vector_get(chi2_alpha,k),
					gsl_vector_get(weights,k));
		}
		fclose(chifile);

#endif


		// Check if final value or initial value is the smallest
		minerr = gsl_vector_get(chi2_alpha,0);
		ind = 0;
		for(k=1;k<num_alpha;k++){
			if(minerr>gsl_vector_get(chi2_alpha,k)){
				minerr = gsl_vector_get(chi2_alpha,k);
				ind = k;
			}
		}
		if(ind==0){
			fprintf(stderr,"ERROR: alpha_grid doesn't extend low enough, set alpha_ratio lower");
		}
		if(ind==num_alpha-1){
			fprintf(stderr,"ERROR: alpha_grid doesn't extend high enough, set best_alpha higher");
		}
		if (ind>=2){
			min_ind = ind-2;
		}else{
			min_ind = 0;
		}
		if (ind<=num_alpha-3){
			max_ind = ind+2;
		}else{
			max_ind = num_alpha-1;
		}

		/* use uniform breakpoints on interval */
		gsl_bspline_knots_uniform(gsl_vector_get(alpha_grid,0), gsl_vector_get(alpha_grid,num_alpha-1), bw);

		/* construct the fit matrix X */
		for (k = 0; k < num_alpha; ++k){
			double xi = gsl_vector_get(alpha_grid, k);

			/* compute B_j(xi) for all j */
			gsl_bspline_eval(xi, B, bw);

			/* fill in row i of X */
			for (j = 0; j < ncoeffs; ++j){
				double Bj = gsl_vector_get(B, j);
				gsl_matrix_set(X, k, j, Bj);
		    }
		}

		fprintf(stderr,"Got to the fit part\n");
		/* do the fit */
		gsl_multifit_wlinear(X, weights, chi2_alpha, c, cov, &chisq, mw);

		// PRINT OUT EVERYTHING WE HAVE SO FAR
		fprintf(stderr,"alpha\tchi2_alpha\t\n");
		for (k=0;k<num_alpha;k++){
			fprintf(stderr,"%e\t%e\t\n",gsl_vector_get(alpha_grid,k),
					gsl_vector_get(chi2_alpha,k));
		}

#ifdef VERB
		dof = num_alpha - ncoeffs;
		tss = gsl_stats_wtss(weights->data, 1, chi2_alpha->data, 1, chi2_alpha->size);
		Rsq = 1.0 - chisq / tss;

		fprintf(stderr, "chisq/dof = %e, Rsq = %f\n",chisq / dof, Rsq);
#endif

		for (xi=gsl_vector_get(alpha_grid,0);xi<gsl_vector_get(alpha_grid,num_alpha-1);xi+=0.01){
			gsl_bspline_eval(xi, B, bw);
			gsl_multifit_linear_est(B, c, cov, &yi, &yerr);
			fprintf(stderr,"%e\t%e\t%e\n",xi,yi,gsl_vector_get(B,0));
		}
		//DO THE MINIMIZATION
		alphavec[i] = minimize(gsl_vector_get(alpha_grid,min_ind), gsl_vector_get(alpha_grid,max_ind),
									   gsl_vector_get(alpha_grid,ind));
		best_alpha = alphavec[i];

		fprintf(stderr,"\n Best alpha: %f\n\n",best_alpha);
	}

	return 0;

}
Ejemplo n.º 14
0
Archivo: test.c Proyecto: lemahdi/mglib
int
main(int argc, char **argv)
{
  size_t order, breakpoints, i;

  gsl_ieee_env_setup();

  argc = 0;                     /* prevent warnings about unused parameters */
  argv = 0;

  for (order = 1; order < 10; order++)
    {
      for (breakpoints = 2; breakpoints < 100; breakpoints++)
        {
          double a = -1.23 * order, b = 45.6 * order;
          gsl_bspline_workspace *bw = gsl_bspline_alloc(order, breakpoints);
          gsl_bspline_deriv_workspace *dbw = gsl_bspline_deriv_alloc(order);
          gsl_bspline_knots_uniform(a, b, bw);
          test_bspline(bw, dbw);
          gsl_bspline_deriv_free(dbw);
          gsl_bspline_free(bw);
        }
    }


  for (order = 1; order < 10; order++)
    {
      for (breakpoints = 2; breakpoints < 100; breakpoints++)
        {
          double a = -1.23 * order, b = 45.6 * order;
          gsl_bspline_workspace *bw = gsl_bspline_alloc(order, breakpoints);
          gsl_bspline_deriv_workspace *dbw = gsl_bspline_deriv_alloc(order);
          gsl_vector *k = gsl_vector_alloc(breakpoints);
          for (i = 0; i < breakpoints; i++)
            {
              double f, x;
              f = sqrt(i / (breakpoints - 1.0));
              x = (1 - f) * a + f * b;
              gsl_vector_set(k, i, x);
            };
          gsl_bspline_knots(k, bw);
          test_bspline(bw, dbw);
          gsl_vector_free(k);
          gsl_bspline_deriv_free(dbw);
          gsl_bspline_free(bw);
        }
    }

  /* Spot check known 0th, 1st, 2nd derivative
     evaluations for a particular k = 2 case.  */
  {
    size_t i, j; /* looping */

    const double xloc[4]     =  { 0.0,  1.0,  6.0,  7.0};
    const double deriv[4][3] =
    {
      { -1.0/2.0,  1.0/2.0, 0.0     },
      { -1.0/2.0,  1.0/2.0, 0.0     },
      {      0.0, -1.0/5.0, 1.0/5.0 },
      {      0.0, -1.0/5.0, 1.0/5.0 }
    };

    gsl_bspline_workspace *bw = gsl_bspline_alloc(2, 3);
    gsl_bspline_deriv_workspace *dbw = gsl_bspline_deriv_alloc(2);
    gsl_matrix *dB = gsl_matrix_alloc(gsl_bspline_ncoeffs(bw),
                                      gsl_bspline_order(bw) + 1);

    gsl_vector *breakpts = gsl_vector_alloc(3);
    gsl_vector_set(breakpts, 0, 0.0);
    gsl_vector_set(breakpts, 1, 2.0);
    gsl_vector_set(breakpts, 2, 7.0);
    gsl_bspline_knots(breakpts, bw);


    for (i = 0; i < 4; ++i)  /* at each location */
      {
        /* Initialize dB with poison to ensure we overwrite it */
        gsl_matrix_set_all(dB, GSL_NAN);

        gsl_bspline_deriv_eval(xloc[i], gsl_bspline_order(bw), dB, bw, dbw);
        for (j = 0; j < gsl_bspline_ncoeffs(bw) ; ++j)
          {
            /* check basis function 1st deriv */
            gsl_test_abs(gsl_matrix_get(dB, j, 1), deriv[i][j], GSL_DBL_EPSILON,
                         "b-spline k=%d basis #%d derivative %d at x = %f",
                         gsl_bspline_order(bw), j, 1, xloc[i]);
          }
        for (j = 0; j < gsl_bspline_ncoeffs(bw); ++j)
          {
            /* check k order basis function has k-th deriv equal to 0 */
            gsl_test_abs(gsl_matrix_get(dB, j, gsl_bspline_order(bw)), 0.0,
                         GSL_DBL_EPSILON,
                         "b-spline k=%d basis #%d derivative %d at x = %f",
                         gsl_bspline_order(bw), j, gsl_bspline_order(bw),
                         xloc[i]);
          }
      }

    gsl_matrix_free(dB);
    gsl_bspline_deriv_free(dbw);
    gsl_bspline_free(bw);
    gsl_vector_free(breakpts);
  }

  /* Spot check known 0th, 1st, 2nd derivative
     evaluations for a particular k = 3 case.  */
  {
    size_t i, j; /* looping */
    const double xloc[5]     =  { 0.0, 5.0, 9.0, 12.0, 15.0 };
    const double eval[5][6]  =
    {
      { 4./25.,  69./100.,   3./ 20. ,  0.    , 0.   , 0.    },
      { 0.     ,  4./21. , 143./210. ,  9./70., 0.   , 0.    },
      { 0.     ,  0.     ,   3./ 10. ,  7./10., 0.   , 0.    },
      { 0.     ,  0.     ,   0.      ,  3./4. , 1./4., 0.    },
      { 0.     ,  0.     ,   0.      ,  1./3. , 5./9., 1./9. }
    };
    const double deriv[5][6] =
    {
      { -4./25.,  3./50.,   1./ 10.,  0.    , 0.    , 0.      },
      {  0.    , -2./21.,   1./105.,  3./35., 0.    , 0.      },
      {  0.    ,  0.    ,  -1./5.  ,  1./ 5., 0.    , 0.      },
      {  0.    ,  0.    ,   0.     , -1./ 6., 1./6. , 0.      },
      {  0.    ,  0.    ,   0.     , -1./ 9., 1./27., 2./27. }
    };
    const double deriv2[5][6] =
    {
      { 2./25., -17./150.,   1.0/30.0 ,  0.0     ,  0.     , 0.     },
      { 0.    ,   1./ 42., -11.0/210.0,  1.0/35.0,  0.     , 0.     },
      { 0.    ,   0.     ,   1.0/15.0 ,-11.0/90.0,  1./18. , 0.     },
      { 0.    ,   0.     ,   0.0      ,  1.0/54.0, -7./162., 2./81. },
      { 0.    ,   0.     ,   0.0      ,  1.0/54.0, -7./162., 2./81. }
    };

    gsl_bspline_workspace *bw = gsl_bspline_alloc(3, 5);
    gsl_bspline_deriv_workspace *dbw = gsl_bspline_deriv_alloc(3);

    gsl_matrix *dB = gsl_matrix_alloc(gsl_bspline_ncoeffs(bw),
                                      gsl_bspline_order(bw) + 1);

    gsl_vector *breakpts = gsl_vector_alloc(5);
    gsl_vector_set(breakpts, 0, -3.0);
    gsl_vector_set(breakpts, 1,  2.0);
    gsl_vector_set(breakpts, 2,  9.0);
    gsl_vector_set(breakpts, 3, 12.0);
    gsl_vector_set(breakpts, 4, 21.0);
    gsl_bspline_knots(breakpts, bw);

    for (i = 0; i < 5; ++i)  /* at each location */
      {
        /* Initialize dB with poison to ensure we overwrite it */
        gsl_matrix_set_all(dB, GSL_NAN);
        gsl_bspline_deriv_eval(xloc[i], gsl_bspline_order(bw), dB, bw, dbw);

        /* check basis function evaluation */
        for (j = 0; j < gsl_bspline_ncoeffs(bw); ++j)
          {
            gsl_test_abs(gsl_matrix_get(dB, j, 0), eval[i][j], GSL_DBL_EPSILON,
                         "b-spline k=%d basis #%d derivative %d at x = %f",
                         gsl_bspline_order(bw), j, 0, xloc[i]);
          }
        /* check 1st derivative evaluation */
        for (j = 0; j < gsl_bspline_ncoeffs(bw); ++j)
          {
            gsl_test_abs(gsl_matrix_get(dB, j, 1), deriv[i][j], GSL_DBL_EPSILON,
                         "b-spline k=%d basis #%d derivative %d at x = %f",
                         gsl_bspline_order(bw), j, 1, xloc[i]);
          }
        /* check 2nd derivative evaluation */
        for (j = 0; j < gsl_bspline_ncoeffs(bw); ++j)
          {
            gsl_test_abs(gsl_matrix_get(dB, j, 2), deriv2[i][j], GSL_DBL_EPSILON,
                         "b-spline k=%d basis #%d derivative %d at x = %f",
                         gsl_bspline_order(bw), j, 2, xloc[i]);
          }
      }

    gsl_matrix_free(dB);
    gsl_bspline_deriv_free(dbw);
    gsl_bspline_free(bw);
    gsl_vector_free(breakpts);
  }

  /* Check Greville abscissae functionality on a non-uniform k=1 */
  {
    size_t i; /* looping */

    /* Test parameters */
    const size_t k = 1;
    const double bpoint_data[]    = { 0.0, 0.2, 0.5, 0.75, 1.0 };
    const size_t nbreak           = sizeof(bpoint_data)/sizeof(bpoint_data[0]);

    /* Expected results */
    const double abscissae_data[] = { 0.1, 0.35, 0.625, 0.875 };
    const size_t nabscissae       = sizeof(abscissae_data)/sizeof(abscissae_data[0]);

    gsl_vector_const_view bpoints = gsl_vector_const_view_array(bpoint_data, nbreak);
    gsl_bspline_workspace *w = gsl_bspline_alloc(k, nbreak);
    gsl_bspline_knots((const gsl_vector *) &bpoints, w);

    gsl_test_int(nabscissae, gsl_bspline_ncoeffs(w),
        "b-spline k=%d number of abscissae", k);
    for (i = 0; i < nabscissae; ++i)
      {
        gsl_test_abs(gsl_bspline_greville_abscissa(i, w), abscissae_data[i], 2*k*GSL_DBL_EPSILON,
            "b-spline k=%d Greville abscissa #%d at x = %f", k, i, abscissae_data[i]);
      }

    gsl_bspline_free(w);
  }

  /* Check Greville abscissae functionality on a non-uniform k=2 */
  {
    size_t i; /* looping */

    /* Test parameters */
    const size_t k = 2;
    const double bpoint_data[]    = { 0.0, 0.2, 0.5, 0.75, 1.0 };
    const size_t nbreak           = sizeof(bpoint_data)/sizeof(bpoint_data[0]);

    /* Expected results */
    const double abscissae_data[] = { 0.0, 0.2, 0.5, 0.75, 1.0 };
    const size_t nabscissae       = sizeof(abscissae_data)/sizeof(abscissae_data[0]);

    gsl_vector_const_view bpoints = gsl_vector_const_view_array(bpoint_data, nbreak);
    gsl_bspline_workspace *w = gsl_bspline_alloc(k, nbreak);
    gsl_bspline_knots((const gsl_vector *) &bpoints, w);

    gsl_test_int(nabscissae, gsl_bspline_ncoeffs(w),
        "b-spline k=%d number of abscissae", k);
    for (i = 0; i < nabscissae; ++i)
      {
        gsl_test_abs(gsl_bspline_greville_abscissa(i, w), abscissae_data[i], 2*k*GSL_DBL_EPSILON,
            "b-spline k=%d Greville abscissa #%d at x = %f", k, i, abscissae_data[i]);
      }

    gsl_bspline_free(w);
  }

  /* Check Greville abscissae functionality on non-uniform k=3 */
  {
    size_t i; /* looping */

    /* Test parameters */
    const size_t k = 3;
    const double bpoint_data[]    = { 0.0, 0.2, 0.5, 0.75, 1.0 };
    const size_t nbreak           = sizeof(bpoint_data)/sizeof(bpoint_data[0]);

    /* Expected results */
    const double abscissae_data[] = {      0.0, 1.0/10.0, 7.0/20.0,
                                      5.0/ 8.0, 7.0/ 8.0,      1.0 };
    const size_t nabscissae       = sizeof(abscissae_data)/sizeof(abscissae_data[0]);

    gsl_vector_const_view bpoints = gsl_vector_const_view_array(bpoint_data, nbreak);
    gsl_bspline_workspace *w = gsl_bspline_alloc(k, nbreak);
    gsl_bspline_knots((const gsl_vector *) &bpoints, w);

    gsl_test_int(nabscissae, gsl_bspline_ncoeffs(w),
        "b-spline k=%d number of abscissae", k);
    for (i = 0; i < nabscissae; ++i)
      {
        gsl_test_abs(gsl_bspline_greville_abscissa(i, w), abscissae_data[i], 2*k*GSL_DBL_EPSILON,
            "b-spline k=%d Greville abscissa #%d at x = %f", k, i, abscissae_data[i]);
      }

    gsl_bspline_free(w);
  }

  /* Check Greville abscissae functionality on non-uniform k=4 */
  {
    size_t i; /* looping */

    /* Test parameters */
    const size_t k = 4;
    const double bpoint_data[]    = { 0.0, 0.2, 0.5, 0.75, 1.0 };
    const size_t nbreak           = sizeof(bpoint_data)/sizeof(bpoint_data[0]);

    /* Expected results */
    const double abscissae_data[] = { 0.0,  1.0/15.0,  7.0/30.0,  29.0/60.0,
                                            3.0/ 4.0, 11.0/12.0,        1.0 };
    const size_t nabscissae       = sizeof(abscissae_data)/sizeof(abscissae_data[0]);

    gsl_vector_const_view bpoints = gsl_vector_const_view_array(bpoint_data, nbreak);
    gsl_bspline_workspace *w = gsl_bspline_alloc(k, nbreak);
    gsl_bspline_knots((const gsl_vector *) &bpoints, w);

    gsl_test_int(nabscissae, gsl_bspline_ncoeffs(w),
        "b-spline k=%d number of abscissae", k);
    for (i = 0; i < nabscissae; ++i)
      {
        gsl_test_abs(gsl_bspline_greville_abscissa(i, w), abscissae_data[i], 2*k*GSL_DBL_EPSILON,
            "b-spline k=%d Greville abscissa #%d at x = %f", k, i, abscissae_data[i]);
      }

    gsl_bspline_free(w);
  }

  exit(gsl_test_summary());
}