https://mooseframework.inl.gov
PolynomialFit.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "PolynomialFit.h"
11 
12 #include "MooseError.h"
13 #include "MathUtils.h"
14 
15 // C++ includes
16 #include <fstream>
17 #include <algorithm>
18 
20 
21 PolynomialFit::PolynomialFit(const std::vector<Real> & x,
22  const std::vector<Real> & y,
23  unsigned int order,
24  bool truncate_order)
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 }
39 
40 void
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 }
51 
52 Real
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 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
virtual void fillMatrix() override
Helper function that creates the matrix necessary for the least squares algorithm.
Definition: PolynomialFit.C:41
virtual Real sample(Real x) override
This function will take an independent variable input and will return the dependent variable based on...
Definition: PolynomialFit.C:53
unsigned int _num_coeff
The number of coefficients.
static int _file_number
File number.
Definition: PolynomialFit.h:42
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
Base class for linear least squares fit method.
std::vector< Real > _x
Independent variable.
bool _truncate_order
Flag to implement a truncated polynomial.
Definition: PolynomialFit.h:40
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
PolynomialFit(const std::vector< Real > &x, const std::vector< Real > &y, unsigned int order, bool truncate_order=false)
Definition: PolynomialFit.C:21
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)
T pow(T x, int e)
Definition: MathUtils.h:91
unsigned int _order
Order of the polynomial.
Definition: PolynomialFit.h:38