cMHN 1.2
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
sqrt.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_CORE_COMPLEX_FUNCTIONS_SQRT_H
4#define pRC_CORE_COMPLEX_FUNCTIONS_SQRT_H
5
7
8namespace pRC
9{
10 template<IsComplex T>
11 static inline constexpr auto sqrt(T const &a)
12 {
13 using R = typename T::Type;
14
15 if(a.real() == zero())
16 {
17 auto const t = sqrt(abs(a.imag()) / identity<R>(2));
18 if(a.imag() < zero())
19 {
20 return Complex(t, -t);
21 }
22 else
23 {
24 return Complex(t, t);
25 }
26 }
27 else
28 {
29 auto const t = sqrt(identity<R>(2) * (abs(a) + abs(a.real())));
30 auto const t2 = t / identity<R>(2);
31 auto const at = a.imag() / t;
32
33 if(a.real() > zero())
34 {
35 return Complex(t2, at);
36 }
37 else
38 {
39 auto const real = abs(at);
40 if(a.imag() < zero())
41 {
42 return Complex(real, -t2);
43 }
44 else
45 {
46 return Complex(real, t2);
47 }
48 }
49 }
50 }
51}
52#endif // pRC_CORE_COMPLEX_FUNCTIONS_SQRT_H
Definition value.hpp:12
Definition cholesky.hpp:10
static constexpr auto sqrt(T const &a)
Definition sqrt.hpp:11
Complex(T const &) -> Complex< T >
static constexpr auto abs(T const &a)
Definition abs.hpp:11
static constexpr decltype(auto) real(X &&a)
Definition real.hpp:12
static constexpr auto identity()
Definition identity.hpp:13
static constexpr auto zero()
Definition zero.hpp:12