Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
MathHeaders.h
Go to the documentation of this file.
1/*
2 * Project: Math
3 * Authors:
4 * Monica Dessole, CERN, 2024
5 *
6 * Copyright (c) 2024, CERN
7 *
8 * Redistribution and use in source and binary forms,
9 * with or without modification, are permitted according to the terms
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11 */
12
13#ifndef ROOT_MathHeaders_H
14#define ROOT_MathHeaders_H
15
17
18#include <limits>
19
20namespace ROOT {
21
22namespace ROOT_MATH_ARCH {
23
24#if defined(ROOT_MATH_SYCL)
25template <class Scalar>
26inline Scalar math_fmod(Scalar x, Scalar y)
27{
28 return sycl::fmod(x, y);
29}
30
31template <class Scalar>
32inline Scalar math_sin(Scalar x)
33{
34 return sycl::sin(x);
35}
36
37template <class Scalar>
38inline Scalar math_cos(Scalar x)
39{
40 return sycl::cos(x);
41}
42
43template <class Scalar>
44inline Scalar math_asin(Scalar x)
45{
46 return sycl::asin(x);
47}
48
49template <class Scalar>
50inline Scalar math_acos(Scalar x)
51{
52 return sycl::acos(x);
53}
54
55template <class Scalar>
56inline Scalar math_sinh(Scalar x)
57{
58 return sycl::sinh(x);
59}
60
61template <class Scalar>
62inline Scalar math_cosh(Scalar x)
63{
64 return sycl::cosh(x);
65}
66
67template <class Scalar>
68inline Scalar math_atan2(Scalar x, Scalar y)
69{
70 return sycl::atan2(x, y);
71}
72
73template <class Scalar>
74inline Scalar math_atan(Scalar x)
75{
76 return sycl::atan(x);
77}
78
79template <class Scalar>
80inline Scalar math_sqrt(Scalar x)
81{
82 return sycl::sqrt(x);
83}
84
85template <class Scalar>
86inline Scalar math_floor(Scalar x)
87{
88 return sycl::floor(x);
89}
90
91template <class Scalar>
92inline Scalar math_exp(Scalar x)
93{
94 return sycl::exp(x);
95}
96
97template <class Scalar>
98inline Scalar math_log(Scalar x)
99{
100 return sycl::log(x);
101}
102
103inline long double math_log(long double x)
104{
105 double castx = x;
106 double castres = sycl::log(castx);
107 return (long double)castres;
108}
109
110template <class Scalar>
111inline Scalar math_tan(Scalar x)
112{
113 return sycl::tan(x);
114}
115
116template <class Scalar>
117inline Scalar math_fabs(Scalar x)
118{
119 return sycl::fabs(x);
120}
121
122template <class Scalar>
123inline Scalar math_pow(Scalar x, Scalar y)
124{
125 return sycl::pow(x, y);
126}
127
128// template <class T>
129// T etaMax2()
130// {
131// return static_cast<T>(22756.0);
132// }
133
134// template <typename Scalar>
135// inline Scalar Eta_FromRhoZ(Scalar rho, Scalar z)
136// {
137// if (rho > 0) {
138// // value to control Taylor expansion of sqrt
139// // static const Scalar
140// Scalar epsilon = static_cast<Scalar>(2e-16);
141// const Scalar big_z_scaled = sycl::pow(epsilon, static_cast<Scalar>(-.25));
142
143// Scalar z_scaled = z / rho;
144// if (sycl::fabs(z_scaled) < big_z_scaled) {
145// return sycl::log(z_scaled + sycl::sqrt(z_scaled * z_scaled + 1.0));
146// } else {
147// // apply correction using first order Taylor expansion of sqrt
148// return z > 0 ? sycl::log(2.0 * z_scaled + 0.5 / z_scaled) : -sycl::log(-2.0 * z_scaled);
149// }
150// return z_scaled;
151// }
152// // case vector has rho = 0
153// else if (z == 0) {
154// return 0;
155// } else if (z > 0) {
156// return z + etaMax2<Scalar>();
157// } else {
158// return z - etaMax2<Scalar>();
159// }
160// }
161
162// /**
163// Implementation of eta from -log(tan(theta/2)).
164// This is convenient when theta is already known (for example in a polar
165// coorindate system)
166// */
167// template <typename Scalar>
168// inline Scalar Eta_FromTheta(Scalar theta, Scalar r)
169// {
170// Scalar tanThetaOver2 = tan(theta / 2.);
171// if (tanThetaOver2 == 0) {
172// return r + etaMax2<Scalar>();
173// } else if (tanThetaOver2 > std::numeric_limits<Scalar>::max()) {
174// return -r - etaMax2<Scalar>();
175// } else {
176// return -log(tanThetaOver2);
177// }
178// }
179
180#elif defined(ROOT_MATH_CUDA)
181template <class Scalar>
182__roohost__ __roodevice__ inline Scalar math_fmod(Scalar x, Scalar y)
183{
184 return std::fmod(x, y);
185}
186
187template <class Scalar>
188__roohost__ __roodevice__ inline Scalar math_sin(Scalar x)
189{
190 return std::sin(x);
191}
192
193template <class Scalar>
194__roohost__ __roodevice__ inline Scalar math_cos(Scalar x)
195{
196 return std::cos(x);
197}
198
199template <class Scalar>
200__roohost__ __roodevice__ inline Scalar math_asin(Scalar x)
201{
202 return std::asin(x);
203}
204
205template <class Scalar>
206__roohost__ __roodevice__ inline Scalar math_acos(Scalar x)
207{
208 return std::acos(x);
209}
210
211template <class Scalar>
212__roohost__ __roodevice__ inline Scalar math_sinh(Scalar x)
213{
214 return std::sinh(x);
215}
216
217template <class Scalar>
218__roohost__ __roodevice__ inline Scalar math_cosh(Scalar x)
219{
220 return std::cosh(x);
221}
222
223template <class Scalar>
224__roohost__ __roodevice__ inline Scalar math_atan2(Scalar x, Scalar y)
225{
226 return std::atan2(x, y);
227}
228
229template <class Scalar>
230__roohost__ __roodevice__ inline Scalar math_atan(Scalar x)
231{
232 return std::atan(x);
233}
234
235template <class Scalar>
236__roohost__ __roodevice__ inline Scalar math_sqrt(Scalar x)
237{
238 return std::sqrt(x);
239}
240
241template <class Scalar>
242__roohost__ __roodevice__ inline Scalar math_floor(Scalar x)
243{
244 return std::floor(x);
245}
246
247template <class Scalar>
248__roohost__ __roodevice__ inline Scalar math_exp(Scalar x)
249{
250 return std::exp(x);
251}
252
253template <class Scalar>
254__roohost__ __roodevice__ inline Scalar math_log(Scalar x)
255{
256 return std::log(x);
257}
258
259template <class Scalar>
260__roohost__ __roodevice__ inline Scalar math_tan(Scalar x)
261{
262 return std::tan(x);
263}
264
265template <class Scalar>
266__roohost__ __roodevice__ inline Scalar math_fabs(Scalar x)
267{
268 return std::fabs(x);
269}
270
271template <class Scalar>
272__roohost__ __roodevice__ inline Scalar math_pow(Scalar x, Scalar y)
273{
274 return std::pow(x, y);
275}
276
277template <class T>
279{
280 return static_cast<T>(22756.0);
281}
282
283template <typename Scalar>
284__roohost__ __roodevice__ inline Scalar Eta_FromRhoZ(Scalar rho, Scalar z)
285{
286 if (rho > 0) {
287 // value to control Taylor expansion of sqrt
288 // static const Scalar
289 Scalar epsilon = static_cast<Scalar>(2e-16);
290 const Scalar big_z_scaled = pow(epsilon, static_cast<Scalar>(-.25));
291
292 Scalar z_scaled = z / rho;
293 if (fabs(z_scaled) < big_z_scaled) {
294 return log(z_scaled + sqrt(z_scaled * z_scaled + 1.0));
295 } else {
296 // apply correction using first order Taylor expansion of sqrt
297 return z > 0 ? log(2.0 * z_scaled + 0.5 / z_scaled) : log(-2.0 * z_scaled);
298 }
299 return z_scaled;
300 }
301 // case vector has rho = 0
302 else if (z == 0) {
303 return 0;
304 } else if (z > 0) {
305 return z + etaMax2<Scalar>();
306 } else {
307 return z - etaMax2<Scalar>();
308 }
309}
310
311/**
312 Implementation of eta from -log(tan(theta/2)).
313 This is convenient when theta is already known (for example in a polar
314 coorindate system)
315*/
316template <typename Scalar>
317__roohost__ __roodevice__ inline Scalar Eta_FromTheta(Scalar theta, Scalar r)
318{
319 Scalar tanThetaOver2 = tan(theta / 2.);
320 if (tanThetaOver2 == 0) {
321 return r + etaMax2<Scalar>();
322 } else if (tanThetaOver2 > std::numeric_limits<Scalar>::max()) {
323 return -r - etaMax2<Scalar>();
324 } else {
325 return -log(tanThetaOver2);
326 }
327}
328
329#else
330
331template <class Scalar>
333{
334 return std::fmod(x, y);
335}
336
337template <class Scalar>
339{
340 return std::sin(x);
341}
342
343template <class Scalar>
345{
346 return std::cos(x);
347}
348
349template <class Scalar>
351{
352 return std::asin(x);
353}
354
355template <class Scalar>
357{
358 return std::acos(x);
359}
360
361template <class Scalar>
363{
364 return std::sinh(x);
365}
366
367template <class Scalar>
369{
370 return std::cosh(x);
371}
372
373template <class Scalar>
375{
376 return std::atan2(x, y);
377}
378
379template <class Scalar>
381{
382 return std::atan(x);
383}
384
385template <class Scalar>
387{
388 return std::sqrt(x);
389}
390
391template <class Scalar>
393{
394 return std::floor(x);
395}
396
397template <class Scalar>
399{
400 return std::exp(x);
401}
402
403template <class Scalar>
405{
406 return std::log(x);
407}
408
409template <class Scalar>
411{
412 return std::tan(x);
413}
414
415template <class Scalar>
417{
418 return std::fabs(x);
419}
420
421template <class Scalar>
423{
424 return std::pow(x, y);
425}
426
427template <class T>
428inline T etaMax2()
429{
430 return static_cast<T>(22756.0);
431}
432
433template <typename Scalar>
435{
436 if (rho > 0) {
437
438 // value to control Taylor expansion of sqrt
439 static const Scalar big_z_scaled = pow(std::numeric_limits<Scalar>::epsilon(), static_cast<Scalar>(-.25));
440
441 Scalar z_scaled = z / rho;
442 if (fabs(z_scaled) < big_z_scaled) {
443 return log(z_scaled + sqrt(z_scaled * z_scaled + 1.0));
444 } else {
445 // apply correction using first order Taylor expansion of sqrt
446 return z > 0 ? log(2.0 * z_scaled + 0.5 / z_scaled) : -log(-2.0 * z_scaled);
447 }
448 }
449 // case vector has rho = 0
450 else if (z == 0) {
451 return 0;
452 } else if (z > 0) {
453 return z + etaMax2<Scalar>();
454 } else {
455 return z - etaMax2<Scalar>();
456 }
457}
458
459/**
460 Implementation of eta from -log(tan(theta/2)).
461 This is convenient when theta is already known (for example in a polar
462 coorindate system)
463*/
464template <typename Scalar>
466{
467 Scalar tanThetaOver2 = tan(theta / 2.);
468 if (tanThetaOver2 == 0) {
469 return r + etaMax2<Scalar>();
470 } else if (tanThetaOver2 > std::numeric_limits<Scalar>::max()) {
471 return -r - etaMax2<Scalar>();
472 } else {
473 return -log(tanThetaOver2);
474 }
475}
476
477#endif
478
479} // namespace ROOT_MATH_ARCH
480
481} // end namespace ROOT
482
483#endif
#define __roohost__
Definition AccHeaders.h:36
#define __roodevice__
Definition AccHeaders.h:35
#define e(i)
Definition RSha256.hxx:103
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
Scalar Eta_FromRhoZ(Scalar rho, Scalar z)
Scalar math_log(Scalar x)
Scalar math_floor(Scalar x)
Scalar math_pow(Scalar x, Scalar y)
Scalar math_cos(Scalar x)
Rotation3D::Scalar Scalar
Scalar math_atan(Scalar x)
Scalar math_sqrt(Scalar x)
Scalar math_tan(Scalar x)
Scalar math_sinh(Scalar x)
Scalar math_asin(Scalar x)
Scalar math_acos(Scalar x)
Scalar math_exp(Scalar x)
Scalar Eta_FromTheta(Scalar theta, Scalar r)
Implementation of eta from -log(tan(theta/2)).
Scalar math_atan2(Scalar x, Scalar y)
Scalar math_fabs(Scalar x)
Scalar math_cosh(Scalar x)
Scalar math_fmod(Scalar x, Scalar y)
Scalar math_sin(Scalar x)