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 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
49 template<typename MatrixType_>
51 {
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 
60  IterScaling(const MatrixType& matrix)
61  {
62  init();
63  compute(matrix);
64  }
65 
67 
75  void compute (const MatrixType& mat)
76  {
77  using std::abs;
78  int m = mat.rows();
79  int n = mat.cols();
80  eigen_assert((m>0 && m == n) && "Please give a non - empty matrix");
81  m_left.resize(m);
82  m_right.resize(n);
83  m_left.setOnes();
84  m_right.setOnes();
85  m_matrix = mat;
86  VectorXd Dr, Dc, DrRes, DcRes; // Temporary Left and right scaling vectors
87  Dr.resize(m); Dc.resize(n);
88  DrRes.resize(m); DcRes.resize(n);
89  double EpsRow = 1.0, EpsCol = 1.0;
90  int its = 0;
91  do
92  { // Iterate until the infinite norm of each row and column is approximately 1
93  // Get the maximum value in each row and column
94  Dr.setZero(); Dc.setZero();
95  for (int k=0; k<m_matrix.outerSize(); ++k)
96  {
97  for (typename MatrixType::InnerIterator it(m_matrix, k); it; ++it)
98  {
99  if ( Dr(it.row()) < abs(it.value()) )
100  Dr(it.row()) = abs(it.value());
101 
102  if ( Dc(it.col()) < abs(it.value()) )
103  Dc(it.col()) = abs(it.value());
104  }
105  }
106  for (int i = 0; i < m; ++i)
107  {
108  Dr(i) = std::sqrt(Dr(i));
109  }
110  for (int i = 0; i < n; ++i)
111  {
112  Dc(i) = std::sqrt(Dc(i));
113  }
114  // Save the scaling factors
115  for (int i = 0; i < m; ++i)
116  {
117  m_left(i) /= Dr(i);
118  }
119  for (int i = 0; i < n; ++i)
120  {
121  m_right(i) /= Dc(i);
122  }
123  // Scale the rows and the columns of the matrix
124  DrRes.setZero(); DcRes.setZero();
125  for (int k=0; k<m_matrix.outerSize(); ++k)
126  {
127  for (typename MatrixType::InnerIterator it(m_matrix, k); it; ++it)
128  {
129  it.valueRef() = it.value()/( Dr(it.row()) * Dc(it.col()) );
130  // Accumulate the norms of the row and column vectors
131  if ( DrRes(it.row()) < abs(it.value()) )
132  DrRes(it.row()) = abs(it.value());
133 
134  if ( DcRes(it.col()) < abs(it.value()) )
135  DcRes(it.col()) = abs(it.value());
136  }
137  }
138  DrRes.array() = (1-DrRes.array()).abs();
139  EpsRow = DrRes.maxCoeff();
140  DcRes.array() = (1-DcRes.array()).abs();
141  EpsCol = DcRes.maxCoeff();
142  its++;
143  }while ( (EpsRow >m_tol || EpsCol > m_tol) && (its < m_maxits) );
144  m_isInitialized = true;
145  }
151  void computeRef (MatrixType& mat)
152  {
153  compute (mat);
154  mat = m_matrix;
155  }
159  {
160  return m_left;
161  }
162 
166  {
167  return m_right;
168  }
169 
172  void setTolerance(double tol)
173  {
174  m_tol = tol;
175  }
176 
177  protected:
178 
179  void init()
180  {
181  m_tol = 1e-10;
182  m_maxits = 5;
183  m_isInitialized = false;
184  }
185 
189  VectorXd m_left; // Left scaling vector
190  VectorXd m_right; // m_right scaling vector
191  double m_tol;
192  int m_maxits; // Maximum number of iterations allowed
193 };
194 }
195 #endif
Matrix3f m
int n
int i
Array< double, 1, 3 > e(1./3., 0.5, 2.)
#define eigen_assert(x)
MatrixXf mat
iterative scaling algorithm to equilibrate rows and column norms in matrices
Definition: Scaling.h:51
void setTolerance(double tol)
Definition: Scaling.h:172
void compute(const MatrixType &mat)
Definition: Scaling.h:75
VectorXd m_left
Definition: Scaling.h:189
void computeRef(MatrixType &mat)
Definition: Scaling.h:151
MatrixType m_matrix
Definition: Scaling.h:186
MatrixType::Scalar Scalar
Definition: Scaling.h:54
ComputationInfo m_info
Definition: Scaling.h:187
MatrixType_ MatrixType
Definition: Scaling.h:53
bool m_isInitialized
Definition: Scaling.h:188
VectorXd & LeftScaling()
Definition: Scaling.h:158
VectorXd & RightScaling()
Definition: Scaling.h:165
VectorXd m_right
Definition: Scaling.h:190
IterScaling(const MatrixType &matrix)
Definition: Scaling.h:60
MatrixType::Index Index
Definition: Scaling.h:55
Derived & setZero(Index rows, Index cols)
Derived & setOnes(Index rows, Index cols)
constexpr void resize(Index rows, Index cols)
ComputationInfo
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
Eigen::AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(Eigen::internal::remove_all_t< DerType >, typename Eigen::internal::traits< Eigen::internal::remove_all_t< DerType >>::Scalar, product) > sqrt(const Eigen::AutoDiffScalar< DerType > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)
adouble abs(const adouble &x)
Definition: AdolcForward:74