Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "main.h"
00026
00027 template<typename MatrixType> void diagonal(const MatrixType& m)
00028 {
00029 typedef typename MatrixType::Index Index;
00030 typedef typename MatrixType::Scalar Scalar;
00031 typedef typename MatrixType::RealScalar RealScalar;
00032 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
00033 typedef Matrix<Scalar, 1, MatrixType::ColsAtCompileTime> RowVectorType;
00034
00035 Index rows = m.rows();
00036 Index cols = m.cols();
00037
00038 MatrixType m1 = MatrixType::Random(rows, cols),
00039 m2 = MatrixType::Random(rows, cols);
00040
00041
00042 VERIFY_IS_APPROX(m1.diagonal(), m1.transpose().diagonal());
00043 m2.diagonal() = 2 * m1.diagonal();
00044 m2.diagonal()[0] *= 3;
00045
00046 if (rows>2)
00047 {
00048 enum {
00049 N1 = MatrixType::RowsAtCompileTime>1 ? 1 : 0,
00050 N2 = MatrixType::RowsAtCompileTime>2 ? -2 : 0
00051 };
00052
00053
00054 m2.template diagonal<N1>() = 2 * m1.template diagonal<N1>();
00055 m2.template diagonal<N1>()[0] *= 3;
00056 VERIFY_IS_APPROX(m2.template diagonal<N1>()[0], static_cast<Scalar>(6) * m1.template diagonal<N1>()[0]);
00057
00058 m2.template diagonal<N2>() = 2 * m1.template diagonal<N2>();
00059 m2.template diagonal<N2>()[0] *= 3;
00060 VERIFY_IS_APPROX(m2.template diagonal<N2>()[0], static_cast<Scalar>(6) * m1.template diagonal<N2>()[0]);
00061
00062 m2.diagonal(N1) = 2 * m1.diagonal(N1);
00063 m2.diagonal(N1)[0] *= 3;
00064 VERIFY_IS_APPROX(m2.diagonal(N1)[0], static_cast<Scalar>(6) * m1.diagonal(N1)[0]);
00065
00066 m2.diagonal(N2) = 2 * m1.diagonal(N2);
00067 m2.diagonal(N2)[0] *= 3;
00068 VERIFY_IS_APPROX(m2.diagonal(N2)[0], static_cast<Scalar>(6) * m1.diagonal(N2)[0]);
00069 }
00070 }
00071
00072 void test_diagonal()
00073 {
00074 for(int i = 0; i < g_repeat; i++) {
00075 CALL_SUBTEST_1( diagonal(Matrix<float, 1, 1>()) );
00076 CALL_SUBTEST_2( diagonal(Matrix4d()) );
00077 CALL_SUBTEST_2( diagonal(MatrixXcf(3, 3)) );
00078 CALL_SUBTEST_2( diagonal(MatrixXi(8, 12)) );
00079 CALL_SUBTEST_2( diagonal(MatrixXcd(20, 20)) );
00080 CALL_SUBTEST_1( diagonal(MatrixXf(21, 19)) );
00081 CALL_SUBTEST_1( diagonal(Matrix<float,Dynamic,4>(3, 4)) );
00082 }
00083 }