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...
 
void dumpSampleFile (std::string base_name, std::string x_label="X", std::string y_label="Y", float xmin=0, float xmax=0, float ymin=0, float ymax=0)
 Dump GNUPLOT input files that can be run to show the data points and function fits. 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:53
unsigned int _order
Order of the polynomial.
Definition: PolynomialFit.h:51

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)

◆ dumpSampleFile()

void PolynomialFit::dumpSampleFile ( std::string  base_name,
std::string  x_label = "X",
std::string  y_label = "Y",
float  xmin = 0,
float  xmax = 0,
float  ymin = 0,
float  ymax = 0 
)

Dump GNUPLOT input files that can be run to show the data points and function fits.

Definition at line 76 of file PolynomialFit.C.

83 {
84  std::stringstream filename, filename_pts;
85  const unsigned char fill_character = '0';
86  const unsigned int field_width = 4;
87 
88  filename.fill(fill_character);
89  filename << base_name;
90  filename.width(field_width);
91  filename << _file_number << ".plt";
92 
93  filename_pts.fill(fill_character);
94  filename_pts << base_name << "_pts";
95  filename_pts.width(field_width);
96  filename_pts << _file_number << ".dat";
97 
98  /* First dump the GNUPLOT file with the Least Squares Equations */
99  std::ofstream out(filename.str().c_str());
100  out.precision(15);
101  out.fill(fill_character);
102 
103  out << "set terminal postscript color enhanced\n"
104  << "set output \"" << base_name;
105  out.width(field_width);
106  out << _file_number << ".eps\"\n"
107  << "set xlabel \"" << x_label << "\"\n"
108  << "set ylabel \"" << y_label << "\"\n";
109  if (xmin != 0 && xmax != 0)
110  out << "set xrange [" << xmin << ":" << xmax << "]\n";
111  if (ymin != 0 && ymax != 0)
112  out << "set yrange [" << ymin << ":" << ymax << "]\n";
113  out << "set key left top\n"
114  << "f(x)=";
115 
116  for (unsigned int i = 0; i < _coeffs.size(); ++i)
117  {
118  if (i)
119  out << "+";
120 
121  out << _coeffs[i];
122  for (unsigned int j = 0; j < i; ++j)
123  out << "*x";
124  }
125  out << "\nplot f(x) with lines, '" << filename_pts.str() << "' using 1:2 title \"Points\"\n";
126  out.close();
127 
128  libmesh_assert(_x.size() == _y.size());
129 
130  out.open(filename_pts.str().c_str());
131  if (out.fail())
132  throw std::runtime_error(std::string("Unable to open file ") + filename_pts.str());
133 
134  /* Next dump the data points into a seperate file */
135  for (unsigned int i = 0; i < _x.size(); ++i)
136  out << _x[i] << " " << _y[i] << "\n";
137  out << std::endl;
138 
139  ++_file_number;
140  out.close();
141 }
static int _file_number
File number.
Definition: PolynomialFit.h:55
std::vector< Real > _y
Dependent variable.
std::vector< Real > _x
Independent variable.
std::vector< Real > _coeffs
Vector of coefficients of the least squares fit.

◆ 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:51

◆ 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(), dumpSampleFile(), LeastSquaresFitBase::getCoefficients(), and sample().

◆ _file_number

int PolynomialFit::_file_number = 0
staticprotected

File number.

Definition at line 55 of file PolynomialFit.h.

Referenced by dumpSampleFile().

◆ _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 51 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 53 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(), and dumpSampleFile().


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