xerus
a general purpose tensor library
math.h
Go to the documentation of this file.
1 // Xerus - A General Purpose Tensor Library
2 // Copyright (C) 2014-2017 Benjamin Huber and Sebastian Wolf.
3 //
4 // Xerus is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published
6 // by the Free Software Foundation, either version 3 of the License,
7 // or (at your option) any later version.
8 //
9 // Xerus is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with Xerus. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // For further information on Xerus visit https://libXerus.org
18 // or contact us at contact@libXerus.org.
19 
25 #pragma once
26 
27 #include <limits>
28 #include <cmath>
29 #include "standard.h"
30 #include "sfinae.h"
31 
32 namespace xerus {
33  namespace misc {
35  template<class T>
36  constexpr int sgn(const T _value) noexcept {
37  return (T(0) < _value) - (_value < T(0));
38  }
39 
40 
42  template<class T>
43  constexpr T sqr(const T &_a) noexcept {
44  return _a*_a;
45  }
46 
47 
49  template<class T>
50  constexpr T pow(const T& _base, const uint32 _exp) noexcept {
51  return _exp==0 ? T(1) : (_exp%2==0 ? pow(T(_base*_base), _exp/2) : T(_base*pow(_base, _exp-1)));
52  }
53 
55  template<class T>
56  constexpr T pow(const T &_base, const uint64 _exp) noexcept {
57  return _exp==0 ? T(1) : (_exp%2==0 ? pow(T(_base*_base), _exp/2) : T(_base*pow(_base, _exp-1)));
58  }
59 
60 
62  template<class T>
63  constexpr T pow(const T &_base, const int64 _exp) noexcept {
64  return _exp==0 ?
65  T(1) :
66  (
67  _exp<0 ?
68  T( 1/pow(_base, -_exp))
69  :
70  ( _exp%2==0 ? pow(T(_base*_base), _exp/2) : T(_base*pow(_base, _exp-1)) )
71  );
72  }
73 
74 
76  template<class T>
77  constexpr T pow(const T &_base, const int32 _exp) noexcept {
78  return _exp==0 ?
79  T(1) :
80  (
81  _exp<0 ?
82  T( 1/pow(_base, -_exp))
83  :
84  ( _exp%2==0 ? pow(T(_base*_base), _exp/2) : T(_base*pow(_base, _exp-1)) )
85  );
86  }
87 
88 
90  template<class T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
91  bool approx_equal(T _a, T _b, T _eps = 4*std::numeric_limits<T>::epsilon()) noexcept {
92  return std::abs(_a-_b) <= _eps*0.5*(std::abs(_a)+std::abs(_b));
93  }
94 
95 
97  template<class T, typename std::enable_if<!std::is_floating_point<T>::value, bool>::type = true>
98  bool approx_equal(T _a, T _b) {
99  return _a == _b;
100  }
101 
102 
104  template<class T>
105  constexpr bool hard_equal(const T _a, const T _b) noexcept {
106  #pragma GCC diagnostic push
107  #pragma GCC diagnostic ignored "-Wfloat-equal"
108  return _a == _b;
109  #pragma GCC diagnostic pop
110  }
111 
112 
114  template<class T>
115  constexpr bool hard_not_equal(const T _a, const T _b) noexcept {
116  #pragma GCC diagnostic push
117  #pragma GCC diagnostic ignored "-Wfloat-equal"
118  return _a != _b;
119  #pragma GCC diagnostic pop
120  }
121  }
122 }
uint64_t uint64
Definition: standard.h:63
bool approx_equal(T _a, T _b, T _eps=4 *std::numeric_limits< T >::epsilon()) noexcept
: Checks whether the relative difference between _a and _b (i.e. |a-b|/(|a|/2+|b|/2)) is smaller than...
Definition: math.h:91
The main namespace of xerus.
Definition: basic.h:37
constexpr bool hard_not_equal(const T _a, const T _b) noexcept
: Checks for hard inequality ( != operator) without compiler warnings.
Definition: math.h:115
int64_t int64
Definition: standard.h:58
Header file for macros that encapsulate SFINAE functionality.
uint32_t uint32
Definition: standard.h:62
constexpr T sqr(const T &_a) noexcept
: Calculates _a*_a.
Definition: math.h:43
constexpr int sgn(const T _value) noexcept
: Calculates the signum (-1, 0, 1) of a given value.
Definition: math.h:36
int32_t int32
Definition: standard.h:57
constexpr bool hard_equal(const T _a, const T _b) noexcept
: Checks for hard equality ( == operator) without compiler warnings.
Definition: math.h:105
constexpr T pow(const T &_base, const uint32 _exp) noexcept
: Calculates _base^_exp by binary exponentiation
Definition: math.h:50
Header file for global shorthand notations of elementary integer types and attribute lists...