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 ()
 
 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...
 
void setVariables (const std::vector< Real > &x, const std::vector< Real > &y)
 

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 23 of file LeastSquaresFitBase.h.

Constructor & Destructor Documentation

◆ LeastSquaresFitBase() [1/2]

LeastSquaresFitBase::LeastSquaresFitBase ( )

Definition at line 19 of file LeastSquaresFitBase.C.

19 {}

◆ LeastSquaresFitBase() [2/2]

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

Definition at line 21 of file LeastSquaresFitBase.C.

22  : _x(x), _y(y)
23 {
24 }
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 45 of file LeastSquaresFitBase.C.

Referenced by generate().

46 {
47  _coeffs.resize(_num_coeff);
48 
49  typedef Eigen::Matrix<Real, Eigen::Dynamic, 1> SolveVec;
50  auto b = Eigen::Map<SolveVec, Eigen::Unaligned>(_y.data(), _y.size());
51  auto x = Eigen::Map<SolveVec, Eigen::Unaligned>(_coeffs.data(), _num_coeff);
52  typedef Eigen::Matrix<Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor> SolveMatrix;
53  auto A = Eigen::Map<SolveMatrix, Eigen::Unaligned>(_matrix.data(), _y.size(), _num_coeff);
54  x = A.colPivHouseholderQr().solve(b);
55 }
unsigned int _num_coeff
The number of coefficients.
std::vector< Real > _y
Dependent 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 34 of file LeastSquaresFitBase.C.

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

35 {
36  if (_x.empty())
37  mooseError("Empty variables in LeastSquaresFitBase. x and y must be set in the constructor or "
38  "using setVariables(x, y)");
39 
40  fillMatrix();
42 }
void doLeastSquares()
Wrapper for the LAPACK dgels function.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:299
virtual void fillMatrix()=0
Helper function that creates the matrix necessary for the least squares algorithm.
std::vector< Real > _x
Independent variable.

◆ 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 64 of file LeastSquaresFitBase.C.

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

65 {
66  return _coeffs;
67 }
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 58 of file LeastSquaresFitBase.C.

59 {
60  return _x.size();
61 }
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.

◆ setVariables()

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

Definition at line 27 of file LeastSquaresFitBase.C.

28 {
29  _x = x;
30  _y = y;
31 }
std::vector< Real > _y
Dependent variable.
std::vector< Real > _x
Independent variable.

Member Data Documentation

◆ _coeffs

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

Vector of coefficients of the least squares fit.

Definition at line 79 of file LeastSquaresFitBase.h.

Referenced by doLeastSquares(), 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 77 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 81 of file LeastSquaresFitBase.h.

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

◆ _x

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

Independent variable.

Definition at line 73 of file LeastSquaresFitBase.h.

Referenced by PolynomialFit::fillMatrix(), generate(), getSampleSize(), PolynomialFit::PolynomialFit(), and setVariables().

◆ _y

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

Dependent variable.

Definition at line 75 of file LeastSquaresFitBase.h.

Referenced by doLeastSquares(), and setVariables().


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