1c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This file is part of Eigen, a lightweight C++ template library 2c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// for linear algebra. 3c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// 4c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Copyright (C) 2012 Desire NUENTSA WAKAM <desire.nuentsa_wakam@inria.fr 5c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// 6c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This Source Code Form is subject to the terms of the Mozilla 7c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Public License v. 2.0. If a copy of the MPL was not distributed 8c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 9c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 107faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez#ifndef EIGEN_ITERSCALING_H 117faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez#define EIGEN_ITERSCALING_H 12c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/** 13c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * \ingroup IterativeSolvers_Module 14c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * \brief iterative scaling algorithm to equilibrate rows and column norms in matrices 15c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * 16c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * This class can be used as a preprocessing tool to accelerate the convergence of iterative methods 17c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * 18c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * This feature is useful to limit the pivoting amount during LU/ILU factorization 19c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * The scaling strategy as presented here preserves the symmetry of the problem 20c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * NOTE It is assumed that the matrix does not have empty row or column, 21c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * 22c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * Example with key steps 23c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * \code 24c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * VectorXd x(n), b(n); 25c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * SparseMatrix<double> A; 26c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * // fill A and b; 277faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez * IterScaling<SparseMatrix<double> > scal; 28c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * // Compute the left and right scaling vectors. The matrix is equilibrated at output 29c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * scal.computeRef(A); 30c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * // Scale the right hand side 31c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * b = scal.LeftScaling().cwiseProduct(b); 32c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * // Now, solve the equilibrated linear system with any available solver 33c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * 34c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * // Scale back the computed solution 35c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * x = scal.RightScaling().cwiseProduct(x); 36c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * \endcode 37c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * 38c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * \tparam _MatrixType the type of the matrix. It should be a real square sparsematrix 39c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * 40c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * References : D. Ruiz and B. Ucar, A Symmetry Preserving Algorithm for Matrix Scaling, INRIA Research report RR-7552 41c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * 42c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * \sa \ref IncompleteLUT 43c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath */ 447faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandeznamespace Eigen { 45c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathusing std::abs; 46c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename _MatrixType> 477faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezclass IterScaling 48c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{ 49c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath public: 50c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath typedef _MatrixType MatrixType; 51c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath typedef typename MatrixType::Scalar Scalar; 52c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath typedef typename MatrixType::Index Index; 53c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 54c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath public: 557faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez IterScaling() { init(); } 56c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 577faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez IterScaling(const MatrixType& matrix) 58c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath { 59c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath init(); 60c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath compute(matrix); 61c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath } 62c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 637faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez ~IterScaling() { } 64c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 65c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /** 66c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * Compute the left and right diagonal matrices to scale the input matrix @p mat 67c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * 68c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * FIXME This algorithm will be modified such that the diagonal elements are permuted on the diagonal. 69c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * 70c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * \sa LeftScaling() RightScaling() 71c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath */ 72c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath void compute (const MatrixType& mat) 73c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath { 74c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath int m = mat.rows(); 75c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath int n = mat.cols(); 767faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez eigen_assert((m>0 && m == n) && "Please give a non - empty matrix"); 77c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath m_left.resize(m); 78c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath m_right.resize(n); 79c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath m_left.setOnes(); 80c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath m_right.setOnes(); 81c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath m_matrix = mat; 82c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath VectorXd Dr, Dc, DrRes, DcRes; // Temporary Left and right scaling vectors 83c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath Dr.resize(m); Dc.resize(n); 84c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath DrRes.resize(m); DcRes.resize(n); 85c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath double EpsRow = 1.0, EpsCol = 1.0; 86c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath int its = 0; 87c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath do 88c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath { // Iterate until the infinite norm of each row and column is approximately 1 89c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath // Get the maximum value in each row and column 90c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath Dr.setZero(); Dc.setZero(); 91c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath for (int k=0; k<m_matrix.outerSize(); ++k) 92c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath { 93c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath for (typename MatrixType::InnerIterator it(m_matrix, k); it; ++it) 94c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath { 95c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath if ( Dr(it.row()) < abs(it.value()) ) 96c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath Dr(it.row()) = abs(it.value()); 97c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 98c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath if ( Dc(it.col()) < abs(it.value()) ) 99c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath Dc(it.col()) = abs(it.value()); 100c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath } 101c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath } 102c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath for (int i = 0; i < m; ++i) 103c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath { 104c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath Dr(i) = std::sqrt(Dr(i)); 105c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath Dc(i) = std::sqrt(Dc(i)); 106c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath } 107c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath // Save the scaling factors 108c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath for (int i = 0; i < m; ++i) 109c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath { 110c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath m_left(i) /= Dr(i); 111c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath m_right(i) /= Dc(i); 112c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath } 113c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath // Scale the rows and the columns of the matrix 114c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath DrRes.setZero(); DcRes.setZero(); 115c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath for (int k=0; k<m_matrix.outerSize(); ++k) 116c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath { 117c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath for (typename MatrixType::InnerIterator it(m_matrix, k); it; ++it) 118c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath { 119c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath it.valueRef() = it.value()/( Dr(it.row()) * Dc(it.col()) ); 120c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath // Accumulate the norms of the row and column vectors 121c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath if ( DrRes(it.row()) < abs(it.value()) ) 122c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath DrRes(it.row()) = abs(it.value()); 123c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 124c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath if ( DcRes(it.col()) < abs(it.value()) ) 125c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath DcRes(it.col()) = abs(it.value()); 126c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath } 127c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath } 128c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath DrRes.array() = (1-DrRes.array()).abs(); 129c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath EpsRow = DrRes.maxCoeff(); 130c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath DcRes.array() = (1-DcRes.array()).abs(); 131c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath EpsCol = DcRes.maxCoeff(); 132c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath its++; 133c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath }while ( (EpsRow >m_tol || EpsCol > m_tol) && (its < m_maxits) ); 134c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath m_isInitialized = true; 135c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath } 136c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /** Compute the left and right vectors to scale the vectors 137c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * the input matrix is scaled with the computed vectors at output 138c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * 139c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath * \sa compute() 140c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath */ 141c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath void computeRef (MatrixType& mat) 142c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath { 143c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath compute (mat); 144c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath mat = m_matrix; 145c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath } 146c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /** Get the vector to scale the rows of the matrix 147c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath */ 148c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath VectorXd& LeftScaling() 149c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath { 150c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath return m_left; 151c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath } 152c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 153c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /** Get the vector to scale the columns of the matrix 154c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath */ 155c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath VectorXd& RightScaling() 156c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath { 157c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath return m_right; 158c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath } 159c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 160c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath /** Set the tolerance for the convergence of the iterative scaling algorithm 161c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath */ 162c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath void setTolerance(double tol) 163c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath { 164c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath m_tol = tol; 165c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath } 166c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 167c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath protected: 168c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 169c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath void init() 170c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath { 171c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath m_tol = 1e-10; 172c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath m_maxits = 5; 173c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath m_isInitialized = false; 174c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath } 175c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath 176c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath MatrixType m_matrix; 177c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath mutable ComputationInfo m_info; 178c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath bool m_isInitialized; 179c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath VectorXd m_left; // Left scaling vector 180c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath VectorXd m_right; // m_right scaling vector 181c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath double m_tol; 182c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath int m_maxits; // Maximum number of iterations allowed 183c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}; 1847faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez} 1857faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez#endif 186