@@ -57,7 +57,7 @@ struct MultivariatePolynomialContainer {
5757class MultivariatePolynomialParametersHelper
5858{
5959 public:
60- // / \returns number of parameters for given dimension and degree of polynomials
60+ // / \returns number of parameters for given dimension and degree of polynomials at compile time
6161 // / calculates the number of parameters for a multivariate polynomial for given degree: nParameters = (n+d-1 d) -> binomial coefficient
6262 // / see: https://mathoverflow.net/questions/225953/number-of-polynomial-terms-for-certain-degree-and-certain-number-of-variables
6363 template <uint32_t Degree, uint32_t Dim>
@@ -70,7 +70,19 @@ class MultivariatePolynomialParametersHelper
7070 }
7171 }
7272
73- // / \returns the number of parameters for interaction terms only (see: https://en.wikipedia.org/wiki/Combination)
73+ // / \returns number of parameters for given dimension and degree of polynomials
74+ // / calculates the number of parameters for a multivariate polynomial for given degree: nParameters = (n+d-1 d) -> binomial coefficient
75+ // / see: https://mathoverflow.net/questions/225953/number-of-polynomial-terms-for-certain-degree-and-certain-number-of-variables
76+ GPUd () static constexpr uint32_t getNParametersAllTerms (uint32_t degree, uint32_t dim)
77+ {
78+ if (degree == 0 ) {
79+ return binomialCoeff (dim - 1 , 0 );
80+ } else {
81+ return binomialCoeff (dim - 1 + degree, degree) + getNParametersAllTerms (degree - 1 , dim);
82+ }
83+ }
84+
85+ // / \returns the number of parameters at compile time for interaction terms only (see: https://en.wikipedia.org/wiki/Combination)
7486 template <uint32_t Degree, uint32_t Dim>
7587 GPUd () static constexpr uint32_t getNParametersInteractionOnly ()
7688 {
@@ -81,6 +93,17 @@ class MultivariatePolynomialParametersHelper
8193 }
8294 }
8395
96+ // / \returns the number of parameters for interaction terms only (see: https://en.wikipedia.org/wiki/Combination)
97+ GPUd () static constexpr uint32_t getNParametersInteractionOnly (uint32_t degree, uint32_t dim)
98+ {
99+ if (degree == 0 ) {
100+ return binomialCoeff (dim - 1 , 0 );
101+ } else {
102+ return binomialCoeff (dim, degree) + getNParametersInteractionOnly (degree - 1 , dim);
103+ }
104+ }
105+
106+
84107 template <uint32_t Degree, uint32_t Dim, bool InteractionOnly>
85108 GPUd () static constexpr uint32_t getNParameters ()
86109 {
@@ -91,8 +114,18 @@ class MultivariatePolynomialParametersHelper
91114 }
92115 }
93116
117+ GPUd () static constexpr uint32_t getNParameters (uint32_t degree, uint32_t dim, bool interactionOnly)
118+ {
119+ if (interactionOnly) {
120+ return getNParametersInteractionOnly (degree, dim);
121+ } else {
122+ return getNParametersAllTerms (degree, dim);
123+ }
124+ }
125+
126+
94127 private:
95- // / calculate factorial of n
128+ // / calculate factorial of n at compile time
96129 // / \return returns n!
97130 template <uint32_t N>
98131 GPUd () static constexpr uint32_t factorial ()
@@ -104,13 +137,24 @@ class MultivariatePolynomialParametersHelper
104137 }
105138 }
106139
107- // / calculates binomial coefficient
140+ // / calculate factorial of n
141+ // / \return returns n!
142+ GPUd () static constexpr uint32_t factorial (uint32_t n) { return n == 0 || n == 1 ? 1 : n * factorial (n - 1 ); }
143+
144+ // / calculates binomial coefficient at compile time
108145 // / \return returns (n k)
109146 template <uint32_t N, uint32_t K>
110147 GPUd () static constexpr uint32_t binomialCoeff ()
111148 {
112149 return factorial<N>() / (factorial<K>() * factorial<N - K>());
113150 }
151+
152+ // / calculates binomial coefficient
153+ // / \return returns (n k)
154+ GPUd () static constexpr uint32_t binomialCoeff (uint32_t n, uint32_t k)
155+ {
156+ return factorial (n) / (factorial (k) * factorial (n - k));
157+ }
114158};
115159
116160// / Helper struct for evaluating a multidimensional polynomial using compile time evaluated formula
0 commit comments