diagonal.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
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   //check diagonal()
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     // check sub/super diagonal
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 }


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:31:01