Eigen-unsupported  5.0.1-dev+284dcc12
 
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
10#ifndef EIGEN_ITERSCALING_H
11#define EIGEN_ITERSCALING_H
12
13// IWYU pragma: private
14#include "./InternalHeaderCheck.h"
15
16namespace Eigen {
17
50template <typename MatrixType_>
51class 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
60 IterScaling(const MatrixType& matrix) {
61 init();
62 compute(matrix);
63 }
64
65 ~IterScaling() {}
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 }
135
140 void computeRef(MatrixType& mat) {
141 compute(mat);
142 mat = m_matrix;
143 }
144
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
163 MatrixType m_matrix;
164 mutable ComputationInfo m_info;
165 bool m_isInitialized;
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
void setTolerance(double tol)
Definition Scaling.h:154
void compute(const MatrixType &mat)
Definition Scaling.h:74
VectorXd & LeftScaling()
Definition Scaling.h:146
void computeRef(MatrixType &mat)
Definition Scaling.h:140
VectorXd & RightScaling()
Definition Scaling.h:150
Derived & setZero(Index rows, Index cols)
constexpr void resize(Index rows, Index cols)
Matrix< double, Dynamic, 1 > VectorXd
Namespace containing all symbols from the Eigen library.
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)