Added support for log-log interpolation

This commit is contained in:
Guilhem Lavaux 2009-02-24 11:46:18 -06:00
parent 298acdfd11
commit 7b67b408ec
2 changed files with 24 additions and 9 deletions

View File

@ -1,3 +1,4 @@
#include <cmath>
#include <algorithm>
#include "interpolate.hpp"
#include <vector>
@ -8,12 +9,15 @@
using namespace std;
using namespace CosmoTool;
Interpolate::Interpolate(double *xs, double *values, uint32_t N, bool autofree)
Interpolate::Interpolate(double *xs, double *values, uint32_t N, bool autofree,
bool logx, bool logy)
{
spline = gsl_spline_alloc (gsl_interp_linear, N);
gsl_spline_init (spline, xs, values, N);
accel_interp = gsl_interp_accel_alloc();
this->logx = logx;
this->logy = logy;
this->autoFree = autofree;
}
@ -29,10 +33,19 @@ double Interpolate::compute(double x)
throw (InvalidRangeException)
{
double y;
if (logx)
x = log(x);
int err = gsl_spline_eval_e(spline, x, accel_interp, &y);
if (err)
throw InvalidRangeException("Interpolate argument outside range");
if (logy)
return exp(y);
else
return y;
}
uint32_t Interpolate::getNumPoints() const
@ -66,7 +79,7 @@ const Interpolate& Interpolate::operator=(const Interpolate& a)
double Interpolate::getMaxX() const
{
return spline->x[spline->size-1];
return exp(spline->x[spline->size-1]);
}
typedef struct {
@ -136,7 +149,8 @@ Interpolate CosmoTool::buildInterpolateFromFile(const char *fname)
return inter;
}
Interpolate CosmoTool::buildInterpolateFromColumns(const char *fname, uint32_t col1, uint32_t col2)
Interpolate CosmoTool::buildInterpolateFromColumns(const char *fname, uint32_t col1, uint32_t col2, bool logx,
bool logy)
throw (NoSuchFileException,InvalidRangeException)
{
vector<MyPair> allData;
@ -198,15 +212,14 @@ Interpolate CosmoTool::buildInterpolateFromColumns(const char *fname, uint32_t c
for (uint32_t i = 0; i < allData.size(); i++)
{
x[i] = allData[i].a;
y[i] = allData[i].b;
x[i] = logx ? log(allData[i].a) : allData[i].a;
y[i] = logy ? log(allData[i].b) : allData[i].b;
}
Interpolate inter = Interpolate(x, y, allData.size(), true);
Interpolate inter = Interpolate(x, y, allData.size(), true, logx, logy);
delete[] x;
delete[] y;
return inter;
}

View File

@ -15,7 +15,8 @@ namespace CosmoTool
{
public:
Interpolate() : spline(0), accel_interp(0) { }
Interpolate(double *xs, double *values, uint32_t N, bool autofree = false);
Interpolate(double *xs, double *values, uint32_t N, bool autofree = false,
bool logx = false, bool logy = false);
~Interpolate();
double compute(double x)
@ -29,13 +30,14 @@ namespace CosmoTool
gsl_interp_accel *accel_interp;
gsl_spline *spline;
bool autoFree;
bool logx, logy;
};
typedef std::vector< std::pair<double,double> > InterpolatePairs;
Interpolate buildInterpolateFromFile(const char *fname)
throw (NoSuchFileException);
Interpolate buildInterpolateFromColumns(const char *fname, uint32_t col1, uint32_t col2)
Interpolate buildInterpolateFromColumns(const char *fname, uint32_t col1, uint32_t col2, bool logx = false, bool logy = false)
throw (NoSuchFileException,InvalidRangeException);
Interpolate buildFromVector(const InterpolatePairs& v);