www.mooseframework.org
Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
PolynomialFit Class Reference

Least squares polynomial fit. More...

#include <PolynomialFit.h>

Inheritance diagram for PolynomialFit:
[legend]

Public Member Functions

 PolynomialFit (const std::vector< Real > &x, const std::vector< Real > &y, unsigned int order, bool truncate_order=false)
 
virtual Real sample (Real x) override
 This function will take an independent variable input and will return the dependent variable based on the generated fit. More...
 
virtual void generate ()
 Generate the 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 () override
 Helper function that creates the matrix necessary for the least squares algorithm. More...
 
void doLeastSquares ()
 Wrapper for the LAPACK dgels function. More...
 

Protected Attributes

unsigned int _order
 Order of the polynomial. More...
 
bool _truncate_order
 Flag to implement a truncated polynomial. More...
 
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...
 

Static Protected Attributes

static int _file_number = 0
 File number. More...
 

Detailed Description

Least squares polynomial fit.

Requires: LAPACK

Definition at line 19 of file PolynomialFit.h.

Constructor & Destructor Documentation

◆ PolynomialFit()

PolynomialFit::PolynomialFit ( const std::vector< Real > &  x,
const std::vector< Real > &  y,
unsigned int  order,
bool  truncate_order = false 
)

Definition at line 21 of file PolynomialFit.C.

25  : LeastSquaresFitBase(x, y), _order(order), _truncate_order(truncate_order)
26 {
27  if (_x.size() == 0)
28  mooseError("PolynomialFit does not allow empty input vectors");
29  if (_truncate_order)
30  {
31  if (_x.size() <= _order)
32  _order = _x.size() - 1;
33  }
34  else if (_x.size() <= order)
35  mooseError("PolynomialFit requires an order less than the size of the input vector");
36 
37  _num_coeff = _order + 1;
38 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:299
unsigned int _num_coeff
The number of coefficients.
std::vector< Real > _x
Independent variable.
bool _truncate_order
Flag to implement a truncated polynomial.
Definition: PolynomialFit.h:40
unsigned int _order
Order of the polynomial.
Definition: PolynomialFit.h:38

Member Function Documentation

◆ doLeastSquares()

void LeastSquaresFitBase::doLeastSquares ( )
protectedinherited

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 LeastSquaresFitBase::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()

void PolynomialFit::fillMatrix ( )
overrideprotectedvirtual

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

Implements LeastSquaresFitBase.

Definition at line 41 of file PolynomialFit.C.

42 {
43  unsigned int num_rows = _x.size();
44  unsigned int num_cols = _order + 1;
45  _matrix.resize(num_rows * num_cols);
46 
47  for (unsigned int col = 0; col < num_cols; ++col)
48  for (unsigned int row = 0; row < num_rows; ++row)
49  _matrix[(col * num_rows) + row] = MathUtils::pow(_x[row], col);
50 }
std::vector< Real > _x
Independent variable.
std::vector< Real > _matrix
Basis functions evaluated at each independent variable (note: actually a vector)
T pow(T x, int e)
Definition: MathUtils.h:90
unsigned int _order
Order of the polynomial.
Definition: PolynomialFit.h:38

◆ generate()

void LeastSquaresFitBase::generate ( )
virtualinherited

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 ( )
inherited

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 ( )
inherited

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()

Real PolynomialFit::sample ( Real  x)
overridevirtual

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

Implements LeastSquaresFitBase.

Definition at line 53 of file PolynomialFit.C.

Referenced by LeastSquaresFit::execute().

54 {
55  unsigned int size = _coeffs.size();
56  Real value = 0;
57 
58  Real curr_x = 1;
59  for (unsigned int i = 0; i < size; ++i)
60  {
61  value += _coeffs[i] * curr_x;
62  curr_x *= x;
63  }
64  return value;
65 }
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
std::vector< Real > _coeffs
Vector of coefficients of the least squares fit.

◆ setVariables()

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

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
protectedinherited

Vector of coefficients of the least squares fit.

Definition at line 79 of file LeastSquaresFitBase.h.

Referenced by LeastSquaresFitBase::doLeastSquares(), LeastSquaresFitBase::getCoefficients(), and sample().

◆ _file_number

int PolynomialFit::_file_number = 0
staticprotected

File number.

Definition at line 42 of file PolynomialFit.h.

◆ _matrix

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

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

Definition at line 77 of file LeastSquaresFitBase.h.

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

◆ _num_coeff

unsigned int LeastSquaresFitBase::_num_coeff
protectedinherited

The number of coefficients.

Definition at line 81 of file LeastSquaresFitBase.h.

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

◆ _order

unsigned int PolynomialFit::_order
protected

Order of the polynomial.

Definition at line 38 of file PolynomialFit.h.

Referenced by fillMatrix(), and PolynomialFit().

◆ _truncate_order

bool PolynomialFit::_truncate_order
protected

Flag to implement a truncated polynomial.

Definition at line 40 of file PolynomialFit.h.

Referenced by PolynomialFit().

◆ _x

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

◆ _y

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

Dependent variable.

Definition at line 75 of file LeastSquaresFitBase.h.

Referenced by LeastSquaresFitBase::doLeastSquares(), and LeastSquaresFitBase::setVariables().


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