Automatic Differentiation
 
Loading...
Searching...
No Matches
eigendecompose.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_EIGENDECOMPOSE_HPP
2#define STAN_MATH_PRIM_FUN_EIGENDECOMPOSE_HPP
3
6
7namespace stan {
8namespace math {
9
20template <typename EigMat, require_eigen_matrix_dynamic_t<EigMat>* = nullptr,
21 require_not_vt_complex<EigMat>* = nullptr>
22inline std::tuple<Eigen::Matrix<complex_return_t<value_type_t<EigMat>>, -1, -1>,
23 Eigen::Matrix<complex_return_t<value_type_t<EigMat>>, -1, 1>>
24eigendecompose(const EigMat& m) {
25 if (unlikely(m.size() == 0)) {
26 return std::make_tuple(
27 Eigen::Matrix<complex_return_t<value_type_t<EigMat>>, -1, -1>(0, 0),
28 Eigen::Matrix<complex_return_t<value_type_t<EigMat>>, -1, 1>(0, 1));
29 }
30 check_square("eigendecompose", "m", m);
31
32 using PlainMat = plain_type_t<EigMat>;
33 const PlainMat& m_eval = m;
34
35 Eigen::EigenSolver<PlainMat> solver(m_eval);
36 return std::make_tuple(std::move(solver.eigenvectors()),
37 std::move(solver.eigenvalues()));
38}
39
50template <typename EigCplxMat,
52inline std::tuple<
53 Eigen::Matrix<complex_return_t<value_type_t<EigCplxMat>>, -1, -1>,
54 Eigen::Matrix<complex_return_t<value_type_t<EigCplxMat>>, -1, 1>>
55eigendecompose(const EigCplxMat& m) {
56 if (unlikely(m.size() == 0)) {
57 return std::make_tuple(
58 Eigen::Matrix<complex_return_t<value_type_t<EigCplxMat>>, -1, -1>(0, 0),
59 Eigen::Matrix<complex_return_t<value_type_t<EigCplxMat>>, -1, 1>(0, 1));
60 }
61 check_square("eigendecompose", "m", m);
62
63 using PlainMat = Eigen::Matrix<scalar_type_t<EigCplxMat>, -1, -1>;
64 const PlainMat& m_eval = m;
65
66 Eigen::ComplexEigenSolver<PlainMat> solver(m_eval);
67
68 return std::make_tuple(std::move(solver.eigenvectors()),
69 std::move(solver.eigenvalues()));
70}
71
72} // namespace math
73} // namespace stan
74#endif
#define unlikely(x)
require_t< container_type_check_base< is_eigen_matrix_dynamic, value_type_t, TypeCheck, Check... > > require_eigen_matrix_dynamic_vt
Require type satisfies is_eigen_matrix_dynamic.
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
std::complex< real_return_t< Ts... > > complex_return_t
Convenience type to calculate the complex return type, which wraps std::complex around the return typ...
void check_square(const char *function, const char *name, const T_y &y)
Check if the specified matrix is square.
std::tuple< Eigen::Matrix< complex_return_t< value_type_t< EigMat > >, -1, -1 >, Eigen::Matrix< complex_return_t< value_type_t< EigMat > >, -1, 1 > > eigendecompose(const EigMat &m)
Return the eigendecomposition of a (real-valued) matrix.
typename plain_type< T >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...