Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
Scaling.h
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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_ITERSCALING_H
12#define EIGEN_ITERSCALING_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
51template <typename MatrixType_>
52class IterScaling {
53 public:
54 using MatrixType = MatrixType_;
55 using Scalar = typename MatrixType::Scalar;
56 using Index = typename MatrixType::Index;
57
58 IterScaling() { init(); }
59
60 IterScaling(const MatrixType& matrix) {
61 init();
62 compute(matrix);
63 }
64
72 void compute(const MatrixType& mat) {
73 int m = mat.rows();
74 int n = mat.cols();
75 eigen_assert((m > 0 && m == n) && "Please give a non - empty matrix");
76 m_left.resize(m);
77 m_right.resize(n);
78 m_left.setOnes();
79 m_right.setOnes();
80 m_matrix = mat;
81 // Temporary left and right scaling vectors
82 VectorXd Dr(m), Dc(n), DrRes(m), DcRes(n);
83 double EpsRow = 1.0, EpsCol = 1.0;
84 int its = 0;
85 do { // Iterate until the infinite norm of each row and column is approximately 1
86 // Get the maximum value in each row and column
87 Dr.setZero();
88 Dc.setZero();
89 for (int k = 0; k < m_matrix.outerSize(); ++k) {
90 for (typename MatrixType::InnerIterator it(m_matrix, k); it; ++it) {
91 if (Dr(it.row()) < numext::abs(it.value())) Dr(it.row()) = numext::abs(it.value());
92
93 if (Dc(it.col()) < numext::abs(it.value())) Dc(it.col()) = numext::abs(it.value());
94 }
95 }
96 for (int i = 0; i < m; ++i) {
97 Dr(i) = numext::sqrt(Dr(i));
98 }
99 for (int i = 0; i < n; ++i) {
100 Dc(i) = numext::sqrt(Dc(i));
101 }
102 // Save the scaling factors
103 for (int i = 0; i < m; ++i) {
104 m_left(i) /= Dr(i);
105 }
106 for (int i = 0; i < n; ++i) {
107 m_right(i) /= Dc(i);
108 }
109 // Scale the rows and the columns of the matrix
110 DrRes.setZero();
111 DcRes.setZero();
112 for (int k = 0; k < m_matrix.outerSize(); ++k) {
113 for (typename MatrixType::InnerIterator it(m_matrix, k); it; ++it) {
114 it.valueRef() = it.value() / (Dr(it.row()) * Dc(it.col()));
115 // Accumulate the norms of the row and column vectors
116 if (DrRes(it.row()) < numext::abs(it.value())) DrRes(it.row()) = numext::abs(it.value());
117
118 if (DcRes(it.col()) < numext::abs(it.value())) DcRes(it.col()) = numext::abs(it.value());
119 }
120 }
121 DrRes.array() = (1 - DrRes.array()).abs();
122 EpsRow = DrRes.maxCoeff();
123 DcRes.array() = (1 - DcRes.array()).abs();
124 EpsCol = DcRes.maxCoeff();
125 its++;
126 } while ((EpsRow > m_tol || EpsCol > m_tol) && (its < m_maxits));
127 m_isInitialized = true;
128 }
129
134 void computeRef(MatrixType& mat) {
135 compute(mat);
136 mat = m_matrix;
137 }
138
140 VectorXd& LeftScaling() { return m_left; }
141
144 VectorXd& RightScaling() { return m_right; }
145
148 void setTolerance(double tol) { m_tol = tol; }
149
150 protected:
151 void init() {
152 m_tol = 1e-10;
153 m_maxits = 5;
154 m_isInitialized = false;
155 }
156
157 MatrixType m_matrix;
158 bool m_isInitialized;
159 VectorXd m_left; // Left scaling vector
160 VectorXd m_right; // Right scaling vector
161 double m_tol;
162 int m_maxits; // Maximum number of iterations allowed
163};
164} // namespace Eigen
165#endif
void setTolerance(double tol)
Definition Scaling.h:148
void compute(const MatrixType &mat)
Definition Scaling.h:72
VectorXd & LeftScaling()
Definition Scaling.h:140
void computeRef(MatrixType &mat)
Definition Scaling.h:134
VectorXd & RightScaling()
Definition Scaling.h:144
Derived & setZero(Index size)
Definition CwiseNullaryOp.h:536
Matrix< double, Dynamic, 1 > VectorXd
DynamicĂ—1 vector of type double.
Definition Matrix.h:489