Project
Loading...
Searching...
No Matches
LegendrePols.h
Go to the documentation of this file.
1// Copyright 2020-2022 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
15
16#ifndef LEGENDRE_NDIM_POLYNOMINAL_H_
17#define LEGENDRE_NDIM_POLYNOMINAL_H_
18
19#include "TNamed.h"
20#include "Math/IParamFunction.h"
21#include "TMatrixD.h"
22
23#include <vector>
24
25#include <boost/math/special_functions/legendre.hpp>
26
27namespace o2::math_utils
28{
29
30// Defines the 1D Legendre Polynominals with coefficients:
31// w(u) = c_0 + c_1 * u + c_2 * 0.5 * (3 * u * u - 1) + ...
32// for u in [-1.0, 1.0]
33class Legendre1DPolynominal final : public TNamed,
35{
36 public:
42 Legendre1DPolynominal(unsigned int order) : fOrder(order) {}
43 Legendre1DPolynominal(const std::vector<double> p)
44 : fOrder(p.size() - 1), fParams(p) {}
45
46 double operator()(double x) const { return DoEvalPar(x, Parameters()); }
47 double operator()(int i, double x) const
48 {
49 return DoEvalParSingle(i, x, Parameters());
50 }
51
52 ~Legendre1DPolynominal() final = default;
53
54 const double* Parameters() const final { return &fParams.front(); }
55
56 virtual void SetParameters(const double* p) final
57 {
58 fParams = std::vector<double>(p, p + NPar());
59 }
60
61 unsigned int NPar() const final { return fParams.size(); }
62 unsigned int NOrder() const { return fOrder; }
63
64 ROOT::Math::IBaseFunctionOneDim* Clone() const final { return new Legendre1DPolynominal(fParams); }
65 TObject* Clone(const char* name) const final
66 {
67 auto n = new Legendre1DPolynominal(fParams);
68 n->SetName(name);
69 return n;
70 }
71
72 private:
73 double DoEvalPar(double x, const double* p) const final
74 {
75 double sum{0.0};
76 for (unsigned int iOrder{0}; iOrder <= fOrder; ++iOrder) {
77 sum += p[iOrder] * boost::math::legendre_p(iOrder, x);
78 }
79 return sum;
80 }
81
82 double DoEvalParSingle(int i, double x, const double* p) const
83 {
84 return p[i] * boost::math::legendre_p(i, x);
85 }
86
87 unsigned int fOrder{0};
88 std::vector<double> fParams;
89
90 ClassDefOverride(o2::math_utils::Legendre1DPolynominal, 1);
91};
92
93// Defines the 2D Legendre Polynominals with coefficients:
94// w(u, v) = c_00 +
95// c_10 * u + c_11 * v +
96// c_20 * 0.5 * (3 * u * u - 1) + c_21 * u * v + c_22 * (3 * v * v - 1) +
98// for u&v in [-1.0, 1.0]
99class Legendre2DPolynominal final : public TNamed,
101{
102 public:
104 Legendre2DPolynominal(unsigned int order) : fOrder(order) {}
105 Legendre2DPolynominal(const std::vector<double>& p)
106 : fOrder(p.size() - 1), fParams(p) {}
107 Legendre2DPolynominal(const TMatrixD& p) : fOrder(p.GetNrows() - 1)
108 {
109 fParams = std::vector<double>(NPar());
110 for (unsigned int iOrder{0}; iOrder <= fOrder; ++iOrder) {
111 for (unsigned int jOrder{0}; jOrder <= iOrder; ++jOrder) {
112 fParams[getFlatIdx(iOrder, jOrder)] = p(iOrder, jOrder);
113 }
114 }
115 }
116 ~Legendre2DPolynominal() final = default;
117
118 double operator()(const double* x) const
119 {
120 return DoEvalPar(x, Parameters());
121 }
122 double operator()(double x, double y) const { return DoEvalPar(x, y); }
123 double operator()(int i, int j, const double* x) const
124 {
125 return DoEvalParSingle(i, j, x, Parameters());
126 }
127 double operator()(int i, int j, double x, double y) const
128 {
129 return DoEvalParSingle(i, j, x, y, Parameters());
130 }
131
132 const double* Parameters() const final { return &fParams.front(); }
133
134 void SetParameters(const double* p) final
135 {
136 fParams = std::vector<double>(p, p + NPar());
137 }
138
139 unsigned int NPar() const final
140 {
141 return fOrder * (fOrder + 1) / 2 + fOrder + 1;
142 }
143 unsigned int NDim() const final { return 2; }
144 unsigned int NOrder() const { return fOrder; }
145
146 TMatrixD getCoefficients() const
147 {
148 TMatrixD m(fOrder + 1, fOrder + 1);
149 for (unsigned int iOrder{0}; iOrder <= fOrder; ++iOrder) {
150 for (unsigned int jOrder{0}; jOrder <= iOrder; ++jOrder) {
151 m(iOrder, jOrder) = fParams[getFlatIdx(iOrder, jOrder)];
152 }
153 }
154 return m;
155 }
156
157 void printCoefficients() const { getCoefficients().Print(); }
158
159 // Unimplemented
160 ROOT::Math::IBaseFunctionMultiDim* Clone() const final { return new Legendre2DPolynominal(fParams); }
161 TObject* Clone(const char* name) const final
162 {
163 auto n = new Legendre2DPolynominal(fParams);
164 n->SetName(name);
165 return n;
166 }
167
168 private:
169 double DoEvalPar(const double* x, const double* p) const final
170 {
171 double sum{0.0};
172 for (unsigned int iOrder{0}; iOrder <= fOrder; ++iOrder) {
173 for (unsigned int jOrder{0}; jOrder <= iOrder; ++jOrder) {
174 sum += DoEvalParSingle(iOrder, jOrder, x, p);
175 }
176 }
177 return sum;
178 }
179
180 double DoEvalPar(double x, double y) const
181 {
182 double sum{0.0};
183 for (unsigned int iOrder{0}; iOrder <= fOrder; ++iOrder) {
184 for (unsigned int jOrder{0}; jOrder <= iOrder; ++jOrder) {
185 sum += DoEvalParSingle(iOrder, jOrder, x, y, Parameters());
186 }
187 }
188 return sum;
189 }
190
191 double DoEvalParSingle(int i, int j, const double* x, const double* p) const
192 {
193 return DoEvalParSingle(i, j, x[0], x[1], p);
194 }
195
196 double DoEvalParSingle(int i, int j, double x, double y,
197 const double* p) const
198 {
199 return p[getFlatIdx(i, j)] * boost::math::legendre_p(j, x) *
200 boost::math::legendre_p(i - j, y);
201 }
202
203 inline int getFlatIdx(int i, int j) const { return i * (i + 1) / 2 + j; }
204
205 unsigned int fOrder{0};
206 std::vector<double> fParams;
207
208 ClassDefOverride(o2::math_utils::Legendre2DPolynominal, 1);
209};
210
211} // namespace o2::math_utils
212
213#endif
int32_t i
uint32_t j
Definition RawData.h:0
virtual void SetParameters(const double *p) final
unsigned int NPar() const final
double operator()(int i, double x) const
double operator()(double x) const
ROOT::Math::IBaseFunctionOneDim * Clone() const final
Legendre1DPolynominal(Legendre1DPolynominal &&)=delete
Legendre1DPolynominal(const Legendre1DPolynominal &)=default
TObject * Clone(const char *name) const final
Legendre1DPolynominal & operator=(Legendre1DPolynominal &&)=delete
Legendre1DPolynominal(const std::vector< double > p)
const double * Parameters() const final
Legendre1DPolynominal & operator=(const Legendre1DPolynominal &)=default
Legendre1DPolynominal(unsigned int order)
double operator()(double x, double y) const
void SetParameters(const double *p) final
unsigned int NDim() const final
Legendre2DPolynominal(const std::vector< double > &p)
unsigned int NPar() const final
double operator()(int i, int j, const double *x) const
double operator()(int i, int j, double x, double y) const
ROOT::Math::IBaseFunctionMultiDim * Clone() const final
Legendre2DPolynominal(unsigned int order)
TObject * Clone(const char *name) const final
const double * Parameters() const final
float sum(float s, o2::dcs::DataPointValue v)
Definition dcs-ccdb.cxx:39
GLdouble n
Definition glcorearb.h:1982
GLint GLenum GLint x
Definition glcorearb.h:403
const GLfloat * m
Definition glcorearb.h:4066
GLsizeiptr size
Definition glcorearb.h:659
GLuint const GLchar * name
Definition glcorearb.h:781