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
7
8namespace stan {
9namespace math {
10
21template <typename EigMat, require_eigen_matrix_dynamic_t<EigMat>* = nullptr,
22 require_not_vt_complex<EigMat>* = nullptr>
23inline std::tuple<Eigen::Matrix<complex_return_t<value_type_t<EigMat>>, -1, -1>,
24 Eigen::Matrix<complex_return_t<value_type_t<EigMat>>, -1, 1>>
25eigendecompose(EigMat&& m) {
26 if (unlikely(m.size() == 0)) {
27 return std::make_tuple(
28 Eigen::Matrix<complex_return_t<value_type_t<EigMat>>, -1, -1>(0, 0),
29 Eigen::Matrix<complex_return_t<value_type_t<EigMat>>, -1, 1>(0, 1));
30 }
31 check_square("eigendecompose", "m", m);
32
33 decltype(auto) m_ref = to_ref(std::forward<EigMat>(m));
34 using PlainMat = plain_type_t<EigMat>;
35 Eigen::EigenSolver<PlainMat> solver(std::forward<decltype(m_ref)>(m_ref));
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(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 decltype(auto) m_ref = to_ref(std::forward<EigCplxMat>(m));
64
65 using PlainMat = Eigen::Matrix<scalar_type_t<EigCplxMat>, -1, -1>;
66 Eigen::ComplexEigenSolver<PlainMat> solver(
67 std::forward<decltype(m_ref)>(m_ref));
68
69 return std::make_tuple(std::move(solver.eigenvectors()),
70 std::move(solver.eigenvalues()));
71}
72
73} // namespace math
74} // namespace stan
75#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(EigMat &&m)
Return the eigendecomposition of a (real-valued) matrix.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
typename plain_type< std::decay_t< T > >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...