10 #ifndef EIGEN_MATRIX_SQUARE_ROOT
11 #define EIGEN_MATRIX_SQUARE_ROOT
21 template <
typename MatrixType,
typename ResultType>
29 sqrtT.template block<2,2>(
i,
i)
30 = (
es.eigenvectors() *
es.eigenvalues().cwiseSqrt().asDiagonal() *
es.eigenvectors().inverse()).
real();
36 template <
typename MatrixType,
typename ResultType>
40 Scalar tmp = (sqrtT.row(
i).segment(
i+1,
j-
i-1) * sqrtT.col(
j).segment(
i+1,
j-
i-1)).value();
41 sqrtT.coeffRef(
i,
j) = (
T.coeff(
i,
j) - tmp) / (sqrtT.coeff(
i,
i) + sqrtT.coeff(
j,
j));
45 template <
typename MatrixType,
typename ResultType>
51 rhs -= sqrtT.block(
i,
i+1, 1,
j-
i-1) * sqrtT.block(
i+1,
j,
j-
i-1, 2);
54 sqrtT.template block<1,2>(
i,
j).transpose() =
A.fullPivLu().solve(rhs.
transpose());
58 template <
typename MatrixType,
typename ResultType>
64 rhs -= sqrtT.block(
i,
i+2, 2,
j-
i-2) * sqrtT.block(
i+2,
j,
j-
i-2, 1);
66 A += sqrtT.template block<2,2>(
i,
i);
67 sqrtT.template block<2,1>(
i,
j) =
A.fullPivLu().solve(rhs);
71 template <
typename MatrixType>
96 result = coeffMatrix.
fullPivLu().solve(rhs);
98 X.coeffRef(0,0) = result.
coeff(0);
99 X.coeffRef(0,1) = result.
coeff(1);
100 X.coeffRef(1,0) = result.
coeff(2);
101 X.coeffRef(1,1) = result.
coeff(3);
105 template <
typename MatrixType,
typename ResultType>
113 C -= sqrtT.block(
i,
i+2, 2,
j-
i-2) * sqrtT.block(
i+2,
j,
j-
i-2, 2);
116 sqrtT.template block<2,2>(
i,
j) =
X;
121 template <
typename MatrixType,
typename ResultType>
127 if (
i ==
size - 1 ||
T.coeff(
i+1,
i) == 0) {
129 sqrtT.coeffRef(
i,
i) =
sqrt(
T.coeff(
i,
i));
140 template <
typename MatrixType,
typename ResultType>
145 if (
T.coeff(
j,
j-1) != 0)
148 if (
i > 0 &&
T.coeff(
i,
i-1) != 0)
150 bool iBlockIs2x2 = (
i <
size - 1) && (
T.coeff(
i+1,
i) != 0);
151 bool jBlockIs2x2 = (
j <
size - 1) && (
T.coeff(
j+1,
j) != 0);
152 if (iBlockIs2x2 && jBlockIs2x2)
154 else if (iBlockIs2x2 && !jBlockIs2x2)
156 else if (!iBlockIs2x2 && jBlockIs2x2)
158 else if (!iBlockIs2x2 && !jBlockIs2x2)
181 template <
typename MatrixType,
typename ResultType>
185 result.resize(
arg.rows(),
arg.cols());
205 template <
typename MatrixType,
typename ResultType>
209 typedef typename MatrixType::Scalar Scalar;
215 result.resize(
arg.rows(),
arg.cols());
222 Scalar tmp = (result.row(
i).segment(
i+1,
j-
i-1) * result.col(
j).segment(
i+1,
j-
i-1)).value();
224 result.coeffRef(
i,
j) = (
arg.coeff(
i,
j) - tmp) / (result.coeff(
i,
i) + result.coeff(
j,
j));
239 template <typename MatrixType, int IsComplex = NumTraits<typename internal::traits<MatrixType>::Scalar>::IsComplex>
240 struct matrix_sqrt_compute
249 template <
typename ResultType>
static void run(
const MatrixType &
arg, ResultType &result);
255 template <
typename MatrixType>
258 typedef typename MatrixType::PlainObject PlainType;
259 template <
typename ResultType>
267 const PlainType& U =
schurOfA.matrixU();
270 PlainType sqrtT = PlainType::Zero(
arg.rows(),
arg.cols());
274 result = U * sqrtT * U.adjoint();
281 template <
typename MatrixType>
284 typedef typename MatrixType::PlainObject PlainType;
285 template <
typename ResultType>
293 const PlainType& U =
schurOfA.matrixU();
300 result = U * (sqrtT.template triangularView<Upper>() * U.adjoint());
318 template<
typename Derived>
class MatrixSquareRootReturnValue
319 :
public ReturnByValue<MatrixSquareRootReturnValue<Derived> >
322 typedef typename internal::ref_selector<Derived>::type DerivedNested;
330 explicit MatrixSquareRootReturnValue(
const Derived& src) : m_src(src) { }
337 template <
typename ResultType>
338 inline void evalTo(ResultType& result)
const
340 typedef typename internal::nested_eval<Derived, 10>::type DerivedEvalType;
341 typedef internal::remove_all_t<DerivedEvalType> DerivedEvalTypeClean;
342 DerivedEvalType tmp(m_src);
343 internal::matrix_sqrt_compute<DerivedEvalTypeClean>::run(tmp, result);
346 Index rows()
const {
return m_src.rows(); }
347 Index cols()
const {
return m_src.cols(); }
350 const DerivedNested m_src;
354 template<
typename Derived>
355 struct traits<MatrixSquareRootReturnValue<Derived> >
357 typedef typename Derived::PlainObject ReturnType;
361 template <
typename Derived>
365 return MatrixSquareRootReturnValue<Derived>(derived());
SparseMatrix< double > A(n, n)
EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL FixedBlockXpr< NRows, NCols >::Type block(Index startRow, Index startCol)
cout<< "Here is a random 4x4 matrix, A:"<< endl<< A<< endl<< endl;ComplexSchur< MatrixXcf > schurOfA(A, false)
EigenSolver< MatrixXf > es
Matrix< float, 1, Dynamic > MatrixType
static const ConstantReturnType Zero()
TransposeReturnType transpose()
static const IdentityReturnType Identity()
const MatrixSquareRootReturnValue< Derived > sqrt() const
const FullPivLU< PlainObject, PermutationIndex > fullPivLu() const
constexpr Scalar & coeffRef(Index index)
const Scalar & coeff(Index index) const
TransposeReturnType transpose()
Scalar coeff(Index row, Index col) const
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.
void matrix_sqrt_quasi_triangular_1x2_off_diagonal_block(const MatrixType &T, Index i, Index j, ResultType &sqrtT)
void matrix_sqrt_quasi_triangular_solve_auxiliary_equation(MatrixType &X, const MatrixType &A, const MatrixType &B, const MatrixType &C)
void matrix_sqrt_quasi_triangular_diagonal(const MatrixType &T, ResultType &sqrtT)
void matrix_sqrt_quasi_triangular_2x2_diagonal_block(const MatrixType &T, Index i, ResultType &sqrtT)
void matrix_sqrt_quasi_triangular_off_diagonal(const MatrixType &T, ResultType &sqrtT)
void matrix_sqrt_quasi_triangular_2x2_off_diagonal_block(const MatrixType &T, Index i, Index j, ResultType &sqrtT)
void matrix_sqrt_quasi_triangular_1x1_off_diagonal_block(const MatrixType &T, Index i, Index j, ResultType &sqrtT)
void matrix_sqrt_quasi_triangular_2x1_off_diagonal_block(const MatrixType &T, Index i, Index j, ResultType &sqrtT)
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
Eigen::AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(Eigen::internal::remove_all_t< DerType >, typename Eigen::internal::traits< Eigen::internal::remove_all_t< DerType >>::Scalar, product) > sqrt(const Eigen::AutoDiffScalar< DerType > &x)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_arg_op< typename Derived::Scalar >, const Derived > arg(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_real_op< typename Derived::Scalar >, const Derived > real(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sqrt_op< typename Derived::Scalar >, const Derived > sqrt(const Eigen::ArrayBase< Derived > &x)