LCOV - code coverage report
Current view: top level - src/utils - MultiDimPolynomialGenerator.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: #32971 (54bef8) with base c6cf66 Lines: 23 23 100.0 %
Date: 2026-05-29 20:40:35 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          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         354 : MultiDimPolynomialGenerator::generateTuple(const unsigned int ndim,
      17             :                                            const unsigned int order,
      18             :                                            const bool include_bias)
      19             : {
      20             :   // Compute full tensor tuple
      21         354 :   std::vector<std::vector<unsigned int>> tuple_1d(ndim);
      22        1465 :   for (unsigned int d = 0; d < ndim; ++d)
      23             :   {
      24        1111 :     tuple_1d[d].resize(order);
      25        5989 :     for (unsigned int i = 0; i < order; ++i)
      26        4878 :       tuple_1d[d][i] = i;
      27             :   }
      28             : 
      29         354 :   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      652782 :   for (unsigned int p = 0; p < tensor_tuple.numRows(); ++p)
      34             :   {
      35      652428 :     std::vector<unsigned int> dorder = tensor_tuple.computeRow(p);
      36      652428 :     unsigned int sum = std::accumulate(dorder.begin(), dorder.end(), 0);
      37      652428 :     if (sum < order)
      38       44271 :       tuple.push_back(dorder);
      39      652428 :   }
      40             : 
      41         354 :   std::sort(tuple.begin(), tuple.end(), sortTuple);
      42             : 
      43         354 :   if (!include_bias)
      44             :     tuple.erase(tuple.begin()); // Erase intercept terms.
      45             : 
      46         354 :   return tuple;
      47         354 : }
      48             : 
      49             : bool
      50      471777 : MultiDimPolynomialGenerator::sortTuple(const std::vector<unsigned int> & first,
      51             :                                        const std::vector<unsigned int> & second)
      52             : {
      53             :   // Sort by sum
      54      471777 :   if (std::accumulate(first.begin(), first.end(), 0) <
      55             :       std::accumulate(second.begin(), second.end(), 0))
      56             :     return true;
      57      392372 :   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      618968 :   for (unsigned int d = 0; d < first.size(); ++d)
      63             :   {
      64      618968 :     if (first[d] == second[d])
      65             :       continue;
      66      347476 :     return (first[d] > second[d]);
      67             :   }
      68             : 
      69             :   return false;
      70             : }
      71             : 
      72             : }

Generated by: LCOV version 1.14