www.mooseframework.org
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
LeastSquaresFitBase Class Referenceabstract

Base class for linear least squares fit method. More...

#include <LeastSquaresFitBase.h>

Inheritance diagram for LeastSquaresFitBase:
[legend]

Public Member Functions

 LeastSquaresFitBase (const std::vector< Real > &x, const std::vector< Real > &y)
 
virtual ~LeastSquaresFitBase ()=default
 
virtual void generate ()
 Generate the fit. More...
 
virtual Real sample (Real x)=0
 This function will take an independent variable input and will return the dependent variable based on the generated fit. More...
 
unsigned int getSampleSize ()
 Size of the array holding the points. More...
 
const std::vector< Real > & getCoefficients ()
 Const reference to the vector of coefficients of the least squares fit. More...
 

Protected Member Functions

virtual void fillMatrix ()=0
 Helper function that creates the matrix necessary for the least squares algorithm. More...
 
void doLeastSquares ()
 Wrapper for the LAPACK dgels function. More...
 

Protected Attributes

std::vector< Real > _x
 Independent variable. More...
 
std::vector< Real > _y
 Dependent variable. More...
 
std::vector< Real > _matrix
 Basis functions evaluated at each independent variable (note: actually a vector) More...
 
std::vector< Real > _coeffs
 Vector of coefficients of the least squares fit. More...
 
unsigned int _num_coeff
 The number of coefficients. More...
 

Detailed Description

Base class for linear least squares fit method.

Derived classes must implement the fillMatrix() and sample() methods with the functional form of the fit.

Requires: LAPACK

Definition at line 24 of file LeastSquaresFitBase.h.

Constructor & Destructor Documentation

◆ LeastSquaresFitBase()

LeastSquaresFitBase::LeastSquaresFitBase ( const std::vector< Real > &  x,
const std::vector< Real > &  y 
)

Definition at line 14 of file LeastSquaresFitBase.C.

15  : _x(x), _y(y)
16 {
17 }
static PetscErrorCode Vec x
std::vector< Real > _y
Dependent variable.
std::vector< Real > _x
Independent variable.

◆ ~LeastSquaresFitBase()

virtual LeastSquaresFitBase::~LeastSquaresFitBase ( )
virtualdefault

Member Function Documentation

◆ doLeastSquares()

void LeastSquaresFitBase::doLeastSquares ( )
protected

Wrapper for the LAPACK dgels function.

Called by generate() to perform the least squares fit

Definition at line 27 of file LeastSquaresFitBase.C.

Referenced by generate().

28 {
29  char mode = 'N';
30  int num_rows = _x.size();
31  unsigned int num_coeff = _num_coeff;
32  int num_rhs = 1;
33  int buffer_size = -1;
34  Real opt_buffer_size;
35  Real * buffer;
36  int return_value = 0;
37 
38  // Must copy _y because the call to dgels destroys the original values
39  std::vector<Real> rhs = _y;
40 
41  FORTRAN_CALL(dgels)
42  (&mode,
43  &num_rows,
44  &num_coeff,
45  &num_rhs,
46  &_matrix[0],
47  &num_rows,
48  &rhs[0],
49  &num_rows,
50  &opt_buffer_size,
51  &buffer_size,
52  &return_value);
53  if (return_value)
54  throw std::runtime_error("Call to Fortran routine 'dgels' returned non-zero exit code");
55 
56  buffer_size = (int)opt_buffer_size;
57 
58  buffer = new Real[buffer_size];
59  FORTRAN_CALL(dgels)
60  (&mode,
61  &num_rows,
62  &num_coeff,
63  &num_rhs,
64  &_matrix[0],
65  &num_rows,
66  &rhs[0],
67  &num_rows,
68  buffer,
69  &buffer_size,
70  &return_value);
71  delete[] buffer;
72 
73  if (return_value)
74  throw std::runtime_error("Call to Fortran routine 'dgels' returned non-zero exit code");
75 
76  _coeffs.resize(num_coeff);
77  for (unsigned int i = 0; i < num_coeff; ++i)
78  _coeffs[i] = rhs[i];
79 }
unsigned int _num_coeff
The number of coefficients.
void FORTRAN_CALL() dgels(...)
std::vector< Real > _y
Dependent variable.
std::vector< Real > _x
Independent variable.
std::vector< Real > _coeffs
Vector of coefficients of the least squares fit.
std::vector< Real > _matrix
Basis functions evaluated at each independent variable (note: actually a vector)

◆ fillMatrix()

virtual void LeastSquaresFitBase::fillMatrix ( )
protectedpure virtual

Helper function that creates the matrix necessary for the least squares algorithm.

Implemented in PolynomialFit.

Referenced by generate().

◆ generate()

void LeastSquaresFitBase::generate ( )
virtual

Generate the fit.

This function must be called prior to using sample. Note: If you pass a vector that contains duplicate independent measures the call to LAPACK will fail

Definition at line 20 of file LeastSquaresFitBase.C.

Referenced by LeastSquaresFit::execute(), and LeastSquaresFitHistory::execute().

21 {
22  fillMatrix();
24 }
void doLeastSquares()
Wrapper for the LAPACK dgels function.
virtual void fillMatrix()=0
Helper function that creates the matrix necessary for the least squares algorithm.

◆ getCoefficients()

const std::vector< Real > & LeastSquaresFitBase::getCoefficients ( )

Const reference to the vector of coefficients of the least squares fit.

Parameters
returnvector of coefficients

Definition at line 88 of file LeastSquaresFitBase.C.

Referenced by LeastSquaresFit::execute(), and LeastSquaresFitHistory::execute().

89 {
90  return _coeffs;
91 }
std::vector< Real > _coeffs
Vector of coefficients of the least squares fit.

◆ getSampleSize()

unsigned int LeastSquaresFitBase::getSampleSize ( )

Size of the array holding the points.

Returns
number of sample points

Definition at line 82 of file LeastSquaresFitBase.C.

83 {
84  return _x.size();
85 }
std::vector< Real > _x
Independent variable.

◆ sample()

virtual Real LeastSquaresFitBase::sample ( Real  x)
pure virtual

This function will take an independent variable input and will return the dependent variable based on the generated fit.

Parameters
xindependent variable
Returns
dependent variable

Implemented in PolynomialFit.

Member Data Documentation

◆ _coeffs

std::vector<Real> LeastSquaresFitBase::_coeffs
protected

Vector of coefficients of the least squares fit.

Definition at line 77 of file LeastSquaresFitBase.h.

Referenced by doLeastSquares(), PolynomialFit::dumpSampleFile(), getCoefficients(), and PolynomialFit::sample().

◆ _matrix

std::vector<Real> LeastSquaresFitBase::_matrix
protected

Basis functions evaluated at each independent variable (note: actually a vector)

Definition at line 75 of file LeastSquaresFitBase.h.

Referenced by doLeastSquares(), and PolynomialFit::fillMatrix().

◆ _num_coeff

unsigned int LeastSquaresFitBase::_num_coeff
protected

The number of coefficients.

Definition at line 79 of file LeastSquaresFitBase.h.

Referenced by doLeastSquares(), and PolynomialFit::PolynomialFit().

◆ _x

std::vector<Real> LeastSquaresFitBase::_x
protected

◆ _y

std::vector<Real> LeastSquaresFitBase::_y
protected

Dependent variable.

Definition at line 73 of file LeastSquaresFitBase.h.

Referenced by doLeastSquares(), and PolynomialFit::dumpSampleFile().


The documentation for this class was generated from the following files: