Changeset 1724
 Timestamp:
 Jan 15, 2009, 3:32:36 PM (12 years ago)
 Location:
 trunk
 Files:

 3 edited
Legend:
 Unmodified
 Added
 Removed

trunk/test/interpolation_test.cc
r1666 r1724 2 2 3 3 /* 4 Copyright (C) 2008 Jari Häkkinen 4 Copyright (C) 2008 Jari Häkkinen, Peter Johansson 5 Copyright (C) 2009 Jari Häkkinen 5 6 6 7 This file is part of the yat library, http://dev.thep.lu.se/yat … … 28 29 #include "yat/regression/LinearInterpolation.h" 29 30 #include "yat/regression/PolynomialInterpolation.h" 31 #include "yat/utility/Exception.h" 30 32 #include "yat/utility/Vector.h" 31 33 #include "yat/utility/VectorConstView.h" … … 41 43 42 44 test::Suite suite(argc, argv); 45 suite.err() << "testing interpolations ..." << std::endl; 46 43 47 test_delete(suite); 44 48 … … 57 61 */ 58 62 63 suite.err() << "testing cspline" << std::endl; 59 64 utility::Vector x(6); 60 65 utility::Vector y(6); … … 85 90 suite.add(false); 86 91 } 87 // Check for errors88 if (cspline.error_status()) {89 suite.err() << "cspline test failed with GSL_EDOM error" << std::endl;90 suite.add(false);91 }92 92 } 93 93 94 suite.err() << "testing exceptions" << std::endl; 94 95 // 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. 99 102 } 100 103 101 104 // 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; 102 108 AkimaInterpolation(x,y); 103 109 AkimaPeriodicInterpolation(x,y); … … 111 117 void test_delete(test::Suite& suite) 112 118 { 119 suite.err() << "testing destruction" << std::endl; 113 120 utility::Vector x(10); 114 121 for (size_t i=0; i<x.size(); ++i) 
trunk/yat/regression/GSLInterpolation.cc
r1655 r1724 2 2 3 3 /* 4 Copyright (C) 2008 Jari Häkkinen4 Copyright (C) 2008, 2009 Jari Häkkinen 5 5 6 6 This file is part of the yat library, http://dev.thep.lu.se/yat … … 22 22 #include "GSLInterpolation.h" 23 23 24 #include "yat/utility/Exception.h" 24 25 #include "yat/utility/VectorBase.h" 25 26 26 27 #include <cassert> 28 #include <sstream> 27 29 28 30 namespace theplu { … … 34 36 const utility::VectorBase& x, 35 37 const utility::VectorBase& y) 36 : evaluation_(0.0) , gsl_status_(0)38 : evaluation_(0.0) 37 39 { 38 40 assert(x.size()==y.size()); … … 45 47 } 46 48 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 } 48 56 accelerator_ = gsl_interp_accel_alloc(); 49 57 } … … 52 60 GSLInterpolation::~GSLInterpolation(void) 53 61 { 62 gsl_interp_accel_free(accelerator_); 54 63 gsl_interp_free(interpolator_); 55 gsl_interp_accel_free(accelerator_);56 64 delete x_; 57 65 delete y_; … … 59 67 60 68 61 int GSLInterpolation::error_status(void) const62 {63 return gsl_status_;64 }65 66 67 69 double GSLInterpolation::evaluate(double x) 68 70 { 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 } 71 77 return evaluation_; 72 78 } … … 75 81 double GSLInterpolation::evaluate_derivative(double x) 76 82 { 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 } 79 89 return evaluation_; 80 90 } … … 83 93 double GSLInterpolation::evaluate_derivative2(double x) 84 94 { 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 } 87 101 return evaluation_; 88 102 } … … 91 105 double GSLInterpolation::evaluate_integral(double a, double b) 92 106 { 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 } 95 113 return evaluation_; 96 114 } 
trunk/yat/regression/GSLInterpolation.h
r1722 r1724 52 52 determined by the type of interpolation used. 53 53 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. 58 56 59 57 \since New in yat 0.5 … … 73 71 not defined. 74 72 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. 80 75 */ 81 76 GSLInterpolation(const gsl_interp_type*, const utility::VectorBase& x, … … 88 83 89 84 /** 90 \brief Check the error status91 92 Some underlying GSL functions return GSL error codes. The error93 code is stored internally and should be checked for by the94 caller. Refer to the gsl_errno.h for the error code listing.95 96 \return The current error status. A zero return values97 indicates no errors where encountered.98 */99 int error_status(void) const;100 101 /**102 85 \brief Calculate the interpolated value for \a x. 103 104 GSL error status is set if evaluation is requested outside the105 range defined by the interpolation algorithm. Use function106 error_status to check for errors.107 86 108 87 \return The interpolated value of \f$ y \f$ for a given point 109 88 \a x. 110 89 111 \see error_status(void) 90 \throw GSL_error if evaluation is requested outside the range 91 defined by the interpolation algorithm. 112 92 */ 113 93 double evaluate(double x); … … 117 97 \a x. 118 98 119 GSL error status is set if evaluation is requested outside the120 range defined by the interpolation algorithm. Use function121 error_status to check for errors.122 123 99 \return The derivative. 124 100 125 \see error_status(void) 101 \throw GSL_error if evaluation is requested outside the range 102 defined by the interpolation algorithm. 126 103 */ 127 104 double evaluate_derivative(double x); … … 131 108 function at \a x. 132 109 133 GSL error status is set if evaluation is requested outside the134 range defined by the interpolation algorithm. Use function135 error_status to check for errors.136 137 110 \return The 2nd derivative. 138 111 139 \see error_status(void) 112 \throw GSL_error if evaluation is requested outside the range 113 defined by the interpolation algorithm. 140 114 */ 141 115 double evaluate_derivative2(double x); … … 145 119 function over the range \f$ [a,b] \f$. 146 120 147 GSL error status is set if evaluation is requested outside the148 range defined by the interpolation algorithm. Use function149 error_status to check for errors.150 151 121 \return The integral. 152 122 153 \see error_status(void) 123 \throw GSL_error if evaluation is requested outside the range 124 defined by the interpolation algorithm. 154 125 */ 155 126 double evaluate_integral(double a, double b); … … 185 156 double evaluation_; 186 157 gsl_interp* interpolator_; 187 int gsl_status_;188 158 double* x_; 189 159 double* y_;
Note: See TracChangeset
for help on using the changeset viewer.