HArD::Core3D
Hybrid Arbitrary Degree::Core 3D - Library to implement 3D schemes with vertex, edge, face and cell polynomials as unknowns
Loading...
Searching...
No Matches
math.hpp
Go to the documentation of this file.
1#ifndef _MATH_HPP
2#define _MATH_HPP
3
4#include <cmath>
5namespace Math
6{
8
9 const double PI = acos(-1);
10
11 inline const size_t factorial(size_t n)
12 {
13 return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;
14 }
15
16 inline const size_t nChoosek(size_t n, size_t k)
17 {
18 if (k > n)
19 {
20 return 0;
21 }
22 if (k * 2 > n)
23 {
24 k = n - k;
25 }
26 if (k == 0)
27 {
28 return 1;
29 }
30
31 size_t result = n;
32 for (size_t i = 2; i <= k; ++i)
33 {
34 result *= (n - i + 1);
35 result /= i;
36 }
37 return result;
38 }
39
40 template <typename T>
41 const int sgn(T val)
42 {
43 return (T(0) < val) - (val < T(0));
44 }
45
46 // branch -1: -2\pi < \theta \le 0
47 // branch 0: -\pi < \theta \le \pi
48 // branch 1: 0 \le \theta < 2\pi
49 inline double atan2(double y, double x, int branch = 0)
50 {
51 double val = (((x != 0) || (y != 0)) ? std::atan2(y, x) : 0.0);
52 if (branch == 1)
53 {
54 if (y < 0)
55 {
56 val += 2.0 * PI;
57 }
58 }
59 if (branch == -1)
60 {
61 if (y > 0)
62 {
63 val -= 2.0 * PI;
64 }
65 else if ((y == 0) && (x < 0))
66 {
67 val -= 2.0 * PI;
68 }
69 }
70 return val;
71 }
72}
73
74#endif
Definition math.hpp:6
const int sgn(T val)
Definition math.hpp:41
double atan2(double y, double x, int branch=0)
Definition math.hpp:49
const size_t nChoosek(size_t n, size_t k)
Definition math.hpp:16
const double PI
Free math functions and global variables ///.
Definition math.hpp:9
const size_t factorial(size_t n)
Definition math.hpp:11