unsupported/Eigen/src/IterativeSolvers/Scaling.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2012 Desire NUENTSA WAKAM <desire.nuentsa_wakam@inria.fr
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_ITERSCALING_H
11 #define EIGEN_ITERSCALING_H
12 
13 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
50 template <typename MatrixType_>
51 class IterScaling {
52  public:
53  typedef MatrixType_ MatrixType;
54  typedef typename MatrixType::Scalar Scalar;
55  typedef typename MatrixType::Index Index;
56 
57  public:
58  IterScaling() { init(); }
59 
61  init();
62  compute(matrix);
63  }
64 
66 
74  void compute(const MatrixType& mat) {
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  }
141  compute(mat);
142  mat = m_matrix;
143  }
146  VectorXd& LeftScaling() { return m_left; }
147 
150  VectorXd& RightScaling() { return m_right; }
151 
154  void setTolerance(double tol) { m_tol = tol; }
155 
156  protected:
157  void init() {
158  m_tol = 1e-10;
159  m_maxits = 5;
160  m_isInitialized = false;
161  }
162 
166  VectorXd m_left; // Left scaling vector
167  VectorXd m_right; // m_right scaling vector
168  double m_tol;
169  int m_maxits; // Maximum number of iterations allowed
170 };
171 } // namespace Eigen
172 #endif
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
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
Array< double, 1, 3 > e(1./3., 0.5, 2.)
Eigen::SparseMatrix< double > mat
Definition: EigenUnitTest.cpp:10
#define eigen_assert(x)
Definition: Macros.h:910
SCALAR Scalar
Definition: bench_gemm.cpp:45
iterative scaling algorithm to equilibrate rows and column norms in matrices
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:51
void setTolerance(double tol)
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:154
void compute(const MatrixType &mat)
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:74
VectorXd m_left
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:166
void computeRef(MatrixType &mat)
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:140
MatrixType m_matrix
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:163
MatrixType::Scalar Scalar
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:54
ComputationInfo m_info
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:164
~IterScaling()
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:65
MatrixType_ MatrixType
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:53
bool m_isInitialized
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:165
void init()
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:157
VectorXd & LeftScaling()
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:146
VectorXd & RightScaling()
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:150
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
IterScaling(const MatrixType &matrix)
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:60
IterScaling()
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:58
MatrixType::Index Index
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:55
Index cols() const
Definition: SparseMatrix.h:161
void resize(Index rows, Index cols)
Definition: SparseMatrix.h:734
Index rows() const
Definition: SparseMatrix.h:159
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
ComputationInfo
Definition: Constants.h:438
int * m
Definition: level2_cplx_impl.h:294
char char char int int * k
Definition: level2_impl.h:374
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83