25 #ifndef GRINS_REGRESSION_HELPER_H
26 #define GRINS_REGRESSION_HELPER_H
32 #include "libmesh/libmesh_common.h"
34 #ifdef GRINS_HAVE_CPPUNIT
36 #include <cppunit/extensions/HelperMacros.h>
38 #endif // GRINS_HAVE_CPPUNIT
51 libMesh::Real
linear_slope(std::vector<libMesh::Real> x, std::vector<libMesh::Real> y,
unsigned int start_index,
unsigned int end_index)
53 libmesh_assert(x.size() == y.size());
54 libmesh_assert(start_index < end_index);
56 unsigned int n = x.size();
57 libmesh_assert(end_index < n);
59 libMesh::Real sum_y = 0.0,
64 for (
unsigned int i=start_index; i<=end_index; i++)
66 libMesh::Real xi = x[i];
67 libMesh::Real yi = y[i];
76 libMesh::Real slope = ( n*sum_xy - sum_x*sum_y )/( n*sum_x2 - (sum_x*sum_x) );
81 libMesh::Real
linear_intercept(std::vector<libMesh::Real> x, std::vector<libMesh::Real> y,
unsigned int start_index,
unsigned int end_index)
83 libmesh_assert(x.size() == y.size());
84 libmesh_assert(start_index < end_index);
86 unsigned int n = x.size();
87 libmesh_assert(end_index < n);
89 libMesh::Real sum_y = 0.0,
94 for (
unsigned int i=start_index; i<=end_index; i++)
96 libMesh::Real xi = x[i];
97 libMesh::Real yi = y[i];
105 libMesh::Real intercept = ( sum_y*sum_x2 - sum_x*sum_xy )/( n*sum_x2 - (sum_x*sum_x) );
110 void check_convergence_rate(std::vector<libMesh::Real> x, std::vector<libMesh::Real> y,
unsigned int start_index,
unsigned int end_index, libMesh::Real convergence_rate, libMesh::Real tol)
112 libmesh_assert(tol > 0.0);
113 libmesh_assert(x.size() == y.size());
114 libmesh_assert(start_index < end_index);
115 libmesh_assert(end_index < x.size());
117 libMesh::Real calculated_rate =
linear_slope(x,y,start_index,end_index);
119 #ifdef GRINS_HAVE_CPPUNIT
121 CPPUNIT_ASSERT_DOUBLES_EQUAL(convergence_rate,calculated_rate,tol);
123 if (std::abs(convergence_rate - calculated_rate) > tol)
125 std::stringstream ss;
126 ss <<
"ERROR" <<std::endl
127 <<
"calculated convergence rate: " <<calculated_rate <<std::endl
128 <<
"desired convergence rate: " <<convergence_rate <<std::endl
129 <<
"tolerance: " <<tol <<std::endl;
131 libmesh_error_msg(ss.str());
133 #endif // GRINS_HAVE_CPPUNIT
139 #endif // GRINS_REGRESSION_HELPER_H
libMesh::Real linear_intercept(std::vector< libMesh::Real > x, std::vector< libMesh::Real > y, unsigned int start_index, unsigned int end_index)
y-intercept for linear regression
void check_convergence_rate(std::vector< libMesh::Real > x, std::vector< libMesh::Real > y, unsigned int start_index, unsigned int end_index, libMesh::Real convergence_rate, libMesh::Real tol)
Check the convergence rate of the supplied data to within the given absolute tolerance.
libMesh::Real linear_slope(std::vector< libMesh::Real > x, std::vector< libMesh::Real > y, unsigned int start_index, unsigned int end_index)
Slope for linear regression.