Skip to content

Commit 7e658a5

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

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-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: 48 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,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

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)