Line data Source code
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 : 19 : int PolynomialFit::_file_number = 0; 20 : 21 202 : PolynomialFit::PolynomialFit(const std::vector<Real> & x, 22 : const std::vector<Real> & y, 23 : unsigned int order, 24 202 : bool truncate_order) 25 202 : : LeastSquaresFitBase(x, y), _order(order), _truncate_order(truncate_order) 26 : { 27 202 : if (_x.size() == 0) 28 0 : mooseError("PolynomialFit does not allow empty input vectors"); 29 202 : if (_truncate_order) 30 : { 31 165 : if (_x.size() <= _order) 32 0 : _order = _x.size() - 1; 33 : } 34 37 : else if (_x.size() <= order) 35 4 : mooseError("PolynomialFit requires an order less than the size of the input vector"); 36 : 37 198 : _num_coeff = _order + 1; 38 198 : } 39 : 40 : void 41 196 : PolynomialFit::fillMatrix() 42 : { 43 196 : unsigned int num_rows = _x.size(); 44 196 : unsigned int num_cols = _order + 1; 45 196 : _matrix.resize(num_rows * num_cols); 46 : 47 589 : for (unsigned int col = 0; col < num_cols; ++col) 48 4172 : for (unsigned int row = 0; row < num_rows; ++row) 49 3779 : _matrix[(col * num_rows) + row] = MathUtils::pow(_x[row], col); 50 196 : } 51 : 52 : Real 53 665 : PolynomialFit::sample(Real x) 54 : { 55 665 : unsigned int size = _coeffs.size(); 56 665 : Real value = 0; 57 : 58 665 : Real curr_x = 1; 59 2000 : for (unsigned int i = 0; i < size; ++i) 60 : { 61 1335 : value += _coeffs[i] * curr_x; 62 1335 : curr_x *= x; 63 : } 64 665 : return value; 65 : }