9 #include <boost/variant.hpp>
11 #include <boost/test/unit_test.hpp>
12 #include <boost/utility/binary.hpp>
14 BOOST_AUTO_TEST_SUITE(BOOST_TEST_MODULE)
20 const Eigen::DenseIndex mat_size = 20;
24 BOOST_CHECK(tridiagonal_matrix.
isZero(0));
26 BOOST_CHECK(tridiagonal_matrix.
matrix().isZero(0));
31 const Eigen::DenseIndex mat_size = 20;
39 BOOST_CHECK(tridiagonal_matrix.
rows() == mat_size);
40 BOOST_CHECK(tridiagonal_matrix.
cols() == mat_size);
41 BOOST_CHECK(tridiagonal_matrix.
diagonal().size() == mat_size);
42 BOOST_CHECK(tridiagonal_matrix.
subDiagonal().size() == mat_size - 1);
44 BOOST_CHECK(tridiagonal_matrix.
diagonal().isOnes(0));
45 BOOST_CHECK(tridiagonal_matrix.
subDiagonal().isZero(0));
46 BOOST_CHECK(tridiagonal_matrix.
matrix().isIdentity(0));
50 PlainMatrixType
mat(mat_size, mat_size);
51 mat = tridiagonal_matrix;
53 BOOST_CHECK(
mat.isIdentity(0));
57 for (
int k = 0; k < 1000; ++k)
59 PlainMatrixType
mat = PlainMatrixType::Random(mat_size, mat_size);
61 PlainMatrixType
plain(mat_size, mat_size);
62 plain = tridiagonal_matrix;
64 PlainMatrixType res_apply_on_the_right = tridiagonal_matrix *
mat;
65 PlainMatrixType res_apply_on_the_right_ref =
plain *
mat;
66 BOOST_CHECK(res_apply_on_the_right.isApprox(res_apply_on_the_right_ref));
68 PlainMatrixType res_apply_on_the_left =
mat * tridiagonal_matrix;
69 PlainMatrixType res_apply_on_the_left_ref =
mat *
plain;
70 BOOST_CHECK(res_apply_on_the_left.isApprox(res_apply_on_the_left_ref));
76 const Eigen::DenseIndex mat_size = 20;
82 BOOST_CHECK(tridiagonal_matrix.
rows() == mat_size);
83 BOOST_CHECK(tridiagonal_matrix.
cols() == mat_size);
84 BOOST_CHECK(tridiagonal_matrix.
diagonal().size() == mat_size);
85 BOOST_CHECK(tridiagonal_matrix.
subDiagonal().size() == mat_size - 1);
89 PlainMatrixType
mat(mat_size, mat_size);
90 mat = tridiagonal_matrix;
92 BOOST_CHECK(
mat.diagonal() == tridiagonal_matrix.
diagonal());
93 BOOST_CHECK(
mat.diagonal<-1>() == tridiagonal_matrix.
subDiagonal());
94 BOOST_CHECK(
mat.diagonal<+1>().conjugate() == tridiagonal_matrix.
subDiagonal().conjugate());
98 for (
int k = 0; k < 1000; ++k)
100 PlainMatrixType rhs_mat = PlainMatrixType::Random(mat_size, mat_size);
102 PlainMatrixType
plain(mat_size, mat_size);
103 plain = tridiagonal_matrix;
105 PlainMatrixType
res = tridiagonal_matrix * rhs_mat;
107 PlainMatrixType res_ref =
plain * rhs_mat;
108 BOOST_CHECK(
res.isApprox(res_ref));
109 BOOST_CHECK((tridiagonal_matrix * PlainMatrixType::Identity(mat_size, mat_size))
117 const Eigen::DenseIndex mat_size = 10;
123 PlainMatrixType plain_mat(mat_size, mat_size);
124 plain_mat = tridiagonal_matrix;
125 const PlainMatrixType plain_mat_inverse = plain_mat.
inverse();
128 tridiagonal_matrix_inverse = tridiagonal_matrix.
inverse();
130 BOOST_CHECK(tridiagonal_matrix_inverse.
rows() == tridiagonal_matrix.
rows());
131 BOOST_CHECK(tridiagonal_matrix_inverse.
cols() == tridiagonal_matrix.
cols());
133 const auto & tridiagonal_matrix_ref = tridiagonal_matrix_inverse.
inverse();
134 BOOST_CHECK(&tridiagonal_matrix_ref == &tridiagonal_matrix);
136 const PlainMatrixType tridiagonal_matrix_inverse_plain = tridiagonal_matrix_inverse;
137 BOOST_CHECK(tridiagonal_matrix_inverse_plain.isApprox(plain_mat_inverse));
140 for (
int k = 0; k < 1; ++k)
142 PlainMatrixType rhs_mat = PlainMatrixType::Random(mat_size, mat_size);
144 PlainMatrixType
res(mat_size, mat_size);
145 res = tridiagonal_matrix_ref * rhs_mat;
147 BOOST_CHECK(
res.isApprox((plain_mat * rhs_mat).eval()));
149 res = tridiagonal_matrix_inverse * rhs_mat;
150 const PlainMatrixType res_ref = plain_mat_inverse * rhs_mat;
152 BOOST_CHECK(
res.isApprox(res_ref));
157 Eigen::MatrixXd sub_diagonal_matrix = Eigen::MatrixXd::Zero(mat_size, mat_size);
158 sub_diagonal_matrix.diagonal<1>().setRandom();
159 sub_diagonal_matrix.diagonal().setRandom();
160 sub_diagonal_matrix.diagonal<-1>().setRandom();
162 const Eigen::MatrixXd test_mat = Eigen::MatrixXd::Random(mat_size, mat_size);
163 const Eigen::MatrixXd res_ref = sub_diagonal_matrix * test_mat;
165 Eigen::MatrixXd
res(mat_size, mat_size);
166 res.noalias() = sub_diagonal_matrix.diagonal().asDiagonal() * test_mat;
167 res.topRows(mat_size - 1) +=
168 sub_diagonal_matrix.diagonal<1>().asDiagonal() * test_mat.bottomRows(mat_size - 1);
169 res.bottomRows(mat_size - 1) +=
170 sub_diagonal_matrix.diagonal<-1>().asDiagonal() * test_mat.topRows(mat_size - 1);
171 BOOST_CHECK(
res.isApprox(res_ref));
175 BOOST_AUTO_TEST_SUITE_END()