52 template<
typename dtype>
63 auto b = inA.template astype<double>();
64 auto eigenVectors = eye<double>(n);
67 constexpr auto MAX_ITERATIONS = 10000;
68 for (
auto iter = 0u; iter < MAX_ITERATIONS; ++iter)
70 auto max_off_diag = 0.;
74 for (
auto i = 0u; i < n; i++)
76 for (
auto j = i + 1;
j < n;
j++)
78 const auto val = std::fabs(b(i,
j));
79 if (val > max_off_diag)
88 if (max_off_diag < inTolerance)
93 const auto app = b(p, p);
94 const auto aqq = b(q, q);
95 const auto apq = b(p, q);
97 const auto theta = (aqq - app) / (2. * apq);
99 const auto t = (theta >= 0.) ? 1. / (theta + onePlusThetaSqr) : 1. / (theta - onePlusThetaSqr);
101 const auto s = t *
c;
103 for (
auto i = 0u; i < n; ++i)
105 if (i != p && i != q)
107 const auto bip = b(i, p);
108 const auto biq = b(i, q);
109 b(i, p) =
c * bip - s * biq;
111 b(i, q) = s * bip +
c * biq;
116 b(p, p) =
c *
c * app + s * s * aqq - 2. *
c * s * apq;
117 b(q, q) = s * s * app +
c *
c * aqq + 2. *
c * s * apq;
121 for (
auto i = 0u; i < n; ++i)
123 const auto vip = eigenVectors(i, p);
124 const auto viq = eigenVectors(i, q);
125 eigenVectors(i, p) =
c * vip - s * viq;
126 eigenVectors(i, q) = s * vip +
c * viq;
130 for (
auto i = 0u; i < n; ++i)
132 eigenVals[i] = b(i, i);
135 for (
auto i = 0u; i < n - 1; ++i)
137 for (
auto j = i + 1;
j < n; ++
j)
139 if (eigenVals[i] < eigenVals[
j])
143 for (
auto k = 0u; k < n; ++k)
145 std::swap(eigenVectors(k, i), eigenVectors(k,
j));
151 return std::make_pair(eigenVals, eigenVectors);
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:39
bool issquare() const noexcept
Definition: NdArrayCore.hpp:3089
size_type numRows() const noexcept
Definition: NdArrayCore.hpp:3557
constexpr auto j
Definition: Core/Constants.hpp:42
constexpr double c
speed of light
Definition: Core/Constants.hpp:36
constexpr double e
eulers number
Definition: Core/Constants.hpp:37
Definition: cholesky.hpp:41
std::pair< NdArray< double >, NdArray< double > > eig(const NdArray< dtype > &inA, double inTolerance=1e-12)
Definition: eig.hpp:53
constexpr dtype sqr(dtype inValue) noexcept
Definition: sqr.hpp:42
void swap(NdArray< dtype > &inArray1, NdArray< dtype > &inArray2) noexcept
Definition: swap.hpp:42
auto sqrt(dtype inValue) noexcept
Definition: sqrt.hpp:48