TRIQS/nda 2.0.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
eigh.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 "../basic_array.hpp"
14#include "../blas/tools.hpp"
15#include "../concepts.hpp"
16#include "../declarations.hpp"
17#include "../exceptions.hpp"
18#include "../lapack/syev.hpp"
19#include "../lapack/sygv.hpp"
20#include "../lapack/heev.hpp"
21#include "../lapack/hegv.hpp"
23#include "../macros.hpp"
26#include "../traits.hpp"
27
28#include <complex>
29#include <type_traits>
30#include <utility>
31
32namespace nda::linalg {
33
38
39 namespace detail {
40
41 // Perform the call to the LAPACK routines syev/heev for eigh_in_place and eigvalsh_in_place.
42 template <typename A>
43 auto eigh_impl(A &&a, char jobz) { // NOLINT (temporary views are allowed here)
44 using fp_t = get_fp_t<A>;
45
46 // early return if the matrix is empty
47 if (a.empty()) return array<fp_t, 1>{};
48
49 // make the call to syev/heev
50 auto lambda = array<fp_t, 1>(a.extent(0));
51 int info = 0;
52 if constexpr (is_complex_v<get_value_t<A>>) {
53 info = lapack::heev(a, lambda, jobz);
54 } else {
55 info = lapack::syev(a, lambda, jobz);
56 }
57 if (info != 0) NDA_RUNTIME_ERROR << "Error in nda::linalg::detail::eigh_impl: syev/heev routine failed: info = " << info;
58
59 return lambda;
60 }
61
62 // Perform the call to the LAPACK routines sygv/hegv for eigh_in_place and eigvalsh_in_place.
63 template <typename A, typename B>
64 auto eigh_impl(A &&a, B &&b, char jobz, int itype) { // NOLINT (temporary views are allowed here)
65 using fp_t = get_fp_t<A>;
66
67 // early return if the matrix is empty
68 if (a.empty()) return array<fp_t, 1>{};
69
70 // make the call to sygv/hegv
71 auto lambda = array<fp_t, 1>(a.extent(0));
72 int info = 0;
73 if constexpr (is_complex_v<get_value_t<A>>) {
74 info = lapack::hegv(a, b, lambda, jobz, itype);
75 } else {
76 info = lapack::sygv(a, b, lambda, jobz, itype);
77 }
78 if (info != 0) NDA_RUNTIME_ERROR << "Error in nda::linalg::detail::eigh_impl: sygv/hegv routine failed: info = " << info;
79
80 return lambda;
81 }
82
83 } // namespace detail
84
107 template <blas_lapack::BlasArray<2> A>
109 auto eigh_in_place(A &&a) {
110 return detail::eigh_impl(std::forward<A>(a), 'V');
111 }
112
144 template <blas_lapack::BlasArray<2> A, blas_lapack::BlasArrayFor<A, 2> B>
146 auto eigh_in_place(A &&a, B &&b, int itype = 1) {
147 return detail::eigh_impl(std::forward<A>(a), std::forward<B>(b), 'V', itype);
148 }
149
163 template <Matrix A>
165 auto eigh(A const &a) {
166 auto a_copy = matrix<get_value_t<A>, F_layout>{a};
167 auto lambda = eigh_in_place(a_copy);
168 return std::make_pair(lambda, a_copy);
169 }
170
189 template <Matrix A, Matrix B>
191 auto eigh(A const &a, B const &b, int itype = 1) {
192 auto a_copy = matrix<get_value_t<A>, F_layout>{a};
193 auto b_copy = matrix<get_value_t<B>, F_layout>{b};
194 auto lambda = eigh_in_place(a_copy, b_copy, itype);
195 return std::make_pair(lambda, a_copy);
196 }
197
219 template <blas_lapack::BlasArray<2> A>
221 auto eigvalsh_in_place(A &&a) {
222 return detail::eigh_impl(std::forward<A>(a), 'N');
223 }
224
255 template <blas_lapack::BlasArray<2> A, blas_lapack::BlasArrayFor<A, 2> B>
257 auto eigvalsh_in_place(A &&a, B &&b, int itype = 1) {
258 return detail::eigh_impl(std::forward<A>(a), std::forward<B>(b), 'N', itype);
259 }
260
274 template <Matrix A>
276 auto eigvalsh(A const &a) {
277 auto a_copy = matrix<get_value_t<A>, F_layout>{a};
278 return eigvalsh_in_place(a_copy);
279 }
280
298 template <Matrix A, Matrix B>
300 auto eigvalsh(A const &a, B const &b, int itype = 1) {
301 auto a_copy = matrix<get_value_t<A>, F_layout>{a};
302 auto b_copy = matrix<get_value_t<B>, F_layout>{b};
303 return eigvalsh_in_place(a_copy, b_copy, itype);
304 }
305
307
308} // namespace nda::linalg
Provides definitions and type traits involving the different memory address spaces supported by nda.
Provides the generic class for arrays.
Provides various traits and utilities for the BLAS interface.
Provides concepts for the nda library.
Provides various convenient aliases and helper functions for nda::basic_array and nda::basic_array_vi...
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.
constexpr bool have_same_value_type_v
Constexpr variable that is true if all types in As have the same value type as A0.
Definition traits.hpp:225
typename remove_complex< get_value_t< A > >::type get_fp_t
Get the floating-point type associated with the value type of an array/view/scalar type.
Definition traits.hpp:221
static constexpr bool has_F_layout
Constexpr variable that is true if all given nda::Array types have nda::F_layout.
Definition tools.hpp:79
auto eigh(A const &a)
Compute the eigenvalues and eigenvectors of a real symmetric or complex hermitian matrix.
Definition eigh.hpp:165
auto eigh_in_place(A &&a)
Compute the eigenvalues and eigenvectors of a real symmetric or complex hermitian matrix in place.
Definition eigh.hpp:109
auto eigvalsh(A const &a)
Compute the eigenvalues of a real symmetric or complex hermitian matrix.
Definition eigh.hpp:276
auto eigvalsh_in_place(A &&a)
Compute the eigenvalues of a real symmetric or complex hermitian matrix in place.
Definition eigh.hpp:221
int hegv(A &&a, B &&b, W &&w, char jobz='V', int itype=1, W1 &&work=vector_value_t< A >{}, W2 &&rwork=vector_fp_t< A >{})
Interface to the LAPACK hegv routine.
Definition hegv.hpp:69
int syev(A &&a, W &&w, char jobz='V', W1 &&work=vector_value_t< A >{})
Interface to the LAPACK syev routine.
Definition syev.hpp:55
int sygv(A &&a, B &&b, W &&w, char jobz='V', int itype=1, W1 &&work=vector_value_t< A >{})
Interface to the LAPACK sygv routine.
Definition sygv.hpp:64
int heev(A &&a, W &&w, char jobz='V', W1 &&work=vector_value_t< A >{}, W2 &&rwork=vector_fp_t< A >{})
Interface to the LAPACK heev routine.
Definition heev.hpp:59
static constexpr bool have_host_compatible_addr_space
Constexpr variable that is true if all given types have an address space compatible with Host.
constexpr bool is_complex_v
Constexpr variable that is true if type T is a std::complex type.
Definition traits.hpp:65
constexpr bool is_blas_lapack_v
Constexpr variable that is true if type T is either of type 'float', double, std::complex<float>' or ...
Definition traits.hpp:95
Provides a generic interface to the LAPACK heev routine.
Provides a generic interface to the LAPACK hegv routine.
Provides definitions of various layout policies.
Macros used in the nda library.
Provides functions to create and manipulate matrices, i.e. arrays/view with 'M' algebra.
Contiguous layout policy with Fortran-order (column-major order).
Definition policies.hpp:52
Provides a generic interface to the LAPACK syev routine.
Provides a generic interface to the LAPACK sygv routine.
Provides type traits for the nda library.