This commit is contained in:
Gael Guennebaud 2009-08-03 16:11:30 +02:00
commit a8f943127c
10 changed files with 246 additions and 12 deletions

View File

@ -8,8 +8,7 @@ string(REGEX MATCH "define *EIGEN_MAJOR_VERSION ([0-9]*)" _eigen2_major_version_
set(EIGEN2_MAJOR_VERSION "${CMAKE_MATCH_1}") set(EIGEN2_MAJOR_VERSION "${CMAKE_MATCH_1}")
string(REGEX MATCH "define *EIGEN_MINOR_VERSION ([0-9]*)" _eigen2_minor_version_match "${_eigen2_version_header}") string(REGEX MATCH "define *EIGEN_MINOR_VERSION ([0-9]*)" _eigen2_minor_version_match "${_eigen2_version_header}")
set(EIGEN2_MINOR_VERSION "${CMAKE_MATCH_1}") set(EIGEN2_MINOR_VERSION "${CMAKE_MATCH_1}")
# TODO find a way to automatically remove the unstable suffix set(EIGEN_VERSION_NUMBER ${EIGEN2_WORLD_VERSION}.${EIGEN2_MAJOR_VERSION}.${EIGEN2_MINOR_VERSION})
set(EIGEN_VERSION_NUMBER ${EIGEN2_WORLD_VERSION}.${EIGEN2_MAJOR_VERSION}.${EIGEN2_MINOR_VERSION}-unstable)
# if the mercurial program is absent, this will leave the EIGEN_HG_REVISION string empty, # if the mercurial program is absent, this will leave the EIGEN_HG_REVISION string empty,
# but won't stop CMake. # but won't stop CMake.

24
Eigen/Householder Normal file
View File

@ -0,0 +1,24 @@
#ifndef EIGEN_HOUSEHOLDER_MODULE_H
#define EIGEN_HOUSEHOLDER_MODULE_H
#include "Core"
#include "src/Core/util/DisableMSVCWarnings.h"
namespace Eigen {
/** \defgroup Householder_Module Householder module
* This module provides householder transformations.
*
* \code
* #include <Eigen/Householder>
* \endcode
*/
#include "src/Householder/Householder.h"
} // namespace Eigen
#include "src/Core/util/EnableMSVCWarnings.h"
#endif // EIGEN_HOUSEHOLDER_MODULE_H

View File

@ -355,10 +355,22 @@ template<typename ExpressionType, int Direction> class VectorwiseOp
/////////// Artithmetic operators /////////// /////////// Artithmetic operators ///////////
/** Copies the vector \a other to each subvector of \c *this */
template<typename OtherDerived>
ExpressionType& operator=(const MatrixBase<OtherDerived>& other)
{
EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
//ei_assert((m_matrix.isNull()) == (other.isNull())); FIXME
for(int j=0; j<subVectors(); ++j)
subVector(j) = other;
return const_cast<ExpressionType&>(m_matrix);
}
/** Adds the vector \a other to each subvector of \c *this */ /** Adds the vector \a other to each subvector of \c *this */
template<typename OtherDerived> template<typename OtherDerived>
ExpressionType& operator+=(const MatrixBase<OtherDerived>& other) ExpressionType& operator+=(const MatrixBase<OtherDerived>& other)
{ {
EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
for(int j=0; j<subVectors(); ++j) for(int j=0; j<subVectors(); ++j)
subVector(j) += other; subVector(j) += other;
return const_cast<ExpressionType&>(m_matrix); return const_cast<ExpressionType&>(m_matrix);
@ -368,6 +380,7 @@ template<typename ExpressionType, int Direction> class VectorwiseOp
template<typename OtherDerived> template<typename OtherDerived>
ExpressionType& operator-=(const MatrixBase<OtherDerived>& other) ExpressionType& operator-=(const MatrixBase<OtherDerived>& other)
{ {
EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
for(int j=0; j<subVectors(); ++j) for(int j=0; j<subVectors(); ++j)
subVector(j) -= other; subVector(j) -= other;
return const_cast<ExpressionType&>(m_matrix); return const_cast<ExpressionType&>(m_matrix);

View File

@ -412,8 +412,10 @@ template<typename Derived, typename OtherDerived,
bool EvalBeforeAssigning = (int(OtherDerived::Flags) & EvalBeforeAssigningBit) != 0, bool EvalBeforeAssigning = (int(OtherDerived::Flags) & EvalBeforeAssigningBit) != 0,
bool NeedToTranspose = Derived::IsVectorAtCompileTime bool NeedToTranspose = Derived::IsVectorAtCompileTime
&& OtherDerived::IsVectorAtCompileTime && OtherDerived::IsVectorAtCompileTime
&& int(Derived::RowsAtCompileTime) == int(OtherDerived::ColsAtCompileTime) && ((int(Derived::RowsAtCompileTime) == 1 && int(OtherDerived::ColsAtCompileTime) == 1)
&& int(Derived::ColsAtCompileTime) == int(OtherDerived::RowsAtCompileTime) | // FIXME | instead of || to please GCC 4.4.0 stupid warning "suggest parentheses around &&".
// revert to || as soon as not needed anymore.
(int(Derived::ColsAtCompileTime) == 1 && int(OtherDerived::RowsAtCompileTime) == 1))
&& int(Derived::SizeAtCompileTime) != 1> && int(Derived::SizeAtCompileTime) != 1>
struct ei_assign_selector; struct ei_assign_selector;

View File

@ -49,7 +49,7 @@ template<typename Derived>
template<typename OtherDerived> template<typename OtherDerived>
bool MatrixBase<Derived>::isApprox( bool MatrixBase<Derived>::isApprox(
const MatrixBase<OtherDerived>& other, const MatrixBase<OtherDerived>& other,
typename NumTraits<Scalar>::Real prec RealScalar prec
) const ) const
{ {
const typename ei_nested<Derived,2>::type nested(derived()); const typename ei_nested<Derived,2>::type nested(derived());
@ -73,7 +73,7 @@ bool MatrixBase<Derived>::isApprox(
template<typename Derived> template<typename Derived>
bool MatrixBase<Derived>::isMuchSmallerThan( bool MatrixBase<Derived>::isMuchSmallerThan(
const typename NumTraits<Scalar>::Real& other, const typename NumTraits<Scalar>::Real& other,
typename NumTraits<Scalar>::Real prec RealScalar prec
) const ) const
{ {
return cwise().abs2().sum() <= prec * prec * other * other; return cwise().abs2().sum() <= prec * prec * other * other;
@ -93,7 +93,7 @@ template<typename Derived>
template<typename OtherDerived> template<typename OtherDerived>
bool MatrixBase<Derived>::isMuchSmallerThan( bool MatrixBase<Derived>::isMuchSmallerThan(
const MatrixBase<OtherDerived>& other, const MatrixBase<OtherDerived>& other,
typename NumTraits<Scalar>::Real prec RealScalar prec
) const ) const
{ {
return this->cwise().abs2().sum() <= prec * prec * other.cwise().abs2().sum(); return this->cwise().abs2().sum() <= prec * prec * other.cwise().abs2().sum();
@ -124,7 +124,7 @@ template<typename Derived>
template<typename OtherDerived> template<typename OtherDerived>
bool MatrixBase<Derived>::isApprox( bool MatrixBase<Derived>::isApprox(
const MatrixBase<OtherDerived>& other, const MatrixBase<OtherDerived>& other,
typename NumTraits<Scalar>::Real prec RealScalar prec
) const ) const
{ {
return ei_fuzzy_selector<Derived,OtherDerived>::isApprox(derived(), other.derived(), prec); return ei_fuzzy_selector<Derived,OtherDerived>::isApprox(derived(), other.derived(), prec);
@ -143,7 +143,7 @@ bool MatrixBase<Derived>::isApprox(
template<typename Derived> template<typename Derived>
bool MatrixBase<Derived>::isMuchSmallerThan( bool MatrixBase<Derived>::isMuchSmallerThan(
const typename NumTraits<Scalar>::Real& other, const typename NumTraits<Scalar>::Real& other,
typename NumTraits<Scalar>::Real prec RealScalar prec
) const ) const
{ {
return ei_fuzzy_selector<Derived>::isMuchSmallerThan(derived(), other, prec); return ei_fuzzy_selector<Derived>::isMuchSmallerThan(derived(), other, prec);
@ -163,7 +163,7 @@ template<typename Derived>
template<typename OtherDerived> template<typename OtherDerived>
bool MatrixBase<Derived>::isMuchSmallerThan( bool MatrixBase<Derived>::isMuchSmallerThan(
const MatrixBase<OtherDerived>& other, const MatrixBase<OtherDerived>& other,
typename NumTraits<Scalar>::Real prec RealScalar prec
) const ) const
{ {
return ei_fuzzy_selector<Derived,OtherDerived>::isMuchSmallerThan(derived(), other.derived(), prec); return ei_fuzzy_selector<Derived,OtherDerived>::isMuchSmallerThan(derived(), other.derived(), prec);

View File

@ -45,7 +45,7 @@ template<typename Derived> struct AnyMatrixBase
inline int rows() const { return derived().rows(); } inline int rows() const { return derived().rows(); }
/** \returns the number of columns. \sa rows(), ColsAtCompileTime*/ /** \returns the number of columns. \sa rows(), ColsAtCompileTime*/
inline int cols() const { return derived().cols(); } inline int cols() const { return derived().cols(); }
template<typename Dest> inline void evalTo(Dest& dst) const template<typename Dest> inline void evalTo(Dest& dst) const
{ derived().evalTo(dst); } { derived().evalTo(dst); }
@ -765,6 +765,19 @@ template<typename Derived> class MatrixBase
template<typename Derived1, typename Derived2> template<typename Derived1, typename Derived2>
Derived& lazyAssign(const SparseProduct<Derived1,Derived2,DenseTimeSparseProduct>& product); Derived& lazyAssign(const SparseProduct<Derived1,Derived2,DenseTimeSparseProduct>& product);
////////// Householder module ///////////
template<typename EssentialPart>
void makeHouseholder(EssentialPart *essential,
RealScalar *beta) const;
template<typename EssentialPart>
void applyHouseholderOnTheLeft(const EssentialPart& essential,
const RealScalar& beta);
template<typename EssentialPart>
void applyHouseholderOnTheRight(const EssentialPart& essential,
const RealScalar& beta);
#ifdef EIGEN_MATRIXBASE_PLUGIN #ifdef EIGEN_MATRIXBASE_PLUGIN
#include EIGEN_MATRIXBASE_PLUGIN #include EIGEN_MATRIXBASE_PLUGIN
#endif #endif

View File

@ -0,0 +1,6 @@
FILE(GLOB Eigen_Householder_SRCS "*.h")
INSTALL(FILES
${Eigen_Householder_SRCS}
DESTINATION ${INCLUDE_INSTALL_DIR}/Eigen/src/Householder COMPONENT Devel
)

View File

@ -0,0 +1,88 @@
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
//
// 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/>.
#ifndef EIGEN_HOUSEHOLDER_H
#define EIGEN_HOUSEHOLDER_H
template<int n> struct ei_decrement_size
{
enum {
ret = (n==1 || n==Dynamic) ? n : n-1
};
};
template<typename Derived>
template<typename EssentialPart>
void MatrixBase<Derived>::makeHouseholder(
EssentialPart *essential,
RealScalar *beta) const
{
EIGEN_STATIC_ASSERT_VECTOR_ONLY(EssentialPart)
RealScalar _squaredNorm = squaredNorm();
Scalar c0;
if(ei_abs2(coeff(0)) <= ei_abs2(precision<Scalar>()) * _squaredNorm)
{
c0 = ei_sqrt(_squaredNorm);
}
else
{
Scalar sign = coeff(0) / ei_abs(coeff(0));
c0 = coeff(0) + sign * ei_sqrt(_squaredNorm);
}
*essential = end(size()-1) / c0; // FIXME take advantage of fixed size
const RealScalar c0abs2 = ei_abs2(c0);
*beta = RealScalar(2) * c0abs2 / (c0abs2 + _squaredNorm - ei_abs2(coeff(0)));
}
template<typename Derived>
template<typename EssentialPart>
void MatrixBase<Derived>::applyHouseholderOnTheLeft(
const EssentialPart& essential,
const RealScalar& beta)
{
Matrix<Scalar, 1, ColsAtCompileTime, PlainMatrixType::Options, 1, MaxColsAtCompileTime> tmp(cols());
tmp = row(0) + essential.adjoint() * block(1,0,rows()-1,cols());
// FIXME take advantage of fixed size
// FIXME play with lazy()
// FIXME maybe not a good idea to use matrix product
row(0) -= beta * tmp;
block(1,0,rows()-1,cols()) -= beta * essential * tmp;
}
template<typename Derived>
template<typename EssentialPart>
void MatrixBase<Derived>::applyHouseholderOnTheRight(
const EssentialPart& essential,
const RealScalar& beta)
{
Matrix<Scalar, RowsAtCompileTime, 1, PlainMatrixType::Options, MaxRowsAtCompileTime, 1> tmp(rows());
tmp = col(0) + block(0,1,rows(),cols()-1) * essential.conjugate();
// FIXME take advantage of fixed size
// FIXME play with lazy()
// FIXME maybe not a good idea to use matrix product
col(0) -= beta * tmp;
block(0,1,rows(),cols()-1) -= beta * tmp * essential.transpose();
}
#endif // EIGEN_HOUSEHOLDER_H

View File

@ -149,7 +149,7 @@ ei_add_test(sparse_basic)
ei_add_test(sparse_product) ei_add_test(sparse_product)
ei_add_test(sparse_solvers " " "${SPARSE_LIBS}") ei_add_test(sparse_solvers " " "${SPARSE_LIBS}")
ei_add_test(umeyama) ei_add_test(umeyama)
ei_add_test(householder)
ei_add_property(EIGEN_TESTING_SUMMARY "CXX: ${CMAKE_CXX_COMPILER}\n") ei_add_property(EIGEN_TESTING_SUMMARY "CXX: ${CMAKE_CXX_COMPILER}\n")
if(CMAKE_COMPILER_IS_GNUCXX) if(CMAKE_COMPILER_IS_GNUCXX)

89
test/householder.cpp Normal file
View File

@ -0,0 +1,89 @@
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
//
// 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"
#include <Eigen/Householder>
template<typename MatrixType> void householder(const MatrixType& m)
{
/* this test covers the following files:
Householder.h
*/
int rows = m.rows();
int cols = m.cols();
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;
RealScalar beta;
EssentialVectorType essential;
VectorType v1 = VectorType::Random(rows), v2;
v2 = v1;
v1.makeHouseholder(&essential, &beta);
v1.applyHouseholderOnTheLeft(essential,beta);
VERIFY_IS_APPROX(v1.norm(), v2.norm());
VERIFY_IS_MUCH_SMALLER_THAN(v1.end(rows-1).norm(), v1.norm());
v1 = VectorType::Random(rows);
v2 = v1;
v1.applyHouseholderOnTheLeft(essential,beta);
VERIFY_IS_APPROX(v1.norm(), v2.norm());
MatrixType m1(rows, cols),
m2(rows, cols);
v1 = VectorType::Random(rows);
m1.colwise() = v1;
m2 = m1;
m1.col(0).makeHouseholder(&essential, &beta);
m1.applyHouseholderOnTheLeft(essential,beta);
VERIFY_IS_APPROX(m1.norm(), m2.norm());
VERIFY_IS_MUCH_SMALLER_THAN(m1.block(1,0,rows-1,cols).norm(), m1.norm());
v1 = VectorType::Random(rows);
SquareMatrixType m3(rows,rows), m4(rows,rows);
m3.rowwise() = v1.transpose();
m4 = m3;
m3.row(0).makeHouseholder(&essential, &beta);
m3.applyHouseholderOnTheRight(essential,beta);
VERIFY_IS_APPROX(m3.norm(), m4.norm());
VERIFY_IS_MUCH_SMALLER_THAN(m3.block(0,1,rows,rows-1).norm(), m3.norm());
}
void test_householder()
{
for(int i = 0; i < g_repeat; i++) {
CALL_SUBTEST( householder(Matrix<double,2,2>()) );
CALL_SUBTEST( householder(Matrix<float,2,3>()) );
CALL_SUBTEST( householder(Matrix<double,3,5>()) );
CALL_SUBTEST( householder(Matrix<float,4,4>()) );
CALL_SUBTEST( householder(MatrixXd(10,12)) );
CALL_SUBTEST( householder(MatrixXcf(16,17)) );
}
}