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...
 

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 20 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 17 of file PolynomialFit.C.

21  : LeastSquaresFitBase(x, y), _order(order), _truncate_order(truncate_order)
22 {
23  if (_truncate_order) // && (_x.size() / 10) < _order)
24  {
25  if (_x.size() == 1)
26  _order = 0;
27  else
28  {
29  _order = (_x.size() / 10) + 1;
30 
31  if (_order > order)
32  _order = order;
33  }
34  }
35  else if (_x.size() < order)
36  throw std::domain_error(
37  "Polynomial Fit requires an order less than the size of the input vector");
38 
39  _num_coeff = _order + 1;
40 }
static PetscErrorCode Vec x
unsigned int _num_coeff
The number of coefficients.
LeastSquaresFitBase(const std::vector< Real > &x, const std::vector< Real > &y)
std::vector< Real > _x
Independent variable.
bool _truncate_order
Flag to implement a truncated polynomial.
Definition: PolynomialFit.h:41
unsigned int _order
Order of the polynomial.
Definition: PolynomialFit.h:39

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

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

void PolynomialFit::fillMatrix ( )
overrideprotectedvirtual

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

Implements LeastSquaresFitBase.

Definition at line 43 of file PolynomialFit.C.

44 {
45  unsigned int num_rows = _x.size();
46  unsigned int num_cols = _order + 1;
47  _matrix.resize(num_rows * num_cols);
48 
49  for (unsigned int col = 0; col < num_cols; ++col)
50  for (unsigned int row = 0; row < num_rows; ++row)
51  {
52  Real value = 1;
53  for (unsigned int i = 0; i < col; ++i)
54  value *= _x[row];
55 
56  _matrix[(col * num_rows) + row] = value;
57  }
58 }
std::vector< Real > _x
Independent variable.
std::vector< Real > _matrix
Basis functions evaluated at each independent variable (note: actually a vector)
unsigned int _order
Order of the polynomial.
Definition: PolynomialFit.h:39

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

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

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

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 61 of file PolynomialFit.C.

Referenced by LeastSquaresFit::execute().

62 {
63  unsigned int size = _coeffs.size();
64  Real value = 0;
65 
66  Real curr_x = 1;
67  for (unsigned int i = 0; i < size; ++i)
68  {
69  value += _coeffs[i] * curr_x;
70  curr_x *= x;
71  }
72  return value;
73 }
static PetscErrorCode Vec x
std::vector< Real > _coeffs
Vector of coefficients of the least squares fit.

Member Data Documentation

◆ _coeffs

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

Vector of coefficients of the least squares fit.

Definition at line 77 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 43 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 75 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 79 of file LeastSquaresFitBase.h.

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

◆ _order

unsigned int PolynomialFit::_order
protected

Order of the polynomial.

Definition at line 39 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 41 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 73 of file LeastSquaresFitBase.h.

Referenced by LeastSquaresFitBase::doLeastSquares().


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