@@ -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,16 @@ 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+
84106 template <uint32_t Degree, uint32_t Dim, bool InteractionOnly>
85107 GPUd () static constexpr uint32_t getNParameters ()
86108 {
@@ -91,8 +113,17 @@ class MultivariatePolynomialParametersHelper
91113 }
92114 }
93115
116+ GPUd () static constexpr uint32_t getNParameters (uint32_t degree, uint32_t dim, bool interactionOnly)
117+ {
118+ if (interactionOnly) {
119+ return getNParametersInteractionOnly (degree, dim);
120+ } else {
121+ return getNParametersAllTerms (degree, dim);
122+ }
123+ }
124+
94125 private:
95- // / calculate factorial of n
126+ // / calculate factorial of n at compile time
96127 // / \return returns n!
97128 template <uint32_t N>
98129 GPUd () static constexpr uint32_t factorial ()
@@ -104,13 +135,24 @@ class MultivariatePolynomialParametersHelper
104135 }
105136 }
106137
107- // / calculates binomial coefficient
138+ // / calculate factorial of n
139+ // / \return returns n!
140+ GPUd () static constexpr uint32_t factorial (uint32_t n) { return n == 0 || n == 1 ? 1 : n * factorial (n - 1 ); }
141+
142+ // / calculates binomial coefficient at compile time
108143 // / \return returns (n k)
109144 template <uint32_t N, uint32_t K>
110145 GPUd () static constexpr uint32_t binomialCoeff ()
111146 {
112147 return factorial<N>() / (factorial<K>() * factorial<N - K>());
113148 }
149+
150+ // / calculates binomial coefficient
151+ // / \return returns (n k)
152+ GPUd () static constexpr uint32_t binomialCoeff (uint32_t n, uint32_t k)
153+ {
154+ return factorial (n) / (factorial (k) * factorial (n - k));
155+ }
114156};
115157
116158// / Helper struct for evaluating a multidimensional polynomial using compile time evaluated formula
0 commit comments