2009-08-03 22:06:57 +08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
// for linear algebra.
|
|
|
|
//
|
2010-01-11 21:48:39 +08:00
|
|
|
// Copyright (C) 2009-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
|
2009-08-03 22:06:57 +08:00
|
|
|
//
|
|
|
|
// Eigen is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 3 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Alternatively, you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of
|
|
|
|
// the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License and a copy of the GNU General Public License along with
|
|
|
|
// Eigen. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
#include "main.h"
|
2010-01-11 21:48:39 +08:00
|
|
|
#include <Eigen/QR>
|
2009-08-03 22:06:57 +08:00
|
|
|
|
|
|
|
template<typename MatrixType> void householder(const MatrixType& m)
|
|
|
|
{
|
2010-06-20 23:37:56 +08:00
|
|
|
typedef typename MatrixType::Index Index;
|
2009-08-17 23:04:32 +08:00
|
|
|
static bool even = true;
|
|
|
|
even = !even;
|
2009-08-03 22:06:57 +08:00
|
|
|
/* this test covers the following files:
|
|
|
|
Householder.h
|
|
|
|
*/
|
2010-06-20 23:37:56 +08:00
|
|
|
Index rows = m.rows();
|
|
|
|
Index cols = m.cols();
|
2009-08-03 22:06:57 +08:00
|
|
|
|
|
|
|
typedef typename MatrixType::Scalar Scalar;
|
|
|
|
typedef typename NumTraits<Scalar>::Real RealScalar;
|
|
|
|
typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
|
|
|
|
typedef Matrix<Scalar, ei_decrement_size<MatrixType::RowsAtCompileTime>::ret, 1> EssentialVectorType;
|
|
|
|
typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType;
|
2010-01-11 21:48:39 +08:00
|
|
|
typedef Matrix<Scalar, Dynamic, MatrixType::ColsAtCompileTime> HBlockMatrixType;
|
|
|
|
typedef Matrix<Scalar, Dynamic, 1> HCoeffsVectorType;
|
2009-08-17 23:04:32 +08:00
|
|
|
|
2010-01-11 21:48:39 +08:00
|
|
|
typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, MatrixType::ColsAtCompileTime> RightSquareMatrixType;
|
|
|
|
typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, Dynamic> VBlockMatrixType;
|
|
|
|
typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, MatrixType::RowsAtCompileTime> TMatrixType;
|
|
|
|
|
2010-06-11 19:56:50 +08:00
|
|
|
Matrix<Scalar, EIGEN_SIZE_MAX(MatrixType::RowsAtCompileTime,MatrixType::ColsAtCompileTime), 1> _tmp(std::max(rows,cols));
|
2009-08-17 23:04:32 +08:00
|
|
|
Scalar* tmp = &_tmp.coeffRef(0,0);
|
2009-09-16 20:35:42 +08:00
|
|
|
|
2009-08-17 23:04:32 +08:00
|
|
|
Scalar beta;
|
|
|
|
RealScalar alpha;
|
2009-08-03 22:06:57 +08:00
|
|
|
EssentialVectorType essential;
|
|
|
|
|
|
|
|
VectorType v1 = VectorType::Random(rows), v2;
|
|
|
|
v2 = v1;
|
2009-11-11 10:22:20 +08:00
|
|
|
v1.makeHouseholder(essential, beta, alpha);
|
2009-08-17 23:04:32 +08:00
|
|
|
v1.applyHouseholderOnTheLeft(essential,beta,tmp);
|
2009-08-03 22:06:57 +08:00
|
|
|
VERIFY_IS_APPROX(v1.norm(), v2.norm());
|
2010-04-17 00:32:33 +08:00
|
|
|
if(rows>=2) VERIFY_IS_MUCH_SMALLER_THAN(v1.tail(rows-1).norm(), v1.norm());
|
2009-08-03 22:06:57 +08:00
|
|
|
v1 = VectorType::Random(rows);
|
|
|
|
v2 = v1;
|
2009-08-17 23:04:32 +08:00
|
|
|
v1.applyHouseholderOnTheLeft(essential,beta,tmp);
|
2009-08-03 22:06:57 +08:00
|
|
|
VERIFY_IS_APPROX(v1.norm(), v2.norm());
|
2009-09-16 20:35:42 +08:00
|
|
|
|
2009-08-03 22:06:57 +08:00
|
|
|
MatrixType m1(rows, cols),
|
|
|
|
m2(rows, cols);
|
|
|
|
|
|
|
|
v1 = VectorType::Random(rows);
|
2010-01-05 10:24:43 +08:00
|
|
|
if(even) v1.tail(rows-1).setZero();
|
2009-08-03 22:06:57 +08:00
|
|
|
m1.colwise() = v1;
|
|
|
|
m2 = m1;
|
2009-11-11 10:22:20 +08:00
|
|
|
m1.col(0).makeHouseholder(essential, beta, alpha);
|
2009-08-17 23:04:32 +08:00
|
|
|
m1.applyHouseholderOnTheLeft(essential,beta,tmp);
|
2009-08-03 22:06:57 +08:00
|
|
|
VERIFY_IS_APPROX(m1.norm(), m2.norm());
|
2010-04-17 00:32:33 +08:00
|
|
|
if(rows>=2) VERIFY_IS_MUCH_SMALLER_THAN(m1.block(1,0,rows-1,cols).norm(), m1.norm());
|
2009-08-17 23:04:32 +08:00
|
|
|
VERIFY_IS_MUCH_SMALLER_THAN(ei_imag(m1(0,0)), ei_real(m1(0,0)));
|
|
|
|
VERIFY_IS_APPROX(ei_real(m1(0,0)), alpha);
|
2009-09-16 20:35:42 +08:00
|
|
|
|
2009-08-03 22:06:57 +08:00
|
|
|
v1 = VectorType::Random(rows);
|
2010-01-05 10:24:43 +08:00
|
|
|
if(even) v1.tail(rows-1).setZero();
|
2009-08-03 22:06:57 +08:00
|
|
|
SquareMatrixType m3(rows,rows), m4(rows,rows);
|
|
|
|
m3.rowwise() = v1.transpose();
|
|
|
|
m4 = m3;
|
2009-11-11 10:22:20 +08:00
|
|
|
m3.row(0).makeHouseholder(essential, beta, alpha);
|
2010-01-08 11:03:58 +08:00
|
|
|
m3.applyHouseholderOnTheRight(essential,beta,tmp);
|
2009-08-03 22:06:57 +08:00
|
|
|
VERIFY_IS_APPROX(m3.norm(), m4.norm());
|
2010-04-17 00:32:33 +08:00
|
|
|
if(rows>=2) VERIFY_IS_MUCH_SMALLER_THAN(m3.block(0,1,rows,rows-1).norm(), m3.norm());
|
2009-08-17 23:04:32 +08:00
|
|
|
VERIFY_IS_MUCH_SMALLER_THAN(ei_imag(m3(0,0)), ei_real(m3(0,0)));
|
|
|
|
VERIFY_IS_APPROX(ei_real(m3(0,0)), alpha);
|
2009-09-16 20:35:42 +08:00
|
|
|
|
2010-01-11 21:48:39 +08:00
|
|
|
// test householder sequence on the left with a shift
|
|
|
|
|
2010-06-21 00:59:15 +08:00
|
|
|
Index shift = ei_random<Index>(0, std::max<Index>(rows-2,0));
|
|
|
|
Index brows = rows - shift;
|
2010-01-11 21:48:39 +08:00
|
|
|
m1.setRandom(rows, cols);
|
|
|
|
HBlockMatrixType hbm = m1.block(shift,0,brows,cols);
|
|
|
|
HouseholderQR<HBlockMatrixType> qr(hbm);
|
|
|
|
m2 = m1;
|
|
|
|
m2.block(shift,0,brows,cols) = qr.matrixQR();
|
|
|
|
HCoeffsVectorType hc = qr.hCoeffs().conjugate();
|
|
|
|
HouseholderSequence<MatrixType, HCoeffsVectorType> hseq(m2, hc, false, hc.size(), shift);
|
|
|
|
MatrixType m5 = m2;
|
|
|
|
m5.block(shift,0,brows,cols).template triangularView<StrictlyLower>().setZero();
|
|
|
|
VERIFY_IS_APPROX(hseq * m5, m1); // test applying hseq directly
|
|
|
|
m3 = hseq;
|
2010-01-15 08:16:49 +08:00
|
|
|
VERIFY_IS_APPROX(m3 * m5, m1); // test evaluating hseq to a dense matrix, then applying
|
2010-01-11 21:48:39 +08:00
|
|
|
|
|
|
|
// test householder sequence on the right with a shift
|
|
|
|
|
|
|
|
TMatrixType tm2 = m2.transpose();
|
2010-01-15 08:16:49 +08:00
|
|
|
HouseholderSequence<TMatrixType, HCoeffsVectorType, OnTheRight> rhseq(tm2, hc, false, hc.size(), shift);
|
|
|
|
VERIFY_IS_APPROX(rhseq * m5, m1); // test applying rhseq directly
|
|
|
|
m3 = rhseq;
|
|
|
|
VERIFY_IS_APPROX(m3 * m5, m1); // test evaluating rhseq to a dense matrix, then applying
|
2009-08-03 22:06:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_householder()
|
|
|
|
{
|
2010-04-16 22:13:32 +08:00
|
|
|
for(int i = 0; i < g_repeat; i++) {
|
2009-10-29 06:19:29 +08:00
|
|
|
CALL_SUBTEST_1( householder(Matrix<double,2,2>()) );
|
|
|
|
CALL_SUBTEST_2( householder(Matrix<float,2,3>()) );
|
|
|
|
CALL_SUBTEST_3( householder(Matrix<double,3,5>()) );
|
|
|
|
CALL_SUBTEST_4( householder(Matrix<float,4,4>()) );
|
|
|
|
CALL_SUBTEST_5( householder(MatrixXd(10,12)) );
|
|
|
|
CALL_SUBTEST_6( householder(MatrixXcf(16,17)) );
|
2010-01-11 21:48:39 +08:00
|
|
|
CALL_SUBTEST_7( householder(MatrixXf(25,7)) );
|
2010-04-17 00:32:33 +08:00
|
|
|
CALL_SUBTEST_8( householder(Matrix<double,1,1>()) );
|
2009-08-03 22:06:57 +08:00
|
|
|
}
|
|
|
|
}
|