DeSiGNAR  0.5a
Data Structures General Library
math.H
Go to the documentation of this file.
1 /*
2  This file is part of Designar.
3  Copyright (C) 2017 by Alejandro J. Mujica
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Any user request of this software, write to
19 
20  Alejandro Mujica
21 
22  aledrums@gmail.com
23 */
24 
25 # ifndef DSGMATH_H
26 # define DSGMATH_H
27 
28 # include <types.H>
29 
30 namespace Designar
31 {
32 
33  constexpr real_t PI = 3.1415926535897932384626433832795028841971693993751;
34  constexpr real_t PI_2 = PI/2.;
35  constexpr real_t PI_4 = PI/4.;
36  constexpr real_t PI3_2 = 3.*PI/2.;
37  constexpr real_t EPSILON = 1e-9;
38  constexpr real_t INF = std::numeric_limits<real_t>::infinity();
39 
40  enum class Sign { NEGATIVE, POSITIVE, ZERO };
41 
42  template <typename T>
43  Sign sign(T);
44 
45  template <typename T>
46  bool is_positive(T);
47 
48  template <typename T>
49  bool is_negative(T);
50 
51  template <typename T>
52  T abs(T);
53 
54  template <typename T>
55  bool real_equal(T, T);
56 
57  template <typename T>
58  bool num_equal(T, T);
59 
60  template <>
61  bool num_equal<float>(float, float);
62  template <>
63 
64  bool num_equal<double>(double, double);
65 
66  template <>
67  bool num_equal<long double>(long double, long double);
68 
69  template <class NumberType> class GenPoint2D;
70 
71  template <typename NumberType>
73  const GenPoint2D<NumberType> &,
74  const GenPoint2D<NumberType> &);
75 
76  template <typename BT, typename ET>
77  BT fast_integral_pow(BT, ET);
78 
79  template <typename BT, typename ET>
80  BT pow(BT, ET);
81 
82  template <typename T>
83  T abs(T x)
84  {
86  "Template argument must be a floating point type");
87  return (x < T(0) ? -x : x);
88  }
89 
90  template <typename T>
91  bool real_equal(T a, T b)
92  {
94  "Template argument must be a floating point type");
95  return abs(a-b) <= EPSILON;
96  }
97 
98  template <typename T>
99  bool num_equal(T a, T b)
100  {
101  static_assert(std::is_integral<T>::value,
102  "Template argument must be an integral type");
103  return a == b;
104  }
105 
106  template <typename T>
107  Sign sign(T n)
108  {
109  if (n < T(0))
110  return Sign::NEGATIVE;
111  else if (n > T(0))
112  return Sign::POSITIVE;
113  return Sign::ZERO;
114  }
115 
116  template <typename T>
117  bool is_positive(T n)
118  {
119  return sign(n) == Sign::POSITIVE;
120  }
121 
122  template <typename T>
123  bool is_negative(T n)
124  {
125  return sign(n) == Sign::NEGATIVE;
126  }
127 
128  template <typename NumberType>
130  const GenPoint2D<NumberType> & b,
131  const GenPoint2D<NumberType> & c)
132  {
133  return (b.get_x() - a.get_x()) * (c.get_y() - a.get_y()) -
134  (c.get_x() - a.get_x()) * (b.get_y() - a.get_y());
135  }
136 
137  template <typename BT, typename ET>
138  BT fast_integral_pow(BT base, ET exp)
139  {
141  "Arguments must be integral types");
142  static_assert(std::is_unsigned<ET>::value,
143  "Exponent must be an unsigned type");
144 
145  BT ret_val = BT(1);
146 
147  while (exp > ET(0))
148  {
149  if (exp & ET(1))
150  ret_val *= base;
151 
152  exp >>= 1;
153  base *= base;
154  }
155 
156  return ret_val;
157  }
158 
159  template <typename BT, typename ET>
161  {
162  BT operator () (BT base, ET exp)
163  {
164  return fast_integral_pow(base, exp);
165  }
166  };
167 
168  template <typename BT, typename ET>
169  struct StdPow
170  {
171  BT operator () (BT base, ET exp)
172  {
173  return std::pow(base, exp);
174  }
175  };
176 
177  template <typename BT, typename ET>
178  BT pow(BT base, ET exp)
179  {
180  auto fct = typename std::conditional<std::is_integral<BT>::value and
184  StdPow<BT, ET>>::type();
185  return fct(base, exp);
186  }
187 
188 } // end namespace Designar
189 
190 # endif // DSGMATH_H
constexpr real_t PI_4
Definition: math.H:35
bool num_equal< long double >(long double, long double)
double real_t
Definition: types.H:51
bool num_equal(T, T)
Definition: math.H:99
BT pow(BT, ET)
Definition: math.H:178
Definition: math.H:160
constexpr real_t PI_2
Definition: math.H:34
bool real_equal(T, T)
Definition: math.H:91
constexpr real_t EPSILON
Definition: math.H:37
Sign sign(T)
Definition: math.H:107
bool is_negative(T)
Definition: math.H:123
constexpr real_t INF
Definition: math.H:38
const NumT & get_x() const
Definition: point2D.H:113
Definition: math.H:69
Sign
Definition: math.H:40
bool num_equal< float >(float, float)
bool num_equal< double >(double, double)
bool is_positive(T)
Definition: math.H:117
Definition: math.H:169
Definition: array.H:32
Value & value(MapKey< Key, Value > &item)
Definition: map.H:77
BT fast_integral_pow(BT, ET)
Definition: math.H:138
T abs(T)
Definition: math.H:83
const NumT & get_y() const
Definition: point2D.H:118
constexpr real_t PI
Definition: math.H:33
constexpr real_t PI3_2
Definition: math.H:36
real_t area_of_parallelogram(const GenPoint2D< NumberType > &, const GenPoint2D< NumberType > &, const GenPoint2D< NumberType > &)
Definition: math.H:129