Skip to content

Commit

Permalink
Split up eigenvectors_test file
Browse files Browse the repository at this point in the history
  • Loading branch information
WardBrian committed Mar 9, 2023
1 parent 5ec8978 commit b2483c4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 57 deletions.
60 changes: 60 additions & 0 deletions test/unit/math/mix/fun/eigendecompose_identity_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <test/unit/math/test_ad.hpp>


template <typename T>
void expect_identity_matrix(const T& x) {
EXPECT_EQ(x.rows(), x.cols());
for (int j = 0; j < x.cols(); ++j) {
for (int i = 0; i < x.rows(); ++i) {
EXPECT_NEAR(i == j ? 1 : 0, stan::math::value_of_rec(x(i, j)), 1e-6);
}
}
}

template <typename T>
void expectEigenvectorsId() {
for (const auto& m_d : stan::test::square_test_matrices(1, 2)) {
Eigen::Matrix<T, -1, -1> m(m_d);
auto vecs = eigenvectors(m).eval();
auto vals = eigenvalues(m).eval();
auto I = (vecs.inverse() * m * vecs * vals.asDiagonal().inverse()).real();
expect_identity_matrix(I);
}
}

template <typename T>
void expectComplexEigenvectorsId() {
Eigen::Matrix<std::complex<T>, -1, -1> c22(2, 2);
c22 << stan::math::to_complex(T(0), T(-1)),
stan::math::to_complex(T(0), T(0)), stan::math::to_complex(T(2), T(0)),
stan::math::to_complex(T(4), T(0));
auto eigenvalues = stan::math::eigenvalues(c22);
auto eigenvectors = stan::math::eigenvectors(c22);

auto I = (eigenvectors.inverse() * c22 * eigenvectors
* eigenvalues.asDiagonal().inverse())
.real();

expect_identity_matrix(I);
}

TEST(mathMixFun, eigenvectorsId) {
using d_t = double;
using v_t = stan::math::var;
using fd_t = stan::math::fvar<double>;
using ffd_t = stan::math::fvar<fd_t>;
using fv_t = stan::math::fvar<stan::math::var>;
using ffv_t = stan::math::fvar<fv_t>;

expectEigenvectorsId<v_t>();
expectEigenvectorsId<fd_t>();
expectEigenvectorsId<ffd_t>();
expectEigenvectorsId<fv_t>();
expectEigenvectorsId<ffv_t>();

expectComplexEigenvectorsId<v_t>();
expectComplexEigenvectorsId<fd_t>();
expectComplexEigenvectorsId<ffd_t>();
expectComplexEigenvectorsId<fv_t>();
expectComplexEigenvectorsId<ffv_t>();
}
57 changes: 0 additions & 57 deletions test/unit/math/mix/fun/eigenvectors_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,60 +29,3 @@ TEST(mathMixFun, eigenvectorsComplex) {
EXPECT_THROW(f(a32), std::invalid_argument);
}

template <typename T>
void expect_identity_matrix(const T& x) {
EXPECT_EQ(x.rows(), x.cols());
for (int j = 0; j < x.cols(); ++j) {
for (int i = 0; i < x.rows(); ++i) {
EXPECT_NEAR(i == j ? 1 : 0, stan::math::value_of_rec(x(i, j)), 1e-6);
}
}
}

template <typename T>
void expectEigenvectorsId() {
for (const auto& m_d : stan::test::square_test_matrices(1, 2)) {
Eigen::Matrix<T, -1, -1> m(m_d);
auto vecs = eigenvectors(m).eval();
auto vals = eigenvalues(m).eval();
auto I = (vecs.inverse() * m * vecs * vals.asDiagonal().inverse()).real();
expect_identity_matrix(I);
}
}

template <typename T>
void expectComplexEigenvectorsId() {
Eigen::Matrix<std::complex<T>, -1, -1> c22(2, 2);
c22 << stan::math::to_complex(T(0), T(-1)),
stan::math::to_complex(T(0), T(0)), stan::math::to_complex(T(2), T(0)),
stan::math::to_complex(T(4), T(0));
auto eigenvalues = stan::math::eigenvalues(c22);
auto eigenvectors = stan::math::eigenvectors(c22);

auto I = (eigenvectors.inverse() * c22 * eigenvectors
* eigenvalues.asDiagonal().inverse())
.real();

expect_identity_matrix(I);
}

TEST(mathMixFun, eigenvectorsId) {
using d_t = double;
using v_t = stan::math::var;
using fd_t = stan::math::fvar<double>;
using ffd_t = stan::math::fvar<fd_t>;
using fv_t = stan::math::fvar<stan::math::var>;
using ffv_t = stan::math::fvar<fv_t>;

expectEigenvectorsId<v_t>();
expectEigenvectorsId<fd_t>();
expectEigenvectorsId<ffd_t>();
expectEigenvectorsId<fv_t>();
expectEigenvectorsId<ffv_t>();

expectComplexEigenvectorsId<v_t>();
expectComplexEigenvectorsId<fd_t>();
expectComplexEigenvectorsId<ffd_t>();
expectComplexEigenvectorsId<fv_t>();
expectComplexEigenvectorsId<ffv_t>();
}

0 comments on commit b2483c4

Please sign in to comment.