Eigen::IterScaling< MatrixType_ > Class Template Reference

iterative scaling algorithm to equilibrate rows and column norms in matrices More...

#include <Scaling.h>

Public Types

typedef MatrixType_ MatrixType
 
typedef MatrixType::Scalar Scalar
 
typedef MatrixType::Index Index
 

Public Member Functions

 IterScaling ()
 
 IterScaling (const MatrixType &matrix)
 
 ~IterScaling ()
 
void compute (const MatrixType &mat)
 
void computeRef (MatrixType &mat)
 
VectorXd & LeftScaling ()
 
VectorXd & RightScaling ()
 
void setTolerance (double tol)
 

Protected Member Functions

void init ()
 

Protected Attributes

MatrixType m_matrix
 
ComputationInfo m_info
 
bool m_isInitialized
 
VectorXd m_left
 
VectorXd m_right
 
double m_tol
 
int m_maxits
 

Detailed Description

template<typename MatrixType_>
class Eigen::IterScaling< MatrixType_ >

iterative scaling algorithm to equilibrate rows and column norms in matrices

This class can be used as a preprocessing tool to accelerate the convergence of iterative methods

This feature is useful to limit the pivoting amount during LU/ILU factorization The scaling strategy as presented here preserves the symmetry of the problem NOTE It is assumed that the matrix does not have empty row or column,

Example with key steps

VectorXd x(n), b(n);
SparseMatrix<double> A;
// fill A and b;
IterScaling<SparseMatrix<double> > scal;
// Compute the left and right scaling vectors. The matrix is equilibrated at output
scal.computeRef(A);
// Scale the right hand side
b = scal.LeftScaling().cwiseProduct(b);
// Now, solve the equilibrated linear system with any available solver
// Scale back the computed solution
x = scal.RightScaling().cwiseProduct(x);
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
Scalar * b
Definition: benchVecAdd.cpp:17
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:47
EIGEN_BLAS_FUNC() scal(int *n, RealScalar *palpha, RealScalar *px, int *incx)
Definition: level1_impl.h:105
list x
Definition: plotDoE.py:28
Template Parameters
MatrixType_the type of the matrix. It should be a real square sparsematrix

References : D. Ruiz and B. Ucar, A Symmetry Preserving Algorithm for Matrix Scaling, INRIA Research report RR-7552

See also
IncompleteLUT

Member Typedef Documentation

◆ Index

template<typename MatrixType_ >
typedef MatrixType::Index Eigen::IterScaling< MatrixType_ >::Index

◆ MatrixType

template<typename MatrixType_ >
typedef MatrixType_ Eigen::IterScaling< MatrixType_ >::MatrixType

◆ Scalar

template<typename MatrixType_ >
typedef MatrixType::Scalar Eigen::IterScaling< MatrixType_ >::Scalar

Constructor & Destructor Documentation

◆ IterScaling() [1/2]

template<typename MatrixType_ >
Eigen::IterScaling< MatrixType_ >::IterScaling ( )
inline
58 { init(); }
void init()
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:157

References Eigen::IterScaling< MatrixType_ >::init().

◆ IterScaling() [2/2]

template<typename MatrixType_ >
Eigen::IterScaling< MatrixType_ >::IterScaling ( const MatrixType matrix)
inline
60  {
61  init();
62  compute(matrix);
63  }
void compute(const MatrixType &mat)
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:74
Eigen::Map< Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor >, 0, Eigen::OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: common.h:85

References Eigen::IterScaling< MatrixType_ >::compute(), Eigen::IterScaling< MatrixType_ >::init(), and matrix().

◆ ~IterScaling()

template<typename MatrixType_ >
Eigen::IterScaling< MatrixType_ >::~IterScaling ( )
inline
65 {}

Member Function Documentation

◆ compute()

template<typename MatrixType_ >
void Eigen::IterScaling< MatrixType_ >::compute ( const MatrixType mat)
inline

Compute the left and right diagonal matrices to scale the input matrix mat

FIXME This algorithm will be modified such that the diagonal elements are permuted on the diagonal.

