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-2023 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: 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.indexmap().is_contiguous());
49
50 // set up the workspace
51 int dim = m.extent(0);
52 int lwork = 64 * dim;
53 array<double, 1> ev(dim);
54 array<value_type, 1> work(lwork);
56
57#if defined(__has_feature)
58#if __has_feature(memory_sanitizer)
59 work2 = 0;
60 work = 0;
61 ev = 0;
62#endif
63#endif
64
65 // call the correct LAPACK routine
66 int info = 0;
67 if constexpr (not is_complex_v<value_type>) {
68 lapack::f77::syev(compz, 'U', dim, m.data(), dim, ev.data(), work.data(), lwork, info);
69 } else {
70 lapack::f77::heev(compz, 'U', dim, m.data(), dim, ev.data(), work.data(), lwork, work2.data(), info);
71 }
72 if (info) NDA_RUNTIME_ERROR << "Error in nda::linalg::detail::_eigen_element_impl: Diagonalization error";
73 return ev;
74 }
75
76 } // namespace detail
77
96 template <typename M>
97 auto eigenelements(M const &m) {
99 auto ev = detail::_eigen_element_impl(m_copy, 'V');
100 return std::pair<array<double, 1>, typename M::regular_type>{ev, m_copy};
101 }
102
115 template <typename M>
116 auto eigenvalues(M const &m) {
118 return detail::_eigen_element_impl(m_copy, 'N');
119 }
120
133 template <typename M>
135 return detail::_eigen_element_impl(m, 'N');
136 }
137
140} // 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.