xref: /aosp_15_r20/external/eigen/unsupported/test/matrix_functions.h (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1*bf2c3715SXin Li // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 2009-2011 Jitse Niesen <[email protected]>
5*bf2c3715SXin Li //
6*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
7*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
8*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9*bf2c3715SXin Li 
10*bf2c3715SXin Li #include "main.h"
11*bf2c3715SXin Li #include <unsupported/Eigen/MatrixFunctions>
12*bf2c3715SXin Li 
13*bf2c3715SXin Li // For complex matrices, any matrix is fine.
14*bf2c3715SXin Li template<typename MatrixType, int IsComplex = NumTraits<typename internal::traits<MatrixType>::Scalar>::IsComplex>
15*bf2c3715SXin Li struct processTriangularMatrix
16*bf2c3715SXin Li {
runprocessTriangularMatrix17*bf2c3715SXin Li   static void run(MatrixType&, MatrixType&, const MatrixType&)
18*bf2c3715SXin Li   { }
19*bf2c3715SXin Li };
20*bf2c3715SXin Li 
21*bf2c3715SXin Li // For real matrices, make sure none of the eigenvalues are negative.
22*bf2c3715SXin Li template<typename MatrixType>
23*bf2c3715SXin Li struct processTriangularMatrix<MatrixType,0>
24*bf2c3715SXin Li {
25*bf2c3715SXin Li   static void run(MatrixType& m, MatrixType& T, const MatrixType& U)
26*bf2c3715SXin Li   {
27*bf2c3715SXin Li     const Index size = m.cols();
28*bf2c3715SXin Li 
29*bf2c3715SXin Li     for (Index i=0; i < size; ++i) {
30*bf2c3715SXin Li       if (i == size - 1 || T.coeff(i+1,i) == 0)
31*bf2c3715SXin Li         T.coeffRef(i,i) = std::abs(T.coeff(i,i));
32*bf2c3715SXin Li       else
33*bf2c3715SXin Li         ++i;
34*bf2c3715SXin Li     }
35*bf2c3715SXin Li     m = U * T * U.transpose();
36*bf2c3715SXin Li   }
37*bf2c3715SXin Li };
38*bf2c3715SXin Li 
39*bf2c3715SXin Li template <typename MatrixType, int IsComplex = NumTraits<typename internal::traits<MatrixType>::Scalar>::IsComplex>
40*bf2c3715SXin Li struct generateTestMatrix;
41*bf2c3715SXin Li 
42*bf2c3715SXin Li template <typename MatrixType>
43*bf2c3715SXin Li struct generateTestMatrix<MatrixType,0>
44*bf2c3715SXin Li {
45*bf2c3715SXin Li   static void run(MatrixType& result, typename MatrixType::Index size)
46*bf2c3715SXin Li   {
47*bf2c3715SXin Li     result = MatrixType::Random(size, size);
48*bf2c3715SXin Li     RealSchur<MatrixType> schur(result);
49*bf2c3715SXin Li     MatrixType T = schur.matrixT();
50*bf2c3715SXin Li     processTriangularMatrix<MatrixType>::run(result, T, schur.matrixU());
51*bf2c3715SXin Li   }
52*bf2c3715SXin Li };
53*bf2c3715SXin Li 
54*bf2c3715SXin Li template <typename MatrixType>
55*bf2c3715SXin Li struct generateTestMatrix<MatrixType,1>
56*bf2c3715SXin Li {
57*bf2c3715SXin Li   static void run(MatrixType& result, typename MatrixType::Index size)
58*bf2c3715SXin Li   {
59*bf2c3715SXin Li     result = MatrixType::Random(size, size);
60*bf2c3715SXin Li   }
61*bf2c3715SXin Li };
62*bf2c3715SXin Li 
63*bf2c3715SXin Li template <typename Derived, typename OtherDerived>
64*bf2c3715SXin Li typename Derived::RealScalar relerr(const MatrixBase<Derived>& A, const MatrixBase<OtherDerived>& B)
65*bf2c3715SXin Li {
66*bf2c3715SXin Li   return std::sqrt((A - B).cwiseAbs2().sum() / (std::min)(A.cwiseAbs2().sum(), B.cwiseAbs2().sum()));
67*bf2c3715SXin Li }
68