10 #ifndef EIGEN_MATRIX_SQUARE_ROOT 11 #define EIGEN_MATRIX_SQUARE_ROOT 19 template <
typename MatrixType,
typename ResultType>
27 sqrtT.template block<2,2>(i,i)
28 = (es.eigenvectors() * es.eigenvalues().cwiseSqrt().asDiagonal() * es.eigenvectors().inverse()).
real();
34 template <
typename MatrixType,
typename ResultType>
38 Scalar tmp = (sqrtT.row(i).segment(i+1,j-i-1) * sqrtT.col(j).segment(i+1,j-i-1)).value();
39 sqrtT.coeffRef(i,j) = (T.coeff(i,j) - tmp) / (sqrtT.coeff(i,i) + sqrtT.coeff(j,j));
43 template <
typename MatrixType,
typename ResultType>
49 rhs -= sqrtT.block(i, i+1, 1, j-i-1) * sqrtT.block(i+1, j, j-i-1, 2);
51 A += sqrtT.template block<2,2>(j,j).transpose();
52 sqrtT.template block<1,2>(i,j).transpose() = A.fullPivLu().solve(rhs.transpose());
56 template <
typename MatrixType,
typename ResultType>
62 rhs -= sqrtT.block(i, i+2, 2, j-i-2) * sqrtT.block(i+2, j, j-i-2, 1);
64 A += sqrtT.template block<2,2>(i,i);
65 sqrtT.template block<2,1>(i,j) = A.fullPivLu().solve(rhs);
69 template <
typename MatrixType>
74 coeffMatrix.
coeffRef(0,0) = A.coeff(0,0) + B.coeff(0,0);
75 coeffMatrix.
coeffRef(1,1) = A.coeff(0,0) + B.coeff(1,1);
76 coeffMatrix.
coeffRef(2,2) = A.coeff(1,1) + B.coeff(0,0);
77 coeffMatrix.
coeffRef(3,3) = A.coeff(1,1) + B.coeff(1,1);
78 coeffMatrix.
coeffRef(0,1) = B.coeff(1,0);
79 coeffMatrix.
coeffRef(0,2) = A.coeff(0,1);
80 coeffMatrix.
coeffRef(1,0) = B.coeff(0,1);
81 coeffMatrix.
coeffRef(1,3) = A.coeff(0,1);
82 coeffMatrix.
coeffRef(2,0) = A.coeff(1,0);
83 coeffMatrix.
coeffRef(2,3) = B.coeff(1,0);
84 coeffMatrix.
coeffRef(3,1) = A.coeff(1,0);
85 coeffMatrix.
coeffRef(3,2) = B.coeff(0,1);
94 result = coeffMatrix.fullPivLu().solve(rhs);
97 X.coeffRef(0,1) = result.
coeff(1);
98 X.coeffRef(1,0) = result.
coeff(2);
99 X.coeffRef(1,1) = result.
coeff(3);
103 template <
typename MatrixType,
typename ResultType>
111 C -= sqrtT.block(i, i+2, 2, j-i-2) * sqrtT.block(i+2, j, j-i-2, 2);
114 sqrtT.template block<2,2>(i,j) = X;
119 template <
typename MatrixType,
typename ResultType>
125 if (i == size - 1 || T.coeff(i+1, i) == 0) {
127 sqrtT.coeffRef(i,i) =
sqrt(T.coeff(i,i));
138 template <
typename MatrixType,
typename ResultType>
143 if (T.coeff(j, j-1) != 0)
145 for (
Index i = j-1; i >= 0; i--) {
146 if (i > 0 && T.coeff(i, i-1) != 0)
148 bool iBlockIs2x2 = (i < size - 1) && (T.coeff(i+1, i) != 0);
149 bool jBlockIs2x2 = (j < size - 1) && (T.coeff(j+1, j) != 0);
150 if (iBlockIs2x2 && jBlockIs2x2)
152 else if (iBlockIs2x2 && !jBlockIs2x2)
154 else if (!iBlockIs2x2 && jBlockIs2x2)
156 else if (!iBlockIs2x2 && !jBlockIs2x2)
179 template <
typename MatrixType,
typename ResultType>
183 result.resize(arg.rows(), arg.cols());
203 template <
typename MatrixType,
typename ResultType>
213 result.resize(arg.rows(), arg.cols());
214 for (
Index i = 0; i < arg.rows(); i++) {
215 result.coeffRef(i,i) =
sqrt(arg.coeff(i,i));
217 for (
Index j = 1; j < arg.cols(); j++) {
218 for (
Index i = j-1; i >= 0; i--) {
220 Scalar tmp = (result.row(i).segment(i+1,j-i-1) * result.col(j).segment(i+1,j-i-1)).value();
222 result.coeffRef(i,j) = (arg.coeff(i,j) - tmp) / (result.coeff(i,i) + result.coeff(j,j));
247 template <
typename ResultType>
static void run(
const MatrixType &
arg, ResultType &result);
253 template <
typename MatrixType>
256 template <
typename ResultType>
267 MatrixType sqrtT = MatrixType::Zero(arg.rows(), arg.cols());
271 result = U * sqrtT * U.adjoint();
278 template <
typename MatrixType>
281 template <
typename ResultType>
296 result = U * (sqrtT.template triangularView<Upper>() * U.adjoint());
315 :
public ReturnByValue<MatrixSquareRootReturnValue<Derived> >
333 template <
typename ResultType>
334 inline void evalTo(ResultType& result)
const 338 DerivedEvalType tmp(m_src);
350 template<
typename Derived>
357 template <
typename Derived>
366 #endif // EIGEN_MATRIX_FUNCTION internal::ref_selector< Derived >::type DerivedNested
const DerivedNested m_src
Proxy for the matrix square root of some matrix (expression).
EIGEN_DEVICE_FUNC const SqrtReturnType sqrt() const
static constexpr size_t size(Tuple< Args... > &)
Provides access to the number of elements in a tuple as a compile-time constant expression.
void matrix_sqrt_quasi_triangular_solve_auxiliary_equation(MatrixType &X, const MatrixType &A, const MatrixType &B, const MatrixType &C)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(Index rowId, Index colId)
MatrixType A(a, *n, *n, *lda)
Map< Matrix< Scalar, Dynamic, Dynamic, ColMajor >, 0, OuterStride<> > MatrixType
void matrix_sqrt_quasi_triangular_diagonal(const MatrixType &T, ResultType &sqrtT)
void matrix_sqrt_quasi_triangular_1x2_off_diagonal_block(const MatrixType &T, typename MatrixType::Index i, typename MatrixType::Index j, ResultType &sqrtT)
Derived::PlainObject ReturnType
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & coeff(Index rowId, Index colId) const
void matrix_sqrt_quasi_triangular_off_diagonal(const MatrixType &T, ResultType &sqrtT)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const ArgReturnType arg() const
void matrix_sqrt_quasi_triangular_1x1_off_diagonal_block(const MatrixType &T, typename MatrixType::Index i, typename MatrixType::Index j, ResultType &sqrtT)
void evalTo(ResultType &result) const
Compute the matrix square root.
static void run(const MatrixType &arg, ResultType &result)
Compute the matrix square root.
static void run(const MatrixType &arg, ResultType &result)
EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL EIGEN_DEVICE_FUNC BlockXpr block(Index startRow, Index startCol, Index blockRows, Index blockCols)
This is the const version of block(Index,Index,Index,Index). */.
static void run(const MatrixType &arg, ResultType &result)
MatrixSquareRootReturnValue(const Derived &src)
Constructor.
Helper struct for computing matrix square roots of general matrices.
void matrix_sqrt_quasi_triangular(const MatrixType &arg, ResultType &result)
Compute matrix square root of quasi-triangular matrix.
void matrix_sqrt_triangular(const MatrixType &arg, ResultType &result)
Compute matrix square root of triangular matrix.
MatrixType B(b, *n, *nrhs, *ldb)
Computes eigenvalues and eigenvectors of general matrices.
void matrix_sqrt_quasi_triangular_2x1_off_diagonal_block(const MatrixType &T, typename MatrixType::Index i, typename MatrixType::Index j, ResultType &sqrtT)
The matrix class, also used for vectors and row-vectors.
void run(Expr &expr, Dev &dev)
const AutoDiffScalar< DerType > & real(const AutoDiffScalar< DerType > &x)
void matrix_sqrt_quasi_triangular_2x2_off_diagonal_block(const MatrixType &T, typename MatrixType::Index i, typename MatrixType::Index j, ResultType &sqrtT)
void matrix_sqrt_quasi_triangular_2x2_diagonal_block(const MatrixType &T, typename MatrixType::Index i, ResultType &sqrtT)