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 namespace Eigen {
14 
47 template<typename _MatrixType>
49 {
50  public:
51  typedef _MatrixType MatrixType;
52  typedef typename MatrixType::Scalar Scalar;
53  typedef typename MatrixType::Index Index;
54 
55  public:
56  IterScaling() { init(); }
57 
59  {
60  init();
61  compute(matrix);
62  }
63 
65 
73  void compute (const MatrixType& mat)
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); Dc.resize(n);
86  DrRes.resize(m); DcRes.resize(n);
87  double EpsRow = 1.0, EpsCol = 1.0;
88  int its = 0;
89  do
90  { // Iterate until the infinite norm of each row and column is approximately 1
91  // Get the maximum value in each row and column
92  Dr.setZero(); Dc.setZero();
93  for (int k=0; k<m_matrix.outerSize(); ++k)
94  {
95  for (typename MatrixType::InnerIterator it(m_matrix, k); it; ++it)
96  {
97  if ( Dr(it.row()) < abs(it.value()) )
98  Dr(it.row()) = abs(it.value());
99 
100  if ( Dc(it.col()) < abs(it.value()) )
101  Dc(it.col()) = abs(it.value());
102  }
103  }
104  for (int i = 0; i < m; ++i)
105  {
106  Dr(i) = std::sqrt(Dr(i));
107  }
108  for (int i = 0; i < n; ++i)
109  {
110  Dc(i) = std::sqrt(Dc(i));
111  }
112  // Save the scaling factors
113  for (int i = 0; i < m; ++i)
114  {
115  m_left(i) /= Dr(i);
116  }
117  for (int i = 0; i < n; ++i)
118  {
119  m_right(i) /= Dc(i);
120  }
121  // Scale the rows and the columns of the matrix
122  DrRes.setZero(); DcRes.setZero();
123  for (int k=0; k<m_matrix.outerSize(); ++k)
124  {
125  for (typename MatrixType::InnerIterator it(m_matrix, k); it; ++it)
126  {
127  it.valueRef() = it.value()/( Dr(it.row()) * Dc(it.col()) );
128  // Accumulate the norms of the row and column vectors
129  if ( DrRes(it.row()) < abs(it.value()) )
130  DrRes(it.row()) = abs(it.value());
131 
132  if ( DcRes(it.col()) < abs(it.value()) )
133  DcRes(it.col()) = abs(it.value());
134  }
135  }
136  DrRes.array() = (1-DrRes.array()).abs();
137  EpsRow = DrRes.maxCoeff();
138  DcRes.array() = (1-DcRes.array()).abs();
139  EpsCol = DcRes.maxCoeff();
140  its++;
141  }while ( (EpsRow >m_tol || EpsCol > m_tol) && (its < m_maxits) );
142  m_isInitialized = true;
143  }
150  {
151  compute (mat);
152  mat = m_matrix;
153  }
156  VectorXd& LeftScaling()
157  {
158  return m_left;
159  }
160 
163  VectorXd& RightScaling()
164  {
165  return m_right;
166  }
167 
170  void setTolerance(double tol)
171  {
172  m_tol = tol;
173  }
174 
175  protected:
176 
177  void init()
178  {
179  m_tol = 1e-10;
180  m_maxits = 5;
181  m_isInitialized = false;
182  }
183 
187  VectorXd m_left; // Left scaling vector
188  VectorXd m_right; // m_right scaling vector
189  double m_tol;
190  int m_maxits; // Maximum number of iterations allowed
191 };
192 }
193 #endif
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Eigen::IterScaling::m_info
ComputationInfo m_info
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:185
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
Eigen::IterScaling::m_maxits
int m_maxits
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:190
eigen_assert
#define eigen_assert(x)
Definition: Macros.h:1037
Eigen::IterScaling::m_tol
double m_tol
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:189
mat
MatrixXf mat
Definition: Tutorial_AdvancedInitialization_CommaTemporary.cpp:1
Eigen::IterScaling::IterScaling
IterScaling()
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:56
Eigen::IterScaling::LeftScaling
VectorXd & LeftScaling()
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:156
Eigen::IterScaling
iterative scaling algorithm to equilibrate rows and column norms in matrices
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:48
n
int n
Definition: BiCGSTAB_simple.cpp:1
Eigen::IterScaling::IterScaling
IterScaling(const MatrixType &matrix)
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:58
Eigen::IterScaling::~IterScaling
~IterScaling()
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:64
Eigen::IterScaling::RightScaling
VectorXd & RightScaling()
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:163
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
matrix
Map< Matrix< T, Dynamic, Dynamic, ColMajor >, 0, OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: gtsam/3rdparty/Eigen/blas/common.h:110
Eigen::IterScaling::compute
void compute(const MatrixType &mat)
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:73
Eigen::IterScaling::setTolerance
void setTolerance(double tol)
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:170
Eigen::IterScaling::computeRef
void computeRef(MatrixType &mat)
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:149
Eigen::IterScaling::Scalar
MatrixType::Scalar Scalar
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:52
Eigen::IterScaling::MatrixType
_MatrixType MatrixType
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:51
gtsam::tol
const G double tol
Definition: Group.h:79
abs
#define abs(x)
Definition: datatypes.h:17
Eigen::IterScaling::init
void init()
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:177
Eigen::IterScaling::Index
MatrixType::Index Index
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:53
Eigen::ComputationInfo
ComputationInfo
Definition: Constants.h:440
Eigen::IterScaling::m_matrix
MatrixType m_matrix
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:184
Eigen::IterScaling::m_right
VectorXd m_right
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:188
ceres::sqrt
Jet< T, N > sqrt(const Jet< T, N > &f)
Definition: jet.h:418
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Eigen::IterScaling::m_isInitialized
bool m_isInitialized
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:186
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
Eigen::IterScaling::m_left
VectorXd m_left
Definition: unsupported/Eigen/src/IterativeSolvers/Scaling.h:187


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:35:01