See also
LeftScaling() RightScaling()
74  {
75  using std::abs;
76  int m = mat.rows();
77  int n = mat.cols();
78  eigen_assert((m > 0 && m == n) && "Please give a non - empty matrix");
79  m_left.resize(m);
80  m_right.resize(n);
81  m_left.setOnes();
82  m_right.setOnes();
83  m_matrix = mat;
84  VectorXd Dr, Dc, DrRes, DcRes; // Temporary Left and right scaling vectors
85  Dr.resize(m);
86  Dc.resize(n);
87  DrRes.resize(m);
88  DcRes.resize(n);
89  double EpsRow = 1.0, EpsCol = 1.0;
90  int its = 0;
91  do { // Iterate until the infinite norm of each row and column is approximately 1
92  // Get the maximum value in each row and column
93  Dr.setZero();
94  Dc.setZero();
95  for (int k = 0; k < m_matrix.outerSize(); ++k) {
96  for (typename MatrixType::InnerIterator it(m_matrix, k); it; ++it) {
97  if (Dr(it.row()) < abs(it.value())) Dr(it.row()) = abs(it.value());
98 
99  if (Dc(it.col()) < abs(it.value())) Dc(it.col()) = abs(it.value());
100  }
101  }
102  for (int i = 0; i < m; ++i) {
103  Dr(i) = std::sqrt(Dr(i));
104  }
105  for (int i = 0; i < n; ++i) {
106  Dc(i) = std::sqrt(Dc(i));
107  }
108  // Save the scaling factors
109  for (int i = 0; i < m; ++i) {
110  m_left(i) /= Dr(i);
111  }
112  for (int i = 0; i < n; ++i) {
113  m_right(i) /= Dc(i);
114  }
115  // Scale the rows and the columns of the matrix
116  DrRes.setZero();
117  DcRes.setZero();
118  for (int k = 0; k < m_matrix.outerSize(); ++k) {
119  for (typename MatrixType::InnerIterator it(m_matrix, k); it; ++it) {
120  it.valueRef() = it.value() / (Dr(it.row()) * Dc(it.col()));
121  // Accumulate the norms of the row and column vectors
122  if (DrRes(it.row()) < abs(it.value())) DrRes(it.row()) = abs(it.value());
123 
124  if (DcRes(it.col()) < abs(it.value())) DcRes(it.col()) = abs(it.value());
125  }
126  }
127  DrRes.array() = (1 - DrRes.array()).abs();
128  EpsRow = DrRes.maxCoeff();
129  DcRes.array() = (1 - DcRes.array()).abs();
130  EpsCol = DcRes.maxCoeff();
131  its++;
132  } while ((EpsRow > m_tol || EpsCol > m_tol) && (its < m_maxits));
133  m_isInitialized = true;
134  }
AnnoyingScalar abs(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:135
AnnoyingScalar sqrt(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:134
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Eigen::SparseMatrix< double > mat
Definition: EigenUnitTest.cpp:10
#define eigen_assert(x)
Definition: Macros.h:910
VectorXd m_left
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:166
MatrixType m_matrix
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:163
bool m_isInitialized
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:165
VectorXd m_right
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:167
int m_maxits
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:169
double m_tol
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:168
Index cols() const
Definition: SparseMatrix.h:161
void resize(Index rows, Index cols)
Definition: SparseMatrix.h:734
Index rows() const
Definition: SparseMatrix.h:159
int * m
Definition: level2_cplx_impl.h:294
char char char int int * k
Definition: level2_impl.h:374

References abs(), Eigen::SparseMatrix< Scalar_, Options_, StorageIndex_ >::cols(), eigen_assert, i, k, m, Eigen::IterScaling< MatrixType_ >::m_isInitialized, Eigen::IterScaling< MatrixType_ >::m_left, Eigen::IterScaling< MatrixType_ >::m_matrix, Eigen::IterScaling< MatrixType_ >::m_maxits, Eigen::IterScaling< MatrixType_ >::m_right, Eigen::IterScaling< MatrixType_ >::m_tol, n, Eigen::SparseMatrix< Scalar_, Options_, StorageIndex_ >::resize(), Eigen::SparseMatrix< Scalar_, Options_, StorageIndex_ >::rows(), and sqrt().

Referenced by Eigen::IterScaling< MatrixType_ >::computeRef(), and Eigen::IterScaling< MatrixType_ >::IterScaling().

◆ computeRef()

template<typename MatrixType_ >
void Eigen::IterScaling< MatrixType_ >::computeRef ( MatrixType mat)
inline

Compute the left and right vectors to scale the vectors the input matrix is scaled with the computed vectors at output

See also
compute()
140  {
141  compute(mat);
142  mat = m_matrix;
143  }

References Eigen::IterScaling< MatrixType_ >::compute(), and Eigen::IterScaling< MatrixType_ >::m_matrix.

◆ init()

template<typename MatrixType_ >
void Eigen::IterScaling< MatrixType_ >::init ( )
inlineprotected

◆ LeftScaling()

template<typename MatrixType_ >
VectorXd& Eigen::IterScaling< MatrixType_ >::LeftScaling ( )
inline

Get the vector to scale the rows of the matrix

146 { return m_left; }

References Eigen::IterScaling< MatrixType_ >::m_left.

◆ RightScaling()

template<typename MatrixType_ >
VectorXd& Eigen::IterScaling< MatrixType_ >::RightScaling ( )
inline

Get the vector to scale the columns of the matrix

150 { return m_right; }

References Eigen::IterScaling< MatrixType_ >::m_right.

◆ setTolerance()

template<typename MatrixType_ >
void Eigen::IterScaling< MatrixType_ >::setTolerance ( double  tol)
inline

Set the tolerance for the convergence of the iterative scaling algorithm

154 { m_tol = tol; }

References Eigen::IterScaling< MatrixType_ >::m_tol.

Member Data Documentation

◆ m_info

template<typename MatrixType_ >
ComputationInfo Eigen::IterScaling< MatrixType_ >::m_info
mutableprotected

◆ m_isInitialized

template<typename MatrixType_ >
bool Eigen::IterScaling< MatrixType_ >::m_isInitialized
protected

◆ m_left

template<typename MatrixType_ >
VectorXd Eigen::IterScaling< MatrixType_ >::m_left
protected

◆ m_matrix

template<typename MatrixType_ >
MatrixType Eigen::IterScaling< MatrixType_ >::m_matrix
protected

◆ m_maxits

template<typename MatrixType_ >
int Eigen::IterScaling< MatrixType_ >::m_maxits
protected

◆ m_right

template<typename MatrixType_ >
VectorXd Eigen::IterScaling< MatrixType_ >::m_right
protected

◆ m_tol


The documentation for this class was generated from the following file: