@@ -15,19 +15,33 @@ TEST(mathMixFun, eigenvectors) {
1515 EXPECT_THROW (f (a32), std::invalid_argument);
1616}
1717
18+ TEST (mathMixFun, eigenvectorsComplex) {
19+ auto f = [](const auto & x) {
20+ using stan::math::eigenvectors;
21+ return eigenvectors (stan::math::to_complex (x, 0 ));
22+ };
23+ for (const auto & x : stan::test::square_test_matrices (0 , 2 )) {
24+ stan::test::expect_ad (f, x);
25+ }
26+
27+ Eigen::MatrixXd a32 (3 , 2 );
28+ a32 << 3 , -5 , 7 , -7.2 , 9.1 , -6.3 ;
29+ EXPECT_THROW (f (a32), std::invalid_argument);
30+ }
31+
1832template <typename T>
1933void expect_identity_matrix (const T& x) {
20- EXPECT_EQUAL (x.rows (), x.cols ());
34+ EXPECT_EQ (x.rows (), x.cols ());
2135 for (int j = 0 ; j < x.cols (); ++j) {
2236 for (int i = 0 ; i < x.rows (); ++i) {
23- EXPECT_NEAR (i == j ? 1 : 0 , x (i, j), 1e-6 );
37+ EXPECT_NEAR (i == j ? 1 : 0 , stan::math::value_of_rec ( x (i, j) ), 1e-6 );
2438 }
2539 }
2640}
2741
2842template <typename T>
2943void expectEigenvectorsId () {
30- for (const auto & m_d : stan::test::square_test_matrices (0 , 2 )) {
44+ for (const auto & m_d : stan::test::square_test_matrices (1 , 2 )) {
3145 Eigen::Matrix<T, -1 , -1 > m (m_d);
3246 auto vecs = eigenvectors (m).eval ();
3347 auto vals = eigenvalues (m).eval ();
@@ -36,7 +50,21 @@ void expectEigenvectorsId() {
3650 }
3751}
3852
39- // THESE TESTS USED TO WORK STANDALONE
53+ template <typename T>
54+ void expectComplexEigenvectorsId () {
55+ Eigen::Matrix<std::complex <T>, -1 , -1 > c22 (2 , 2 );
56+ c22 << stan::math::to_complex (T (0 ), T (-1 )),
57+ stan::math::to_complex (T (0 ), T (0 )), stan::math::to_complex (T (2 ), T (0 )),
58+ stan::math::to_complex (T (4 ), T (0 ));
59+ auto eigenvalues = stan::math::eigenvalues (c22);
60+ auto eigenvectors = stan::math::eigenvectors (c22);
61+
62+ auto I = (eigenvectors.inverse () * c22 * eigenvectors
63+ * eigenvalues.asDiagonal ().inverse ())
64+ .real ();
65+
66+ expect_identity_matrix (I);
67+ }
4068
4169TEST (mathMixFun, eigenvectorsId) {
4270 using d_t = double ;
@@ -46,9 +74,15 @@ TEST(mathMixFun, eigenvectorsId) {
4674 using fv_t = stan::math::fvar<stan::math::var>;
4775 using ffv_t = stan::math::fvar<fv_t >;
4876
49- // expectEigenvectorsId<v_t>();
50- // expectEigenvectorsId<fd_t>();
51- // expectEigenvectorsId<ffd_t>();
52- // expectEigenvectorsId<fv_t>();
53- // expectEigenvectorsId<ffv_t>();
77+ expectEigenvectorsId<v_t >();
78+ expectEigenvectorsId<fd_t >();
79+ expectEigenvectorsId<ffd_t >();
80+ expectEigenvectorsId<fv_t >();
81+ expectEigenvectorsId<ffv_t >();
82+
83+ expectComplexEigenvectorsId<v_t >();
84+ expectComplexEigenvectorsId<fd_t >();
85+ expectComplexEigenvectorsId<ffd_t >();
86+ expectComplexEigenvectorsId<fv_t >();
87+ expectComplexEigenvectorsId<ffv_t >();
5488}
0 commit comments