TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
eigenelements.hpp
Go to the documentation of this file.
1// Copyright (c) 2019--present, The Simons Foundation
2// This file is part of TRIQS/nda and is licensed under the Apache License, Version 2.0.
3// SPDX-License-Identifier: Apache-2.0
4// See LICENSE in the root of this distribution for details.
5
10
11#pragma once
12
13#include "./det_and_inverse.hpp"
14#include "../basic_array.hpp"
15#include "../declarations.hpp"
16#include "../exceptions.hpp"
19#include "../macros.hpp"
20#include "../traits.hpp"
21
22#include <type_traits>
23#include <utility>
24
25namespace nda::linalg {
26
27 namespace detail {
28
29 // Dispatch the call to the appropriate LAPACK routine based on the value type of the matrix.
30 template <typename M>
31 auto _eigen_element_impl(M &&m, char compz) { // NOLINT (temporary views are allowed here)
32 using value_type = typename std::decay_t<M>::value_type;
33
34 // runtime checks
35 EXPECTS((not m.empty()));
36 EXPECTS(is_matrix_square(m, true));
37 EXPECTS(m.is_contiguous());
38 EXPECTS(m.has_positive_strides());
39
40 // set up the workspace
41 int dim = m.extent(0);
42 int lwork = 64 * dim;
43 array<double, 1> ev(dim);
44 array<value_type, 1> work(lwork);
46
47#if defined(__has_feature)
48#if __has_feature(memory_sanitizer)
49 work2 = 0;
50 work = 0;
51 ev = 0;
52#endif
53#endif
54
55 // call the correct LAPACK routine
56 int info = 0;
57 if constexpr (not is_complex_v<value_type>) {
58 lapack::f77::syev(compz, 'U', dim, m.data(), dim, ev.data(), work.data(), lwork, info);
59 } else {
60 lapack::f77::heev(compz, 'U', dim, m.data(), dim, ev.data(), work.data(), lwork, work2.data(), info);
61 }
62 if (info) NDA_RUNTIME_ERROR << "Error in nda::linalg::detail::_eigen_element_impl: Diagonalization error";
63 return ev;
64 }
65
66 } // namespace detail
67
72
86 template <typename M>
87 auto eigenelements(M const &m) {
89 auto ev = detail::_eigen_element_impl(m_copy, 'V');
90 return std::pair<array<double, 1>, typename M::regular_type>{ev, m_copy};
91 }
92
105 template <typename M>
106 auto eigenvalues(M const &m) {
108 return detail::_eigen_element_impl(m_copy, 'N');
109 }
110
123 template <typename M>
125 return detail::_eigen_element_impl(m, 'N');
126 }
127
129
130} // namespace nda::linalg
Provides the generic class for arrays.
Provides various convenient aliases and helper functions for nda::basic_array and nda::basic_array_vi...
Provides functions to compute the determinant and inverse of a matrix.
Provides a custom runtime error class and macros to assert conditions and throw exceptions.
basic_array< ValueType, Rank, Layout, 'A', ContainerPolicy > array
Alias template of an nda::basic_array with an 'A' algebra.
basic_array< ValueType, 2, Layout, 'M', ContainerPolicy > matrix
Alias template of an nda::basic_array with rank 2 and an 'M' algebra.
auto eigenvalues(M const &m)
Find the eigenvalues of a symmetric (real) or hermitian (complex) matrix/view.
auto eigenelements(M const &m)
Find the eigenvalues and eigenvectors of a symmetric (real) or hermitian (complex) matrix/view.
bool is_matrix_square(A const &a, bool print_error=false)
Check if a given array/view is square, i.e. if the first dimension has the same extent as the second ...
auto eigenvalues_in_place(M &m)
Find the eigenvalues of a symmetric (real) or hermitian (complex) matrix/view.
constexpr bool is_complex_v
Constexpr variable that is true if type T is a std::complex type.
Definition traits.hpp:65
Provides a C++ interface for various LAPACK routines.
Provides definitions of various layout policies.
Macros used in the nda library.
Provides type traits for the nda library.