Arcane  4.2.1.0
User documentation
Loading...
Searching...
No Matches
MathBase.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* MathBase.h (C) 2000-2026 */
9/* */
10/* Basic mathematical functions. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_BASE_MATHASE_H
13#define ARCCORE_BASE_MATHASE_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
18
19#include <cmath>
20
21#if defined(ARCANE_CHECK_MATH) || defined(ARCCORE_CHECK_MATH)
22#define ARCCORE_DO_CHECK_MATH
23#endif
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27/*!
28 * \brief Namespace for mathematical functions.
29 *
30 * This namespace contains all mathematical functions used
31 * by the code.
32 */
33namespace Arcane::math
34{
35
36/*---------------------------------------------------------------------------*/
37/*---------------------------------------------------------------------------*/
38/*!
39 * \brief Natural logarithm of \a v.
40 */
41ARCCORE_HOST_DEVICE inline double
42log(double v)
43{
44#ifdef ARCCORE_DO_CHECK_MATH
45 if (v == 0.0 || v < 0.0)
46 arcaneMathError(v, "log");
47#endif
48 return std::log(v);
49}
50
51/*!
52 * \brief Natural logarithm of \a v.
53 */
54ARCCORE_HOST_DEVICE inline long double
55log(long double v)
56{
57#ifdef ARCCORE_DO_CHECK_MATH
58 if (v == 0.0 || v < 0.0)
59 arcaneMathError(v, "log");
60#endif
61 return std::log(v);
62}
63
64/*---------------------------------------------------------------------------*/
65/*---------------------------------------------------------------------------*/
66
67/*!
68 * \brief Decimal logarithm of \a v.
69 */
70ARCCORE_HOST_DEVICE inline double
71log10(double v)
72{
73#ifdef ARCCORE_DO_CHECK_MATH
74 if (v == 0.0 || v < 0.0)
75 arcaneMathError(v, "log");
76#endif
77 return std::log10(v);
78}
79
80/*!
81 * \brief Decimal logarithm of \a v.
82 */
83ARCCORE_HOST_DEVICE inline long double
84log10(long double v)
85{
86#ifdef ARCCORE_DO_CHECK_MATH
87 if (v == 0.0 || v < 0.0)
88 arcaneMathError(v, "log");
89#endif
90 return std::log10(v);
91}
92
93/*---------------------------------------------------------------------------*/
94/*---------------------------------------------------------------------------*/
95
96/*!
97 * \brief Round \a v down to the immediately lower integer.
98 */
99ARCCORE_HOST_DEVICE inline double
100floor(double v)
101{
102 return std::floor(v);
103}
104
105/*!
106 * \brief Round \a v down to the immediately lower integer.
107 */
108ARCCORE_HOST_DEVICE inline long double
109floor(long double v)
110{
111 return std::floor(v);
112}
113
114/*---------------------------------------------------------------------------*/
115/*---------------------------------------------------------------------------*/
116
117/*!
118 * \brief Exponential of \a v.
119 */
120ARCCORE_HOST_DEVICE inline double
121exp(double v)
122{
123 return std::exp(v);
124}
125
126/*!
127 * \brief Exponential of \a v.
128 */
129ARCCORE_HOST_DEVICE inline long double
130exp(long double v)
131{
132 return std::exp(v);
133}
134
135/*---------------------------------------------------------------------------*/
136/*---------------------------------------------------------------------------*/
137
138/*!
139 * \brief Square root of \a v.
140 */
141ARCCORE_HOST_DEVICE inline double
142sqrt(double v)
143{
144#ifdef ARCCORE_DO_CHECK_MATH
145 if (v < 0.)
146 arcaneMathError(v, "sqrt");
147#endif
148 return std::sqrt(v);
149}
150
151/*!
152 * \brief Square root of \a v.
153 */
154ARCCORE_HOST_DEVICE inline long double
155sqrt(long double v)
156{
157#ifdef ARCCORE_DO_CHECK_MATH
158 if (v < 0.)
159 arcaneMathError(v, "sqrt");
160#endif
161 return std::sqrt(v);
162}
163
164/*---------------------------------------------------------------------------*/
165/*---------------------------------------------------------------------------*/
166
167/*!
168 * \brief Power function.
169 *
170 * Calculates \a x to the power of \a y.
171 *
172 * \pre x>=0 or y is an integer
173 */
174ARCCORE_HOST_DEVICE inline double
175pow(double x, double y)
176{
177#ifdef ARCCORE_DO_CHECK_MATH
178 // Invalid arguments if x is negative and y is not an integer
179 if (x < 0.0 && ::floor(y) != y)
180 arcaneMathError(x, y, "pow");
181#endif
182 return std::pow(x, y);
183}
184
185/*---------------------------------------------------------------------------*/
186/*---------------------------------------------------------------------------*/
187/*!
188 * \brief Power function.
189 *
190 * Calculates \a x to the power of \a y.
191 *
192 * \pre x>=0 or y is an integer
193 */
194ARCCORE_HOST_DEVICE inline long double
195pow(long double x, long double y)
196{
197#ifdef ARCCORE_DO_CHECK_MATH
198 // Invalid arguments if x is negative and y is not an integer
199 if (x < 0.0 && ::floorl(y) != y)
200 arcaneMathError(x, y, "pow");
201#endif
202 return std::pow(x, y);
203}
204
205/*!
206 * \brief Power function.
207 *
208 * Calculates \a x to the power of \a y.
209 *
210 * \pre x>=0 or y is an integer
211 */
212ARCCORE_HOST_DEVICE inline long double
213pow(double x, long double y)
214{
215#ifdef ARCCORE_DO_CHECK_MATH
216 // Invalid arguments if x is negative and y is not an integer
217 if (x < 0.0 && ::floorl(y) != y)
218 arcaneMathError(x, y, "pow");
219#endif
220 return std::pow(x, y);
221}
222
223/*!
224 * \brief Power function.
225 *
226 * Calculates \a x to the power of \a y.
227 *
228 * \pre x>=0 or y is an integer
229 */
230ARCCORE_HOST_DEVICE inline long double
231pow(long double x, double y)
232{
233#ifdef ARCCORE_DO_CHECK_MATH
234 // Invalid arguments if x is negative and y is not an integer
235 if (x < 0.0 && ::floor(y) != y)
236 arcaneMathError(x, y, "pow");
237#endif
238 return std::pow(x, y);
239}
240
241/*---------------------------------------------------------------------------*/
242/*---------------------------------------------------------------------------*/
243
244/*!
245 * \brief Returns the minimum of two elements.
246 *
247 * \ingroup GroupMathUtils
248 *
249 * Uses the < operator to determine the minimum.
250 */
251template <class T> ARCCORE_HOST_DEVICE inline T
252min(const T& a, const T& b)
253{
254 return ((a < b) ? a : b);
255}
256
257/*!
258 * \brief Returns the minimum of two real numbers.
259 * \ingroup GroupMathUtils
260 */
261ARCCORE_HOST_DEVICE inline long double
262min(long double a, long double b)
263{
264 return ((a < b) ? a : b);
265}
266
267/*!
268 * \brief Returns the minimum of two real numbers.
269 * \ingroup GroupMathUtils
270 */
271ARCCORE_HOST_DEVICE inline long double
272min(double a, long double b)
273{
274 return ((a < b) ? a : b);
275}
276
277/*!
278 * \brief Returns the minimum of two real numbers.
279 * \ingroup GroupMathUtils
280 */
281ARCCORE_HOST_DEVICE inline long double
282min(long double a, double b)
283{
284 return ((a < b) ? a : b);
285}
286
287/*!
288 * \brief Returns the minimum of two real numbers.
289 * \ingroup GroupMathUtils
290 */
291ARCCORE_HOST_DEVICE inline double
292min(double a, double b)
293{
294 return ((a < b) ? a : b);
295}
296
297/*!
298 * \brief Returns the minimum of two real numbers.
299 * \ingroup GroupMathUtils
300 */
301ARCCORE_HOST_DEVICE inline float
302min(float a, float b)
303{
304 return ((a < b) ? a : b);
305}
306
307/*!
308 * \brief Returns the minimum of two integers.
309 * \ingroup GroupMathUtils
310 */
311ARCCORE_HOST_DEVICE inline int
312min(int a, int b)
313{
314 return ((a < b) ? a : b);
315}
316
317/*---------------------------------------------------------------------------*/
318/*---------------------------------------------------------------------------*/
319
320/*!
321 * \brief Returns the maximum of two elements.
322 *
323 * \ingroup GroupMathUtils
324 *
325 * Uses the < operator to determine the maximum.
326 */
327template <class T> ARCCORE_HOST_DEVICE inline T
328max(const T& a, const T& b)
329{
330 return ((a < b) ? b : a);
331}
332
333/*!
334 * \brief Returns the maximum of two real numbers.
335 * \ingroup GroupMathUtils
336 */
337ARCCORE_HOST_DEVICE inline long double
338max(long double a, long double b)
339{
340 return ((a < b) ? b : a);
341}
342
343/*!
344 * \brief Returns the maximum of two real numbers.
345 * \ingroup GroupMathUtils
346 */
347ARCCORE_HOST_DEVICE inline long double
348max(double a, long double b)
349{
350 return ((a < b) ? b : a);
351}
352
353/*!
354 * \brief Returns the maximum of two real numbers.
355 * \ingroup GroupMathUtils
356 */
357ARCCORE_HOST_DEVICE inline long double
358max(long double a, double b)
359{
360 return ((a < b) ? b : a);
361}
362
363/*!
364 * \brief Returns the maximum of two integers.
365 * \ingroup GroupMathUtils
366 */
367ARCCORE_HOST_DEVICE inline unsigned long
368max(unsigned long a, unsigned long b)
369{
370 return ((a < b) ? b : a);
371}
372
373/*!
374 * \brief Returns the maximum of two real numbers.
375 * \ingroup GroupMathUtils
376 */
377ARCCORE_HOST_DEVICE inline double
378max(double a, double b)
379{
380 return ((a < b) ? b : a);
381}
382
383/*!
384 * \brief Returns the maximum of two real numbers.
385 * \ingroup GroupMathUtils
386 */
387ARCCORE_HOST_DEVICE inline float
388max(float a, float b)
389{
390 return ((a < b) ? b : a);
391}
392
393/*!
394 * \brief Returns the maximum of two Int16
395 * \ingroup GroupMathUtils
396 */
397ARCCORE_HOST_DEVICE inline Int16
399{
400 return ((a < b) ? b : a);
401}
402
403/*!
404 * \brief Returns the maximum of two Int32
405 * \ingroup GroupMathUtils
406 */
407ARCCORE_HOST_DEVICE inline Int32
409{
410 return ((a < b) ? b : a);
411}
412
413/*!
414 * \brief Returns the maximum of two Int32
415 * \ingroup GroupMathUtils
416 */
417ARCCORE_HOST_DEVICE inline Int64
419{
420 return ((a < b) ? b : a);
421}
422
423/*!
424 * \brief Returns the maximum of two Int64
425 * \ingroup GroupMathUtils
426 */
427ARCCORE_HOST_DEVICE inline Int64
429{
430 return ((a < b) ? b : a);
431}
432
433/*!
434 * \brief Returns the maximum of two Int64
435 * \ingroup GroupMathUtils
436 */
437ARCCORE_HOST_DEVICE inline Int64
439{
440 return ((a < b) ? b : a);
441}
442
443/*---------------------------------------------------------------------------*/
444/*---------------------------------------------------------------------------*/
445
446/*!
447 * \brief Returns the absolute value of a real number.
448 * \ingroup GroupMathUtils
449 */
450ARCCORE_HOST_DEVICE inline long double
451abs(long double a)
452{
453 return std::abs(a);
454}
455
456/*!
457 * \brief Returns the absolute value of a real number.
458 * \ingroup GroupMathUtils
459 */
460ARCCORE_HOST_DEVICE inline double
461abs(double a)
462{
463 return std::abs(a);
464}
465
466/*!
467 * \brief Returns the absolute value of a real number.
468 * \ingroup GroupMathUtils
469 */
470ARCCORE_HOST_DEVICE inline float
471abs(float a)
472{
473 return std::abs(a);
474}
475
476/*!
477 * \brief Returns the absolute value of an 'int'.
478 * \ingroup GroupMathUtils
479 */
480ARCCORE_HOST_DEVICE inline short
481abs(short a)
482{
483 return (a > 0) ? a : (short)(-a);
484}
485
486/*!
487 * \brief Returns the absolute value of an 'int'.
488 * \ingroup GroupMathUtils
489 */
490ARCCORE_HOST_DEVICE inline int
491abs(int a)
492{
493 return (a > 0) ? a : (-a);
494}
495
496/*!
497 * \brief Returns the absolute value of a 'long'.
498 * \ingroup GroupMathUtils
499 */
500ARCCORE_HOST_DEVICE inline long
501abs(long a)
502{
503 return (a > 0L) ? a : (-a);
504}
505
506/*!
507 * \brief Returns the absolute value of a 'long'.
508 * \ingroup GroupMathUtils
509 */
510ARCCORE_HOST_DEVICE inline long long
511abs(long long a)
512{
513 return (a > 0LL) ? a : (-a);
514}
515
516/*---------------------------------------------------------------------------*/
517/*---------------------------------------------------------------------------*/
518
519} // End namespace Arcane::math
520
521/*---------------------------------------------------------------------------*/
522/*---------------------------------------------------------------------------*/
523
524#endif
Definitions and globals of Arccore.
__host__ __device__ Real2 min(Real2 a, Real2 b)
Returns the minimum of two Real2.
Definition MathUtils.h:346
T max(const T &a, const T &b, const T &c)
Returns the maximum of three elements.
Definition MathUtils.h:407
Namespace for mathematical functions.
Definition MathUtils.h:36
apfloat pow(apfloat x, apfloat y)
Power function.
Definition MathApfloat.h:89
apfloat exp(apfloat v)
Exponential of v.
Definition MathApfloat.h:57
apfloat floor(apfloat v)
Round v down to the immediately lower integer.
Definition MathApfloat.h:45
apfloat log(apfloat v)
Natural logarithm of v.
Definition MathApfloat.h:32
apfloat sqrt(apfloat v)
Square root of v.
Definition MathApfloat.h:69
std::int64_t Int64
Signed integer type of 64 bits.
std::int16_t Int16
Signed integer type of 16 bits.
__host__ __device__ void arcaneMathError(long double arg_value, const char *func_name)
Signals an invalid argument in a mathematical function.
std::int32_t Int32
Signed integer type of 32 bits.