eigen2_triangular.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra. Eigen itself is part of the KDE project.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@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 triangular(const MatrixType& m)
00028 {
00029   typedef typename MatrixType::Scalar Scalar;
00030   typedef typename NumTraits<Scalar>::Real RealScalar;
00031   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
00032 
00033   RealScalar largerEps = 10*test_precision<RealScalar>();
00034 
00035   int rows = m.rows();
00036   int cols = m.cols();
00037 
00038   MatrixType m1 = MatrixType::Random(rows, cols),
00039              m2 = MatrixType::Random(rows, cols),
00040              m3(rows, cols),
00041              m4(rows, cols),
00042              r1(rows, cols),
00043              r2(rows, cols),
00044              mzero = MatrixType::Zero(rows, cols),
00045              mones = MatrixType::Ones(rows, cols),
00046              identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
00047                               ::Identity(rows, rows),
00048              square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
00049                               ::Random(rows, rows);
00050   VectorType v1 = VectorType::Random(rows),
00051              v2 = VectorType::Random(rows),
00052              vzero = VectorType::Zero(rows);
00053 
00054   MatrixType m1up = m1.template part<Eigen::UpperTriangular>();
00055   MatrixType m2up = m2.template part<Eigen::UpperTriangular>();
00056 
00057   if (rows*cols>1)
00058   {
00059     VERIFY(m1up.isUpperTriangular());
00060     VERIFY(m2up.transpose().isLowerTriangular());
00061     VERIFY(!m2.isLowerTriangular());
00062   }
00063 
00064 //   VERIFY_IS_APPROX(m1up.transpose() * m2, m1.upper().transpose().lower() * m2);
00065 
00066   // test overloaded operator+=
00067   r1.setZero();
00068   r2.setZero();
00069   r1.template part<Eigen::UpperTriangular>() +=  m1;
00070   r2 += m1up;
00071   VERIFY_IS_APPROX(r1,r2);
00072 
00073   // test overloaded operator=
00074   m1.setZero();
00075   m1.template part<Eigen::UpperTriangular>() = (m2.transpose() * m2).lazy();
00076   m3 = m2.transpose() * m2;
00077   VERIFY_IS_APPROX(m3.template part<Eigen::LowerTriangular>().transpose(), m1);
00078 
00079   // test overloaded operator=
00080   m1.setZero();
00081   m1.template part<Eigen::LowerTriangular>() = (m2.transpose() * m2).lazy();
00082   VERIFY_IS_APPROX(m3.template part<Eigen::LowerTriangular>(), m1);
00083 
00084   VERIFY_IS_APPROX(m3.template part<Diagonal>(), m3.diagonal().asDiagonal());
00085 
00086   m1 = MatrixType::Random(rows, cols);
00087   for (int i=0; i<rows; ++i)
00088     while (ei_abs2(m1(i,i))<1e-3) m1(i,i) = ei_random<Scalar>();
00089 
00090   Transpose<MatrixType> trm4(m4);
00091   // test back and forward subsitution
00092   m3 = m1.template part<Eigen::LowerTriangular>();
00093   VERIFY(m3.template marked<Eigen::LowerTriangular>().solveTriangular(m3).cwise().abs().isIdentity(test_precision<RealScalar>()));
00094   VERIFY(m3.transpose().template marked<Eigen::UpperTriangular>()
00095     .solveTriangular(m3.transpose()).cwise().abs().isIdentity(test_precision<RealScalar>()));
00096   // check M * inv(L) using in place API
00097   m4 = m3;
00098   m3.transpose().template marked<Eigen::UpperTriangular>().solveTriangularInPlace(trm4);
00099   VERIFY(m4.cwise().abs().isIdentity(test_precision<RealScalar>()));
00100 
00101   m3 = m1.template part<Eigen::UpperTriangular>();
00102   VERIFY(m3.template marked<Eigen::UpperTriangular>().solveTriangular(m3).cwise().abs().isIdentity(test_precision<RealScalar>()));
00103   VERIFY(m3.transpose().template marked<Eigen::LowerTriangular>()
00104     .solveTriangular(m3.transpose()).cwise().abs().isIdentity(test_precision<RealScalar>()));
00105   // check M * inv(U) using in place API
00106   m4 = m3;
00107   m3.transpose().template marked<Eigen::LowerTriangular>().solveTriangularInPlace(trm4);
00108   VERIFY(m4.cwise().abs().isIdentity(test_precision<RealScalar>()));
00109 
00110   m3 = m1.template part<Eigen::UpperTriangular>();
00111   VERIFY(m2.isApprox(m3 * (m3.template marked<Eigen::UpperTriangular>().solveTriangular(m2)), largerEps));
00112   m3 = m1.template part<Eigen::LowerTriangular>();
00113   VERIFY(m2.isApprox(m3 * (m3.template marked<Eigen::LowerTriangular>().solveTriangular(m2)), largerEps));
00114 
00115   VERIFY((m1.template part<Eigen::UpperTriangular>() * m2.template part<Eigen::UpperTriangular>()).isUpperTriangular());
00116 
00117   // test swap
00118   m1.setOnes();
00119   m2.setZero();
00120   m2.template part<Eigen::UpperTriangular>().swap(m1);
00121   m3.setZero();
00122   m3.template part<Eigen::UpperTriangular>().setOnes();
00123   VERIFY_IS_APPROX(m2,m3);
00124 
00125 }
00126 
00127 void selfadjoint()
00128 {
00129   Matrix2i m;
00130   m << 1, 2,
00131        3, 4;
00132 
00133   Matrix2i m1 = Matrix2i::Zero();
00134   m1.part<SelfAdjoint>() = m;
00135   Matrix2i ref1;
00136   ref1 << 1, 2,
00137           2, 4;
00138   VERIFY(m1 == ref1);
00139   
00140   Matrix2i m2 = Matrix2i::Zero();
00141   m2.part<SelfAdjoint>() = m.part<UpperTriangular>();
00142   Matrix2i ref2;
00143   ref2 << 1, 2,
00144           2, 4;
00145   VERIFY(m2 == ref2);
00146  
00147   Matrix2i m3 = Matrix2i::Zero();
00148   m3.part<SelfAdjoint>() = m.part<LowerTriangular>();
00149   Matrix2i ref3;
00150   ref3 << 1, 0,
00151           0, 4;
00152   VERIFY(m3 == ref3);
00153   
00154   // example inspired from bug 159
00155   int array[] = {1, 2, 3, 4};
00156   Matrix2i::Map(array).part<SelfAdjoint>() = Matrix2i::Random().part<LowerTriangular>();
00157   
00158   std::cout << "hello\n" << array << std::endl;
00159 }
00160 
00161 void test_eigen2_triangular()
00162 {
00163   CALL_SUBTEST_8( selfadjoint() );
00164   for(int i = 0; i < g_repeat ; i++) {
00165     CALL_SUBTEST_1( triangular(Matrix<float, 1, 1>()) );
00166     CALL_SUBTEST_2( triangular(Matrix<float, 2, 2>()) );
00167     CALL_SUBTEST_3( triangular(Matrix3d()) );
00168     CALL_SUBTEST_4( triangular(MatrixXcf(4, 4)) );
00169     CALL_SUBTEST_5( triangular(Matrix<std::complex<float>,8, 8>()) );
00170     CALL_SUBTEST_6( triangular(MatrixXd(17,17)) );
00171     CALL_SUBTEST_7( triangular(Matrix<float,Dynamic,Dynamic,RowMajor>(5, 5)) );
00172   }
00173 }


libicr
Author(s): Robert Krug
autogenerated on Mon Jan 6 2014 11:32:39