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 "MultiDimPolynomialGenerator.h" 11 : 12 : namespace StochasticTools 13 : { 14 : 15 : std::vector<std::vector<unsigned int>> 16 762 : MultiDimPolynomialGenerator::generateTuple(const unsigned int ndim, 17 : const unsigned int order, 18 : const bool include_bias) 19 : { 20 : // Compute full tensor tuple 21 762 : std::vector<std::vector<unsigned int>> tuple_1d(ndim); 22 3134 : for (unsigned int d = 0; d < ndim; ++d) 23 : { 24 2372 : tuple_1d[d].resize(order); 25 13232 : for (unsigned int i = 0; i < order; ++i) 26 10860 : tuple_1d[d][i] = i; 27 : } 28 : 29 762 : CartesianProduct<unsigned int> tensor_tuple(tuple_1d); 30 : 31 : // Remove polynomials that exceed the maximum order 32 : std::vector<std::vector<unsigned int>> tuple; 33 1491576 : for (unsigned int p = 0; p < tensor_tuple.numRows(); ++p) 34 : { 35 1490814 : std::vector<unsigned int> dorder = tensor_tuple.computeRow(p); 36 1490814 : unsigned int sum = std::accumulate(dorder.begin(), dorder.end(), 0); 37 1490814 : if (sum < order) 38 100994 : tuple.push_back(dorder); 39 : } 40 : 41 762 : std::sort(tuple.begin(), tuple.end(), sortTuple); 42 : 43 762 : if (!include_bias) 44 : tuple.erase(tuple.begin()); // Erase intercept terms. 45 : 46 762 : return tuple; 47 762 : } 48 : 49 : bool 50 1077774 : MultiDimPolynomialGenerator::sortTuple(const std::vector<unsigned int> & first, 51 : const std::vector<unsigned int> & second) 52 : { 53 : // Sort by sum 54 1077774 : if (std::accumulate(first.begin(), first.end(), 0) < 55 : std::accumulate(second.begin(), second.end(), 0)) 56 : return true; 57 896344 : else if (std::accumulate(first.begin(), first.end(), 0) > 58 : std::accumulate(second.begin(), second.end(), 0)) 59 : return false; 60 : 61 : // Loop over elements 62 1414504 : for (unsigned int d = 0; d < first.size(); ++d) 63 : { 64 1414504 : if (first[d] == second[d]) 65 : continue; 66 794024 : return (first[d] > second[d]); 67 : } 68 : 69 : return false; 70 : } 71 : 72 : }