algebra/include/nil/crypto3/algebra/scalar/math.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2020-2021 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2020-2021 Nikita Kaskov <nbering@nil.foundation>
4 // Copyright (c) 2020-2021 Ilias Khairullin <ilias@nil.foundation>
5 //
6 // MIT License
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 //---------------------------------------------------------------------------//
26 
27 #ifndef CRYPTO3_ALGEBRA_SCALAR_MATH_HPP
28 #define CRYPTO3_ALGEBRA_SCALAR_MATH_HPP
29 
32 
33 namespace nil {
34  namespace crypto3 {
35  namespace algebra {
36 
47  constexpr double sqrt(double x) {
48  if (x < 0)
49  throw "sqrt argument must be positive";
50  double prev = 0;
51  double est = (1 + x) / 2;
52  while (prev != est) {
53  prev = est;
54  est = (est + x / est) / 2;
55  }
56  return est;
57  }
58 
65  constexpr float sqrt(float x) {
66  return sqrt(double(x));
67  }
68 
75  template<typename T>
77  // CRYPTO3_DETAIL_ASSERT_ARITHMETIC(T);
78  if constexpr (algebra::is_complex_v<T>)
79  return sqrt(x.real() * x.real() + x.imag() * x.imag());
80  else
81  return x > 0 ? x : -x;
82  }
83 
91  constexpr double exponentiate(double x, int n) {
92  if (n == 0)
93  return 1;
94  if (n < 0) {
95  x = 1. / x;
96  n = -n;
97  }
98  double y = 1.;
99  while (n > 1) {
100  if (n % 2 == 0) {
101  n = n / 2.;
102  } else {
103  y *= x;
104  n = (n - 1.) / 2.;
105  }
106  x *= x;
107  }
108  return x * y;
109  }
110 
118  constexpr double nthroot(double x, int n) {
119  if (x < 0)
120  throw "nth root argument must be positive";
121  double prev = -1;
122  double est = 1;
123  while (prev != est) {
124  prev = est;
125  double dxk = 1. / n * (x / exponentiate(prev, n - 1) - prev);
126  est = prev + dxk;
127  }
128  return est;
129  }
130 
137  template<typename T>
138  constexpr T conj(T x) {
139  // CRYPTO3_DETAIL_ASSERT_ARITHMETIC(T);
140  if constexpr (algebra::is_complex_v<T>)
141  return {x.real(), -x.imag()};
142  else
143  return x;
144  }
145 
146  } // namespace algebra
147  } // namespace crypto3
148 } // namespace nil
149 
150 #endif // CRYPTO3_ALGEBRA_SCALAR_MATH_HPP
constexpr matrix< T, M, N > conj(const matrix< T, M, N > &m)
computes the elementwise complex conjugate
Definition: algebra/include/nil/crypto3/algebra/matrix/math.hpp:57
constexpr nil::crypto3::detail::remove_complex_t< T > abs(T x)
computes the absolute value
Definition: algebra/include/nil/crypto3/algebra/scalar/math.hpp:76
constexpr double exponentiate(double x, int n)
computes exponents
Definition: algebra/include/nil/crypto3/algebra/scalar/math.hpp:91
constexpr double nthroot(double x, int n)
computes the th root
Definition: algebra/include/nil/crypto3/algebra/scalar/math.hpp:118
constexpr double sqrt(double x)
computes the square root
Definition: algebra/include/nil/crypto3/algebra/scalar/math.hpp:47
typename remove_complex< T >::type remove_complex_t
Definition: hash/include/nil/crypto3/detail/type_traits.hpp:404
Definition: pair.hpp:31