Skip to content

Commit 5663a8e

Browse files
committed
TPCFastTransformation: Fix runtime parameters on CPU for polynoms.
1 parent 2525d2d commit 5663a8e

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

GPU/TPCFastTransformation/MultivariatePolynomial.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class MultivariatePolynomial : public FlatObject, public MultivariatePolynomialH
5656

5757
/// constructor for compile time evaluation of polynomial formula
5858
template <bool IsEnabled = true, typename std::enable_if<(IsEnabled && (Dim != 0 && Degree != 0)), int32_t>::type = 0>
59-
MultivariatePolynomial() : mNParams{this->getNParameters(Degree, Dim, InteractionOnly)}
59+
MultivariatePolynomial() : mNParams{this->template getNParameters<Degree, Dim, InteractionOnly>()}
6060
{
6161
construct();
6262
}

GPU/TPCFastTransformation/MultivariatePolynomialHelper.h

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ struct MultivariatePolynomialContainer {
5757
class 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

GPU/TPCFastTransformation/NDPiecewisePolynomials.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ void NDPiecewisePolynomials<Dim, Degree, InteractionOnly>::performFits(const std
165165
// check if data points are in the grid
166166
if (index == indexClamped) {
167167
// index of the polyniomial
168-
const uint32_t idx = getDataIndex(index.data()) / MultivariatePolynomialParametersHelper::getNParameters(Degree, Dim, InteractionOnly);
168+
const uint32_t idx = getDataIndex(index.data()) / MultivariatePolynomialParametersHelper::getNParameters<Degree, Dim, InteractionOnly>();
169169

170170
// store index to data point
171171
dataPointsIndices[idx].emplace_back(i);
@@ -216,7 +216,7 @@ void NDPiecewisePolynomials<Dim, Degree, InteractionOnly>::performFits(const std
216216
const auto params = MultivariatePolynomialHelper<0, 0, false>::fit(fitter, xCords, response, error, true);
217217

218218
// store parameters
219-
std::copy(params.begin(), params.end(), &mParams[i * MultivariatePolynomialParametersHelper::getNParameters(Degree, Dim, InteractionOnly)]);
219+
std::copy(params.begin(), params.end(), &mParams[i * MultivariatePolynomialParametersHelper::getNParameters<Degree, Dim, InteractionOnly>()]);
220220
}
221221
}
222222

0 commit comments

Comments
 (0)