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 using namespace std;
00027 template<typename MatrixType> void diagonalmatrices(const MatrixType& m)
00028 {
00029 typedef typename MatrixType::Index Index;
00030 typedef typename MatrixType::Scalar Scalar;
00031 typedef typename MatrixType::RealScalar RealScalar;
00032 enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime };
00033 typedef Matrix<Scalar, Rows, 1> VectorType;
00034 typedef Matrix<Scalar, 1, Cols> RowVectorType;
00035 typedef Matrix<Scalar, Rows, Rows> SquareMatrixType;
00036 typedef DiagonalMatrix<Scalar, Rows> LeftDiagonalMatrix;
00037 typedef DiagonalMatrix<Scalar, Cols> RightDiagonalMatrix;
00038 typedef Matrix<Scalar, Rows==Dynamic?Dynamic:2*Rows, Cols==Dynamic?Dynamic:2*Cols> BigMatrix;
00039 Index rows = m.rows();
00040 Index cols = m.cols();
00041
00042 MatrixType m1 = MatrixType::Random(rows, cols),
00043 m2 = MatrixType::Random(rows, cols);
00044 VectorType v1 = VectorType::Random(rows),
00045 v2 = VectorType::Random(rows);
00046 RowVectorType rv1 = RowVectorType::Random(cols),
00047 rv2 = RowVectorType::Random(cols);
00048 LeftDiagonalMatrix ldm1(v1), ldm2(v2);
00049 RightDiagonalMatrix rdm1(rv1), rdm2(rv2);
00050
00051 SquareMatrixType sq_m1 (v1.asDiagonal());
00052 VERIFY_IS_APPROX(sq_m1, v1.asDiagonal().toDenseMatrix());
00053 sq_m1 = v1.asDiagonal();
00054 VERIFY_IS_APPROX(sq_m1, v1.asDiagonal().toDenseMatrix());
00055 SquareMatrixType sq_m2 = v1.asDiagonal();
00056 VERIFY_IS_APPROX(sq_m1, sq_m2);
00057
00058 ldm1 = v1.asDiagonal();
00059 LeftDiagonalMatrix ldm3(v1);
00060 VERIFY_IS_APPROX(ldm1.diagonal(), ldm3.diagonal());
00061 LeftDiagonalMatrix ldm4 = v1.asDiagonal();
00062 VERIFY_IS_APPROX(ldm1.diagonal(), ldm4.diagonal());
00063
00064 sq_m1.block(0,0,rows,rows) = ldm1;
00065 VERIFY_IS_APPROX(sq_m1, ldm1.toDenseMatrix());
00066 sq_m1.transpose() = ldm1;
00067 VERIFY_IS_APPROX(sq_m1, ldm1.toDenseMatrix());
00068
00069 Index i = internal::random<Index>(0, rows-1);
00070 Index j = internal::random<Index>(0, cols-1);
00071
00072 VERIFY_IS_APPROX( ((ldm1 * m1)(i,j)) , ldm1.diagonal()(i) * m1(i,j) );
00073 VERIFY_IS_APPROX( ((ldm1 * (m1+m2))(i,j)) , ldm1.diagonal()(i) * (m1+m2)(i,j) );
00074 VERIFY_IS_APPROX( ((m1 * rdm1)(i,j)) , rdm1.diagonal()(j) * m1(i,j) );
00075 VERIFY_IS_APPROX( ((v1.asDiagonal() * m1)(i,j)) , v1(i) * m1(i,j) );
00076 VERIFY_IS_APPROX( ((m1 * rv1.asDiagonal())(i,j)) , rv1(j) * m1(i,j) );
00077 VERIFY_IS_APPROX( (((v1+v2).asDiagonal() * m1)(i,j)) , (v1+v2)(i) * m1(i,j) );
00078 VERIFY_IS_APPROX( (((v1+v2).asDiagonal() * (m1+m2))(i,j)) , (v1+v2)(i) * (m1+m2)(i,j) );
00079 VERIFY_IS_APPROX( ((m1 * (rv1+rv2).asDiagonal())(i,j)) , (rv1+rv2)(j) * m1(i,j) );
00080 VERIFY_IS_APPROX( (((m1+m2) * (rv1+rv2).asDiagonal())(i,j)) , (rv1+rv2)(j) * (m1+m2)(i,j) );
00081
00082 BigMatrix big;
00083 big.setZero(2*rows, 2*cols);
00084
00085 big.block(i,j,rows,cols) = m1;
00086 big.block(i,j,rows,cols) = v1.asDiagonal() * big.block(i,j,rows,cols);
00087
00088 VERIFY_IS_APPROX((big.block(i,j,rows,cols)) , v1.asDiagonal() * m1 );
00089
00090 big.block(i,j,rows,cols) = m1;
00091 big.block(i,j,rows,cols) = big.block(i,j,rows,cols) * rv1.asDiagonal();
00092 VERIFY_IS_APPROX((big.block(i,j,rows,cols)) , m1 * rv1.asDiagonal() );
00093
00094 }
00095
00096 void test_diagonalmatrices()
00097 {
00098 for(int i = 0; i < g_repeat; i++) {
00099 CALL_SUBTEST_1( diagonalmatrices(Matrix<float, 1, 1>()) );
00100 CALL_SUBTEST_2( diagonalmatrices(Matrix3f()) );
00101 CALL_SUBTEST_3( diagonalmatrices(Matrix<double,3,3,RowMajor>()) );
00102 CALL_SUBTEST_4( diagonalmatrices(Matrix4d()) );
00103 CALL_SUBTEST_5( diagonalmatrices(Matrix<float,4,4,RowMajor>()) );
00104 CALL_SUBTEST_6( diagonalmatrices(MatrixXcf(3, 5)) );
00105 CALL_SUBTEST_7( diagonalmatrices(MatrixXi(10, 8)) );
00106 CALL_SUBTEST_8( diagonalmatrices(Matrix<double,Dynamic,Dynamic,RowMajor>(20, 20)) );
00107 CALL_SUBTEST_9( diagonalmatrices(MatrixXf(21, 24)) );
00108 }
00109 }