https://mooseframework.inl.gov
MultiDimPolynomialGenerator.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 
11 
12 namespace StochasticTools
13 {
14 
15 std::vector<std::vector<unsigned int>>
17  const unsigned int order,
18  const bool include_bias)
19 {
20  // Compute full tensor tuple
21  std::vector<std::vector<unsigned int>> tuple_1d(ndim);
22  for (unsigned int d = 0; d < ndim; ++d)
23  {
24  tuple_1d[d].resize(order);
25  for (unsigned int i = 0; i < order; ++i)
26  tuple_1d[d][i] = i;
27  }
28 
29  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  for (unsigned int p = 0; p < tensor_tuple.numRows(); ++p)
34  {
35  std::vector<unsigned int> dorder = tensor_tuple.computeRow(p);
36  unsigned int sum = std::accumulate(dorder.begin(), dorder.end(), 0);
37  if (sum < order)
38  tuple.push_back(dorder);
39  }
40 
41  std::sort(tuple.begin(), tuple.end(), sortTuple);
42 
43  if (!include_bias)
44  tuple.erase(tuple.begin()); // Erase intercept terms.
45 
46  return tuple;
47 }
48 
49 bool
50 MultiDimPolynomialGenerator::sortTuple(const std::vector<unsigned int> & first,
51  const std::vector<unsigned int> & second)
52 {
53  // Sort by sum
54  if (std::accumulate(first.begin(), first.end(), 0) <
55  std::accumulate(second.begin(), second.end(), 0))
56  return true;
57  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  for (unsigned int d = 0; d < first.size(); ++d)
63  {
64  if (first[d] == second[d])
65  continue;
66  return (first[d] > second[d]);
67  }
68 
69  return false;
70 }
71 
72 }
static std::vector< std::vector< unsigned int > > generateTuple(unsigned int n_dims, unsigned int max_degree, bool include_bias=true)
Function computing for computing _tuple Example for ndim = 3, order = 4: | 0 | 1 0 0 | 2 1 1 0 0 0 | ...
Enum for batch type in stochastic tools MultiApp.
static bool sortTuple(const std::vector< unsigned int > &first, const std::vector< unsigned int > &second)
Tuple sorter function.
std::vector< T > computeRow(std::size_t row) const
Compute specified row of Cartesian product matrix.
std::size_t numRows() const
Total number of rows in the complete matrix.