Changeset 1724


Ignore:
Timestamp:
Jan 15, 2009, 3:32:36 PM (12 years ago)
Author:
Jari Häkkinen
Message:

Fixes #466. GSL_error is now thrown on GSL errors. Last thing in this ticket.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/interpolation_test.cc

    r1666 r1724  
    22
    33/*
    4   Copyright (C) 2008 Jari Häkkinen
     4  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
     5  Copyright (C) 2009 Jari Häkkinen
    56
    67  This file is part of the yat library, http://dev.thep.lu.se/yat
     
    2829#include "yat/regression/LinearInterpolation.h"
    2930#include "yat/regression/PolynomialInterpolation.h"
     31#include "yat/utility/Exception.h"
    3032#include "yat/utility/Vector.h"
    3133#include "yat/utility/VectorConstView.h"
     
    4143
    4244  test::Suite suite(argc, argv);
     45  suite.err() << "testing interpolations ..." << std::endl;
     46
    4347  test_delete(suite);
    4448
     
    5761  */
    5862
     63  suite.err() << "testing cspline" << std::endl;
    5964  utility::Vector x(6);
    6065  utility::Vector y(6);
     
    8590      suite.add(false);
    8691    }
    87     // Check for errors
    88     if (cspline.error_status()) {
    89       suite.err() << "cspline test failed with GSL_EDOM error" << std::endl;
    90       suite.add(false);
    91     }
    9292  }
    9393
     94  suite.err() << "testing exceptions" << std::endl;
    9495  // An error should be generated, if not test fails
    95   cspline.evaluate(w(0)-5);
    96   if (!cspline.error_status()) {
    97     suite.err() << "cspline test failed no GSL_EDOM error occured" << std::endl;
    98     suite.add(false);
     96  try {
     97    cspline.evaluate(w(0)-5); // expect GSL_error to be thrown
     98    suite.add(false); // this line is only executed if no error occured above
     99  }
     100  catch (utility::GSL_error) {
     101    // just catch the error, all is fine since the error is expected.
    99102  }
    100103
    101104  // lazy testing, at least try to create the objects
     105  suite.err() << "testing construction of\n"
     106              << "\tAkima, AkimaPeriodic, CSplinePeriodic, Linear, Polynomial"
     107              << std::endl;
    102108  AkimaInterpolation(x,y);
    103109  AkimaPeriodicInterpolation(x,y);
     
    111117void test_delete(test::Suite& suite)
    112118{
     119  suite.err() << "testing destruction" << std::endl;
    113120  utility::Vector x(10);
    114121  for (size_t i=0; i<x.size(); ++i)
  • trunk/yat/regression/GSLInterpolation.cc

    r1655 r1724  
    22
    33/*
    4   Copyright (C) 2008 Jari Häkkinen
     4  Copyright (C) 2008, 2009 Jari Häkkinen
    55
    66  This file is part of the yat library, http://dev.thep.lu.se/yat
     
    2222#include "GSLInterpolation.h"
    2323
     24#include "yat/utility/Exception.h"
    2425#include "yat/utility/VectorBase.h"
    2526
    2627#include <cassert>
     28#include <sstream>
    2729
    2830namespace theplu {
     
    3436                                     const utility::VectorBase& x,
    3537                                     const utility::VectorBase& y)
    36     : evaluation_(0.0), gsl_status_(0)
     38    : evaluation_(0.0)
    3739  {
    3840    assert(x.size()==y.size());
     
    4547    }
    4648    interpolator_ = gsl_interp_alloc(interpolator_type, size);
    47     gsl_status_=gsl_interp_init(interpolator_, x_, y_, size);
     49    if (int status=gsl_interp_init(interpolator_, x_, y_, size)) {
     50      std::stringstream ss("GSLInterpolation::GSLInterpolation ");
     51      ss << " GSL error code " << status;
     52      // clean up
     53      gsl_interp_free(interpolator_);
     54      throw utility::GSL_error(ss.str());
     55    }
    4856    accelerator_ = gsl_interp_accel_alloc();
    4957  }
     
    5260  GSLInterpolation::~GSLInterpolation(void)
    5361  {
     62    gsl_interp_accel_free(accelerator_);
    5463    gsl_interp_free(interpolator_);
    55     gsl_interp_accel_free(accelerator_);
    5664    delete x_;
    5765    delete y_;
     
    5967
    6068
    61   int GSLInterpolation::error_status(void) const
    62   {
    63     return gsl_status_;
    64   }
    65 
    66 
    6769  double GSLInterpolation::evaluate(double x)
    6870  {
    69     gsl_status_=gsl_interp_eval_e(interpolator_, x_, y_, x, accelerator_,
    70                                   &evaluation_);
     71    if (int status=gsl_interp_eval_e(interpolator_, x_, y_, x, accelerator_,
     72                                     &evaluation_)) {
     73      std::stringstream ss("GSLInterpolation::evaluate ");
     74      ss << " GSL error code " << status;
     75      throw utility::GSL_error(ss.str());
     76    }
    7177    return evaluation_;
    7278  }
     
    7581  double GSLInterpolation::evaluate_derivative(double x)
    7682  {
    77     gsl_status_=gsl_interp_eval_deriv_e(interpolator_, x_, y_, x, accelerator_,
    78                                         &evaluation_);
     83    if (int status=gsl_interp_eval_deriv_e(interpolator_, x_, y_, x,
     84                                           accelerator_, &evaluation_)) {
     85      std::stringstream ss("GSLInterpolation::evaluate_derivative ");
     86      ss << " GSL error code " << status;
     87      throw utility::GSL_error(ss.str());
     88    }
    7989    return evaluation_;
    8090  }
     
    8393  double GSLInterpolation::evaluate_derivative2(double x)
    8494  {
    85     gsl_status_=gsl_interp_eval_deriv2_e(interpolator_, x_, y_, x, accelerator_,
    86                                          &evaluation_);
     95    if (int status=gsl_interp_eval_deriv2_e(interpolator_, x_, y_, x,
     96                                            accelerator_, &evaluation_)) {
     97      std::stringstream ss("GSLInterpolation::evaluate_derivative2 ");
     98      ss << " GSL error code " << status;
     99      throw utility::GSL_error(ss.str());
     100    }
    87101    return evaluation_;
    88102  }
     
    91105  double GSLInterpolation::evaluate_integral(double a, double b)
    92106  {
    93     gsl_status_=gsl_interp_eval_integ_e(interpolator_, x_, y_, a, b,
    94                                         accelerator_, &evaluation_);
     107    if (int status=gsl_interp_eval_integ_e(interpolator_, x_, y_, a, b,
     108                                           accelerator_, &evaluation_)) {
     109      std::stringstream ss("GSLInterpolation::evaluate_integral ");
     110      ss << " GSL error code " << status;
     111      throw utility::GSL_error(ss.str());
     112    }
    95113    return evaluation_;
    96114  }
  • trunk/yat/regression/GSLInterpolation.h

    r1722 r1724  
    5252     determined by the type of interpolation used.
    5353
    54      Some underlying GSL functions return GSL error codes. The error
    55      code is stored internally and should be checked for by the caller
    56      using GSLInterpolation::error_status(void). Refer to the
    57      gsl_errno.h for the error code listing.
     54     When underlying GSL functions return GSL error an GSL_error is
     55     thrown. Refer to the gsl_errno.h for the error code listing.
    5856
    5957     \since New in yat 0.5
     
    7371       not defined.
    7472
    75        Some underlying GSL functions return GSL error codes. The error
    76        code is stored internally and should be checked for by the
    77        caller. Refer to the gsl_errno.h for the error code listing.
    78 
    79        \see error_status(void)
     73       \throw GSL_error if some underlying GSL functions return GSL
     74       error. Refer to gsl_errno.h for the error code listing.
    8075    */
    8176    GSLInterpolation(const gsl_interp_type*, const utility::VectorBase& x,
     
    8883
    8984    /**
    90        \brief Check the error status
    91 
    92        Some underlying GSL functions return GSL error codes. The error
    93        code is stored internally and should be checked for by the
    94        caller. Refer to the gsl_errno.h for the error code listing.
    95 
    96        \return The current error status. A zero return values
    97        indicates no errors where encountered.
    98     */
    99     int error_status(void) const;
    100 
    101     /**
    10285       \brief Calculate the interpolated value for \a x.
    103 
    104        GSL error status is set if evaluation is requested outside the
    105        range defined by the interpolation algorithm. Use function
    106        error_status to check for errors.
    10786
    10887       \return The interpolated value of \f$ y \f$ for a given point
    10988       \a x.
    11089
    111        \see error_status(void)
     90       \throw GSL_error if evaluation is requested outside the range
     91       defined by the interpolation algorithm.
    11292    */
    11393    double evaluate(double x);
     
    11797       \a x.
    11898
    119        GSL error status is set if evaluation is requested outside the
    120        range defined by the interpolation algorithm. Use function
    121        error_status to check for errors.
    122 
    12399       \return The derivative.
    124100
    125        \see error_status(void)
     101       \throw GSL_error if evaluation is requested outside the range
     102       defined by the interpolation algorithm.
    126103    */
    127104    double evaluate_derivative(double x);
     
    131108       function at \a x.
    132109
    133        GSL error status is set if evaluation is requested outside the
    134        range defined by the interpolation algorithm. Use function
    135        error_status to check for errors.
    136 
    137110       \return The 2nd derivative.
    138111
    139        \see error_status(void)
     112       \throw GSL_error if evaluation is requested outside the range
     113       defined by the interpolation algorithm.
    140114    */
    141115    double evaluate_derivative2(double x);
     
    145119       function over the range \f$ [a,b] \f$.
    146120
    147        GSL error status is set if evaluation is requested outside the
    148        range defined by the interpolation algorithm. Use function
    149        error_status to check for errors.
    150 
    151121       \return The integral.
    152122
    153        \see error_status(void)
     123       \throw GSL_error if evaluation is requested outside the range
     124       defined by the interpolation algorithm.
    154125    */
    155126    double evaluate_integral(double a, double b);
     
    185156    double evaluation_;
    186157    gsl_interp* interpolator_;
    187     int gsl_status_;
    188158    double* x_;
    189159    double* y_;
Note: See TracChangeset for help on using the changeset viewer.