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-2024 Simons Foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0.txt
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Authors: Thomas Hahn, Olivier Parcollet, Nils Wentzell
16
22#pragma once
23
24#include "./det_and_inverse.hpp"
25#include "../basic_array.hpp"
26#include "../declarations.hpp"
27#include "../exceptions.hpp"
30#include "../macros.hpp"
31#include "../traits.hpp"
32
33#include <type_traits>
34#include <utility>
35
36namespace nda::linalg {
37
38 namespace detail {
39
40 // Dispatch the call to the appropriate LAPACK routine based on the value type of the matrix.
41 template <typename M>
42 auto _eigen_element_impl(M &&m, char compz) { // NOLINT (temporary views are allowed here)
43 using value_type = typename std::decay_t<M>::value_type;
44
45 // runtime checks
46 EXPECTS((not m.empty()));
47 EXPECTS(is_matrix_square(m, true));
48 EXPECTS(m.is_contiguous());
49 EXPECTS(m.has_positive_strides());
50
51 // set up the workspace
52 int dim = m.extent(0);
53 int lwork = 64 * dim;
54 array<double, 1> ev(dim);
55 array<value_type, 1> work(lwork);
57
58#if defined(__has_feature)
59#if __has_feature(memory_sanitizer)
60 work2 = 0;
61 work = 0;
62 ev = 0;
63#endif
64#endif
65
66 // call the correct LAPACK routine
67 int info = 0;
68 if constexpr (not is_complex_v<value_type>) {
69 lapack::f77::syev(compz, 'U', dim, m.data(), dim, ev.data(), work.data(), lwork, info);
70 } else {
71 lapack::f77::heev(compz, 'U', dim, m.data(), dim, ev.data(), work.data(), lwork, work2.data(), info);
72 }
73 if (info) NDA_RUNTIME_ERROR << "Error in nda::linalg::detail::_eigen_element_impl: Diagonalization error";
74 return ev;
75 }
76
77 } // namespace detail
78
97 template <typename M>
98 auto eigenelements(M const &m) {
100 auto ev = detail::_eigen_element_impl(m_copy, 'V');
101 return std::pair<array<double, 1>, typename M::regular_type>{ev, m_copy};
102 }
103
116 template <typename M>
117 auto eigenvalues(M const &m) {
119 return detail::_eigen_element_impl(m_copy, 'N');
120 }
121
134 template <typename M>
136 return detail::_eigen_element_impl(m, 'N');
137 }
138
141} // 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:75
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.