the Index types change.

As discussed on the list (too long to explain here).
This commit is contained in:
Benoit Jacob
2010-05-30 16:00:58 -04:00
parent faa3ff3be6
commit aaaade4b3d
158 changed files with 3137 additions and 2878 deletions

View File

@@ -1,7 +1,7 @@
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
// Copyright (C) 2009-2010 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
@@ -63,6 +63,7 @@ template<typename MatrixType, unsigned int Options> class JacobiSVD
private:
typedef typename MatrixType::Scalar Scalar;
typedef typename NumTraits<typename MatrixType::Scalar>::Real RealScalar;
typedef typename MatrixType::Index Index;
enum {
ComputeU = (Options & SkipU) == 0,
ComputeV = (Options & SkipV) == 0,
@@ -107,7 +108,7 @@ template<typename MatrixType, unsigned int Options> class JacobiSVD
* according to the specified problem \a size.
* \sa JacobiSVD()
*/
JacobiSVD(int rows, int cols) : m_matrixU(rows, rows),
JacobiSVD(Index rows, Index cols) : m_matrixU(rows, rows),
m_matrixV(cols, cols),
m_singularValues(std::min(rows, cols)),
m_workMatrix(rows, cols),
@@ -119,7 +120,7 @@ template<typename MatrixType, unsigned int Options> class JacobiSVD
m_workMatrix(),
m_isInitialized(false)
{
const int minSize = std::min(matrix.rows(), matrix.cols());
const Index minSize = std::min(matrix.rows(), matrix.cols());
m_singularValues.resize(minSize);
m_workMatrix.resize(minSize, minSize);
compute(matrix);
@@ -164,7 +165,8 @@ template<typename MatrixType, unsigned int Options>
struct ei_svd_precondition_2x2_block_to_be_real<MatrixType, Options, false>
{
typedef JacobiSVD<MatrixType, Options> SVD;
static void run(typename SVD::WorkMatrixType&, JacobiSVD<MatrixType, Options>&, int, int) {}
typedef typename SVD::Index Index;
static void run(typename SVD::WorkMatrixType&, JacobiSVD<MatrixType, Options>&, Index, Index) {}
};
template<typename MatrixType, unsigned int Options>
@@ -173,8 +175,9 @@ struct ei_svd_precondition_2x2_block_to_be_real<MatrixType, Options, true>
typedef JacobiSVD<MatrixType, Options> SVD;
typedef typename MatrixType::Scalar Scalar;
typedef typename MatrixType::RealScalar RealScalar;
typedef typename SVD::Index Index;
enum { ComputeU = SVD::ComputeU, ComputeV = SVD::ComputeV };
static void run(typename SVD::WorkMatrixType& work_matrix, JacobiSVD<MatrixType, Options>& svd, int p, int q)
static void run(typename SVD::WorkMatrixType& work_matrix, JacobiSVD<MatrixType, Options>& svd, Index p, Index q)
{
Scalar z;
PlanarRotation<Scalar> rot;
@@ -210,8 +213,8 @@ struct ei_svd_precondition_2x2_block_to_be_real<MatrixType, Options, true>
}
};
template<typename MatrixType, typename RealScalar>
void ei_real_2x2_jacobi_svd(const MatrixType& matrix, int p, int q,
template<typename MatrixType, typename RealScalar, typename Index>
void ei_real_2x2_jacobi_svd(const MatrixType& matrix, Index p, Index q,
PlanarRotation<RealScalar> *j_left,
PlanarRotation<RealScalar> *j_right)
{
@@ -250,12 +253,13 @@ struct ei_svd_precondition_if_more_rows_than_cols<MatrixType, Options, true>
typedef JacobiSVD<MatrixType, Options> SVD;
typedef typename MatrixType::Scalar Scalar;
typedef typename MatrixType::RealScalar RealScalar;
typedef typename MatrixType::Index Index;
enum { ComputeU = SVD::ComputeU, ComputeV = SVD::ComputeV };
static bool run(const MatrixType& matrix, typename SVD::WorkMatrixType& work_matrix, SVD& svd)
{
int rows = matrix.rows();
int cols = matrix.cols();
int diagSize = cols;
Index rows = matrix.rows();
Index cols = matrix.cols();
Index diagSize = cols;
if(rows > cols)
{
FullPivHouseholderQR<MatrixType> qr(matrix);
@@ -282,6 +286,7 @@ struct ei_svd_precondition_if_more_cols_than_rows<MatrixType, Options, true>
typedef JacobiSVD<MatrixType, Options> SVD;
typedef typename MatrixType::Scalar Scalar;
typedef typename MatrixType::RealScalar RealScalar;
typedef typename MatrixType::Index Index;
enum {
ComputeU = SVD::ComputeU,
ComputeV = SVD::ComputeV,
@@ -294,9 +299,9 @@ struct ei_svd_precondition_if_more_cols_than_rows<MatrixType, Options, true>
static bool run(const MatrixType& matrix, typename SVD::WorkMatrixType& work_matrix, SVD& svd)
{
int rows = matrix.rows();
int cols = matrix.cols();
int diagSize = rows;
Index rows = matrix.rows();
Index cols = matrix.cols();
Index diagSize = rows;
if(cols > rows)
{
typedef Matrix<Scalar,ColsAtCompileTime,RowsAtCompileTime,
@@ -315,9 +320,9 @@ struct ei_svd_precondition_if_more_cols_than_rows<MatrixType, Options, true>
template<typename MatrixType, unsigned int Options>
JacobiSVD<MatrixType, Options>& JacobiSVD<MatrixType, Options>::compute(const MatrixType& matrix)
{
int rows = matrix.rows();
int cols = matrix.cols();
int diagSize = std::min(rows, cols);
Index rows = matrix.rows();
Index cols = matrix.cols();
Index diagSize = std::min(rows, cols);
m_singularValues.resize(diagSize);
const RealScalar precision = 2 * NumTraits<Scalar>::epsilon();
@@ -333,9 +338,9 @@ JacobiSVD<MatrixType, Options>& JacobiSVD<MatrixType, Options>::compute(const Ma
while(!finished)
{
finished = true;
for(int p = 1; p < diagSize; ++p)
for(Index p = 1; p < diagSize; ++p)
{
for(int q = 0; q < p; ++q)
for(Index q = 0; q < p; ++q)
{
if(std::max(ei_abs(m_workMatrix.coeff(p,q)),ei_abs(m_workMatrix.coeff(q,p)))
> std::max(ei_abs(m_workMatrix.coeff(p,p)),ei_abs(m_workMatrix.coeff(q,q)))*precision)
@@ -356,16 +361,16 @@ JacobiSVD<MatrixType, Options>& JacobiSVD<MatrixType, Options>::compute(const Ma
}
}
for(int i = 0; i < diagSize; ++i)
for(Index i = 0; i < diagSize; ++i)
{
RealScalar a = ei_abs(m_workMatrix.coeff(i,i));
m_singularValues.coeffRef(i) = a;
if(ComputeU && (a!=RealScalar(0))) m_matrixU.col(i) *= m_workMatrix.coeff(i,i)/a;
}
for(int i = 0; i < diagSize; i++)
for(Index i = 0; i < diagSize; i++)
{
int pos;
Index pos;
m_singularValues.tail(diagSize-i).maxCoeff(&pos);
if(pos)
{

View File

@@ -46,6 +46,7 @@ template<typename _MatrixType> class SVD
typedef _MatrixType MatrixType;
typedef typename MatrixType::Scalar Scalar;
typedef typename NumTraits<typename MatrixType::Scalar>::Real RealScalar;
typedef typename MatrixType::Index Index;
enum {
RowsAtCompileTime = MatrixType::RowsAtCompileTime,
@@ -79,7 +80,7 @@ template<typename _MatrixType> class SVD
* according to the specified problem \a size.
* \sa JacobiSVD()
*/
SVD(int rows, int cols) : m_matU(rows, rows),
SVD(Index rows, Index cols) : m_matU(rows, rows),
m_matV(cols,cols),
m_sigma(std::min(rows, cols)),
m_workMatrix(rows, cols),
@@ -143,13 +144,13 @@ template<typename _MatrixType> class SVD
template<typename ScalingType, typename RotationType>
void computeScalingRotation(ScalingType *positive, RotationType *unitary) const;
inline int rows() const
inline Index rows() const
{
ei_assert(m_isInitialized && "SVD is not initialized.");
return m_rows;
}
inline int cols() const
inline Index cols() const
{
ei_assert(m_isInitialized && "SVD is not initialized.");
return m_cols;
@@ -182,7 +183,7 @@ template<typename _MatrixType> class SVD
MatrixType m_workMatrix;
RowVector m_rv1;
bool m_isInitialized;
int m_rows, m_cols;
Index m_rows, m_cols;
};
/** Computes / recomputes the SVD decomposition A = U S V^* of \a matrix
@@ -194,8 +195,8 @@ template<typename _MatrixType> class SVD
template<typename MatrixType>
SVD<MatrixType>& SVD<MatrixType>::compute(const MatrixType& matrix)
{
const int m = m_rows = matrix.rows();
const int n = m_cols = matrix.cols();
const Index m = m_rows = matrix.rows();
const Index n = m_cols = matrix.cols();
m_matU.resize(m, m);
m_matU.setZero();
@@ -203,14 +204,14 @@ SVD<MatrixType>& SVD<MatrixType>::compute(const MatrixType& matrix)
m_matV.resize(n,n);
m_workMatrix = matrix;
int max_iters = 30;
Index max_iters = 30;
MatrixVType& V = m_matV;
MatrixType& A = m_workMatrix;
SingularValuesType& W = m_sigma;
bool flag;
int i=0,its=0,j=0,k=0,l=0,nm=0;
Index i=0,its=0,j=0,k=0,l=0,nm=0;
Scalar anorm, c, f, g, h, s, scale, x, y, z;
bool convergence = true;
Scalar eps = NumTraits<Scalar>::dummy_precision();
@@ -426,9 +427,9 @@ SVD<MatrixType>& SVD<MatrixType>::compute(const MatrixType& matrix)
// sort the singular values:
{
for (int i=0; i<n; i++)
for (Index i=0; i<n; i++)
{
int k;
Index k;
W.tail(n-i).maxCoeff(&k);
if (k != 0)
{
@@ -459,11 +460,11 @@ struct ei_solve_retval<SVD<_MatrixType>, Rhs>
{
ei_assert(rhs().rows() == dec().rows());
for (int j=0; j<cols(); ++j)
for (Index j=0; j<cols(); ++j)
{
Matrix<Scalar,MatrixType::RowsAtCompileTime,1> aux = dec().matrixU().adjoint() * rhs().col(j);
for (int i = 0; i < dec().rows(); ++i)
for (Index i = 0; i < dec().rows(); ++i)
{
Scalar si = dec().singularValues().coeff(i);
if(si == RealScalar(0))
@@ -471,7 +472,7 @@ struct ei_solve_retval<SVD<_MatrixType>, Rhs>
else
aux.coeffRef(i) /= si;
}
const int minsize = std::min(dec().rows(),dec().cols());
const Index minsize = std::min(dec().rows(),dec().cols());
dst.col(j).head(minsize) = aux.head(minsize);
if(dec().cols()>dec().rows()) dst.col(j).tail(cols()-minsize).setZero();
dst.col(j) = dec().matrixV() * dst.col(j);

View File

@@ -37,6 +37,7 @@ template<typename _MatrixType> class UpperBidiagonalization
};
typedef typename MatrixType::Scalar Scalar;
typedef typename MatrixType::RealScalar RealScalar;
typedef typename MatrixType::Index Index;
typedef Matrix<Scalar, 1, ColsAtCompileTime> RowVectorType;
typedef Matrix<Scalar, RowsAtCompileTime, 1> ColVectorType;
typedef BandMatrix<RealScalar, ColsAtCompileTime, ColsAtCompileTime, 1, 0> BidiagonalType;
@@ -95,8 +96,8 @@ template<typename _MatrixType> class UpperBidiagonalization
template<typename _MatrixType>
UpperBidiagonalization<_MatrixType>& UpperBidiagonalization<_MatrixType>::compute(const _MatrixType& matrix)
{
int rows = matrix.rows();
int cols = matrix.cols();
Index rows = matrix.rows();
Index cols = matrix.cols();
ei_assert(rows >= cols && "UpperBidiagonalization is only for matrices satisfying rows>=cols.");
@@ -104,10 +105,10 @@ UpperBidiagonalization<_MatrixType>& UpperBidiagonalization<_MatrixType>::comput
ColVectorType temp(rows);
for (int k = 0; /* breaks at k==cols-1 below */ ; ++k)
for (Index k = 0; /* breaks at k==cols-1 below */ ; ++k)
{
int remainingRows = rows - k;
int remainingCols = cols - k - 1;
Index remainingRows = rows - k;
Index remainingCols = cols - k - 1;
// construct left householder transform in-place in m_householder
m_householder.col(k).tail(remainingRows)