30 template<
typename MatrixType>
class SVD 38 AlignmentMask = int(PacketSize)-1,
39 MinSize = EIGEN_SIZE_MIN_PREFER_DYNAMIC(MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime)
54 : m_matU(matrix.rows(), (std::min)(matrix.rows(), matrix.cols())),
55 m_matV(matrix.cols(),matrix.cols()),
56 m_sigma((std::min)(matrix.rows(),matrix.cols()))
61 template<
typename OtherDerived,
typename ResultType>
64 const MatrixUType& matrixU()
const {
return m_matU; }
65 const SingularValuesType& singularValues()
const {
return m_sigma; }
66 const MatrixVType& matrixV()
const {
return m_matV; }
71 template<
typename UnitaryType,
typename PositiveType>
73 template<
typename PositiveType,
typename UnitaryType>
74 void computePositiveUnitary(PositiveType *positive, UnitaryType *unitary)
const;
75 template<
typename RotationType,
typename ScalingType>
77 template<
typename ScalingType,
typename RotationType>
86 SingularValuesType m_sigma;
93 template<
typename MatrixType>
96 const int m = matrix.rows();
97 const int n = matrix.cols();
98 const int nu = (std::min)(m,n);
99 ei_assert(m>=n &&
"In Eigen 2.0, SVD only works for MxN matrices with M>=N. Sorry!");
100 ei_assert(m>1 &&
"In Eigen 2.0, SVD doesn't work on 1x1 matrices");
104 m_sigma.
resize((std::min)(m,n));
110 const bool wantu =
true;
111 const bool wantv =
true;
116 int nct = (std::min)(m-1,n);
117 int nrt = (std::max)(0,(std::min)(n-2,m));
118 for (k = 0; k < (std::max)(nct,nrt); ++k)
124 m_sigma[k] = matA.col(k).end(m-k).norm();
125 if (m_sigma[k] != 0.0)
128 m_sigma[k] = -m_sigma[k];
129 matA.col(k).end(m-k) /= m_sigma[k];
132 m_sigma[k] = -m_sigma[k];
135 for (j = k+1; j < n; ++j)
137 if ((k < nct) && (m_sigma[k] != 0.0))
140 Scalar t = matA.col(k).end(m-k).eigen2_dot(matA.col(j).end(m-k));
142 matA.col(j).end(m-k) += t * matA.col(k).end(m-k);
151 if (wantu & (k < nct))
152 m_matU.col(k).end(m-k) = matA.col(k).end(m-k);
158 e[k] = e.end(n-k-1).norm();
163 e.end(n-k-1) /= e[k];
167 if ((k+1 < m) & (e[k] != 0.0))
170 work.end(m-k-1) = matA.corner(BottomRight,m-k-1,n-k-1) * e.end(n-k-1);
171 for (j = k+1; j < n; ++j)
172 matA.col(j).end(m-k-1) += (-e[j]/e[k+1]) * work.end(m-k-1);
177 m_matV.col(k).end(n-k-1) = e.end(n-k-1);
183 int p = (std::min)(n,m+1);
185 m_sigma[nct] = matA(nct,nct);
189 e[nrt] = matA(nrt,p-1);
195 for (j = nct; j < nu; ++j)
200 for (k = nct-1; k >= 0; k--)
202 if (m_sigma[k] != 0.0)
204 for (j = k+1; j < nu; ++j)
206 Scalar t = m_matU.col(k).end(m-k).eigen2_dot(m_matU.col(j).end(m-k));
208 m_matU.col(j).end(m-k) += t * m_matU.col(k).end(m-k);
210 m_matU.col(k).end(m-k) = - m_matU.col(k).end(m-k);
211 m_matU(k,k) = Scalar(1) + m_matU(k,k);
213 m_matU.col(k).start(k-1).
setZero();
226 for (k = n-1; k >= 0; k--)
228 if ((k < nrt) & (e[k] != 0.0))
230 for (j = k+1; j < nu; ++j)
232 Scalar t = m_matV.col(k).end(n-k-1).eigen2_dot(m_matV.col(j).end(n-k-1));
233 t = -t/m_matV(k+1,k);
234 m_matV.col(j).end(n-k-1) += t * m_matV.col(k).end(n-k-1);
263 for (k = p-2; k >= -1; --k)
267 if (ei_abs(e[k]) <= eps*(ei_abs(m_sigma[k]) + ei_abs(m_sigma[k+1])))
280 for (ks = p-1; ks >= k; --ks)
284 Scalar t = (ks != p ? ei_abs(e[ks]) : Scalar(0)) + (ks != k+1 ? ei_abs(e[ks-1]) : Scalar(0));
285 if (ei_abs(m_sigma[ks]) <= eps*t)
316 for (j = p-2; j >= k; --j)
318 Scalar t(numext::hypot(m_sigma[j],f));
319 Scalar cs(m_sigma[j]/t);
329 for (i = 0; i < n; ++i)
331 t = cs*m_matV(i,j) + sn*m_matV(i,p-1);
332 m_matV(i,p-1) = -sn*m_matV(i,j) + cs*m_matV(i,p-1);
345 for (j = k; j < p; ++j)
347 Scalar t(numext::hypot(m_sigma[j],f));
348 Scalar cs( m_sigma[j]/t);
355 for (i = 0; i < m; ++i)
357 t = cs*m_matU(i,j) + sn*m_matU(i,k-1);
358 m_matU(i,k-1) = -sn*m_matU(i,j) + cs*m_matU(i,k-1);
370 Scalar scale = (std::max)((std::max)((std::max)((std::max)(
371 ei_abs(m_sigma[p-1]),ei_abs(m_sigma[p-2])),ei_abs(e[p-2])),
372 ei_abs(m_sigma[k])),ei_abs(e[k]));
373 Scalar sp = m_sigma[p-1]/scale;
374 Scalar spm1 = m_sigma[p-2]/scale;
375 Scalar epm1 = e[p-2]/scale;
376 Scalar sk = m_sigma[k]/scale;
377 Scalar ek = e[k]/scale;
378 Scalar b = ((spm1 + sp)*(spm1 - sp) + epm1*epm1)/Scalar(2);
379 Scalar c = (sp*epm1)*(sp*epm1);
381 if ((b != 0.0) || (c != 0.0))
383 shift = ei_sqrt(b*b + c);
386 shift = c/(b + shift);
388 Scalar f = (sk + sp)*(sk - sp) + shift;
393 for (j = k; j < p-1; ++j)
395 Scalar t = numext::hypot(f,g);
400 f = cs*m_sigma[j] + sn*e[j];
401 e[j] = cs*e[j] - sn*m_sigma[j];
403 m_sigma[j+1] = cs*m_sigma[j+1];
406 for (i = 0; i < n; ++i)
408 t = cs*m_matV(i,j) + sn*m_matV(i,j+1);
409 m_matV(i,j+1) = -sn*m_matV(i,j) + cs*m_matV(i,j+1);
413 t = numext::hypot(f,g);
417 f = cs*e[j] + sn*m_sigma[j+1];
418 m_sigma[j+1] = -sn*e[j] + cs*m_sigma[j+1];
421 if (wantu && (j < m-1))
423 for (i = 0; i < m; ++i)
425 t = cs*m_matU(i,j) + sn*m_matU(i,j+1);
426 m_matU(i,j+1) = -sn*m_matU(i,j) + cs*m_matU(i,j+1);
440 if (m_sigma[k] <= 0.0)
442 m_sigma[k] = m_sigma[k] < Scalar(0) ? -m_sigma[k] : Scalar(0);
444 m_matV.col(k).start(pp+1) = -m_matV.col(k).start(pp+1);
450 if (m_sigma[k] >= m_sigma[k+1])
452 Scalar t = m_sigma[k];
453 m_sigma[k] = m_sigma[k+1];
455 if (wantv && (k < n-1))
456 m_matV.col(k).swap(m_matV.col(k+1));
457 if (wantu && (k < m-1))
458 m_matU.col(k).swap(m_matU.col(k+1));
469 template<
typename MatrixType>
472 int mu = m_matU.rows();
473 int mv = m_matV.rows();
474 int n = m_matU.cols();
476 for (
int i=0; i<n; ++i)
479 Scalar p = m_sigma.coeff(i);
481 for (
int j=i+1; j<n; ++j)
483 if (m_sigma.coeff(j) > p)
486 p = m_sigma.coeff(j);
491 m_sigma.coeffRef(k) = m_sigma.coeff(i);
492 m_sigma.coeffRef(i) = p;
495 for(
int s=0; j!=0; ++s, --j)
496 std::swap(m_matU.coeffRef(s,i), m_matU.coeffRef(s,k));
499 for (
int s=0; j!=0; ++s, --j)
500 std::swap(m_matV.coeffRef(s,i), m_matV.coeffRef(s,k));
511 template<
typename MatrixType>
512 template<
typename OtherDerived,
typename ResultType>
515 ei_assert(b.rows() == m_matU.rows());
517 Scalar maxVal = m_sigma.cwise().abs().maxCoeff();
518 for (
int j=0; j<b.cols(); ++j)
522 for (
int i = 0; i <m_matU.cols(); ++i)
524 Scalar si = m_sigma.coeff(i);
525 if (ei_isMuchSmallerThan(ei_abs(si),maxVal))
528 aux.coeffRef(i) /= si;
531 result->col(j) = m_matV * aux;
544 template<
typename MatrixType>
545 template<
typename UnitaryType,
typename PositiveType>
547 PositiveType *positive)
const 549 ei_assert(m_matU.cols() == m_matV.cols() &&
"Polar decomposition is only for square matrices");
550 if(unitary) *unitary = m_matU * m_matV.adjoint();
551 if(positive) *positive = m_matV * m_sigma.asDiagonal() * m_matV.adjoint();
562 template<
typename MatrixType>
563 template<
typename UnitaryType,
typename PositiveType>
565 PositiveType *unitary)
const 567 ei_assert(m_matU.rows() == m_matV.rows() &&
"Polar decomposition is only for square matrices");
568 if(unitary) *unitary = m_matU * m_matV.adjoint();
569 if(positive) *positive = m_matU * m_sigma.asDiagonal() * m_matU.adjoint();
581 template<
typename MatrixType>
582 template<
typename RotationType,
typename ScalingType>
585 ei_assert(m_matU.rows() == m_matV.rows() &&
"Polar decomposition is only for square matrices");
586 Scalar x = (m_matU * m_matV.adjoint()).determinant();
589 if(scaling) scaling->lazyAssign(m_matV * sv.asDiagonal() * m_matV.adjoint());
594 rotation->lazyAssign(m * m_matV.adjoint());
607 template<
typename MatrixType>
608 template<
typename ScalingType,
typename RotationType>
611 ei_assert(m_matU.rows() == m_matV.rows() &&
"Polar decomposition is only for square matrices");
612 Scalar x = (m_matU * m_matV.adjoint()).determinant();
615 if(scaling) scaling->lazyAssign(m_matU * sv.asDiagonal() * m_matU.adjoint());
620 rotation->lazyAssign(m * m_matV.adjoint());
628 template<
typename Derived>
637 #endif // EIGEN2_SVD_H ColXpr col(Index i)
Definition: DenseBase.h:733
iterative scaling algorithm to equilibrate rows and column norms in matrices
Definition: TestIMU_Common.h:87
Holds information about the various numeric (i.e.
Definition: NumTraits.h:88
void computeScalingRotation(ScalingType *positive, RotationType *unitary) const
decomposes the matrix as a product scaling x rotation, the scaling being not necessarily positive...
Definition: SVD.h:609
Definition: GenericPacketMath.h:71
void compute(const MatrixType &matrix)
Computes / recomputes the SVD decomposition A = U S V^* of matrix.
Definition: SVD.h:94
EIGEN_STRONG_INLINE void resize(Index nbRows, Index nbCols)
Resizes *this to a rows x cols matrix.
Definition: PlainObjectBase.h:235
bool solve(const MatrixBase< OtherDerived > &b, ResultType *result) const
Definition: SVD.h:513
Derived & setZero(Index size)
Resizes to the given size, and sets all coefficients in this expression to zero.
Definition: CwiseNullaryOp.h:515
void computeUnitaryPositive(UnitaryType *unitary, PositiveType *positive) const
Computes the polar decomposition of the matrix, as a product unitary x positive.
Definition: SVD.h:546
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
void computeRotationScaling(RotationType *unitary, ScalingType *positive) const
decomposes the matrix as a product rotation x scaling, the scaling being not necessarily positive...
Definition: SVD.h:583
double Scalar
Common scalar type.
Definition: FlexibleKalmanBase.h:48