TRIQS/nda 2.0.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
geev.hpp
Go to the documentation of this file.
1// Copyright (c) 2024--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
14#include "../basic_array.hpp"
16#include "../blas/tools.hpp"
17#include "../concepts.hpp"
18#include "../declarations.hpp"
19#include "../macros.hpp"
21#include "../traits.hpp"
22
23#include <cmath>
24#include <complex>
25#include <concepts>
26
27namespace nda::lapack {
28
74 template <BlasArrayReal<2> A, BlasArrayFor<A, 1> WR, BlasArrayFor<A, 1> WI, BlasArrayFor<A, 2> VL, BlasArrayFor<A, 2> VR,
75 BlasArrayFor<A, 1> W1 = vector_value_t<A>>
77 int geev(A &&a, WR &&wr, WI &&wi, VL &&vl, VR &&vr, char jobvl = 'N', char jobvr = 'V', W1 &&work = vector_value_t<A>{}) { // NOLINT
78 // check the dimensions of the input/output arrays/views
79 auto const [m, n] = a.shape();
80 EXPECTS(m == n);
83
84 // check other input parameters for consistency
85 EXPECTS(jobvl == 'V' or jobvl == 'N');
86 EXPECTS(jobvr == 'V' or jobvr == 'N');
87
88 // resize eigenvector matrices if needed
89 int ldvl = 1;
90 int ldvr = 1;
91 if (jobvl == 'V') {
92 resize_or_check_if_view(vl, {n, n});
93 ldvl = get_ld(vl);
94 }
95 if (jobvr == 'V') {
96 resize_or_check_if_view(vr, {n, n});
97 ldvr = get_ld(vr);
98 }
99
100 // arrays/views must be LAPACK compatible
101 EXPECTS(a.indexmap().min_stride() == 1);
102 EXPECTS(wr.indexmap().min_stride() == 1);
103 EXPECTS(wi.indexmap().min_stride() == 1);
104 EXPECTS(jobvl == 'N' or vl.indexmap().min_stride() == 1);
105 EXPECTS(jobvr == 'N' or vr.indexmap().min_stride() == 1);
106
107 // first call to get the optimal buffer size
108 auto tmp_lwork = get_value_t<A>{};
109 int info = 0;
110 lapack::f77::geev(jobvl, jobvr, n, a.data(), get_ld(a), wr.data(), wi.data(), vl.data(), ldvl, vr.data(), ldvr, &tmp_lwork, -1, info);
111 int lwork = static_cast<int>(std::ceil(tmp_lwork));
112
113 // resize/check work buffer
114 resize_or_check_work_buffer(work, lwork);
115
116 // perform actual library call
117 lapack::f77::geev(jobvl, jobvr, n, a.data(), get_ld(a), wr.data(), wi.data(), vl.data(), ldvl, vr.data(), ldvr, work.data(), lwork, info);
118
119 return info;
120 }
121
162 template <BlasArrayCplx<2> A, BlasArrayFor<A, 1> W, BlasArrayFor<A, 2> VL, BlasArrayFor<A, 2> VR, BlasArrayFor<A, 1> W1 = vector_value_t<A>,
163 BlasArrayRealFor<A, 1> W2 = vector_fp_t<A>>
165 int geev(A &&a, W &&w, VL &&vl, VR &&vr, char jobvl = 'N', char jobvr = 'V', W1 &&work = vector_value_t<A>{}, // NOLINT
166 W2 &&rwork = vector_fp_t<A>{}) { // NOLINT
167 // check the dimensions of the input/output arrays/views
168 auto const [m, n] = a.shape();
169 EXPECTS(m == n);
171 resize_or_check_work_buffer(rwork, 2 * n);
172
173 // check parameters
174 EXPECTS(jobvl == 'V' or jobvl == 'N');
175 EXPECTS(jobvr == 'V' or jobvr == 'N');
176
177 // resize eigenvector matrices if needed
178 int ldvl = 1;
179 int ldvr = 1;
180 if (jobvl == 'V') {
181 resize_or_check_if_view(vl, {n, n});
182 ldvl = get_ld(vl);
183 }
184 if (jobvr == 'V') {
185 resize_or_check_if_view(vr, {n, n});
186 ldvr = get_ld(vr);
187 }
188
189 // arrays/views must be LAPACK compatible
190 EXPECTS(a.indexmap().min_stride() == 1);
191 EXPECTS(w.indexmap().min_stride() == 1);
192 EXPECTS(jobvl == 'N' or vl.indexmap().min_stride() == 1);
193 EXPECTS(jobvr == 'N' or vr.indexmap().min_stride() == 1);
194
195 // first call to get the optimal buffer size
196 auto tmp_lwork = get_value_t<A>{};
197 int info = 0;
198 lapack::f77::geev(jobvl, jobvr, n, a.data(), get_ld(a), w.data(), vl.data(), ldvl, vr.data(), ldvr, &tmp_lwork, -1, rwork.data(), info);
199 int lwork = static_cast<int>(std::ceil(std::real(tmp_lwork)));
200
201 // resize/check work buffer
202 resize_or_check_work_buffer(work, lwork);
203
204 // perform actual library call
205 lapack::f77::geev(jobvl, jobvr, n, a.data(), get_ld(a), w.data(), vl.data(), ldvl, vr.data(), ldvr, work.data(), lwork, rwork.data(), info);
206
207 return info;
208 }
209
210} // namespace nda::lapack
Provides definitions and type traits involving the different memory address spaces supported by nda.
Provides the generic class for arrays.
Provides basic functions to create and manipulate arrays and views.
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...
void resize_or_check_if_view(A &a, std::array< long, A::rank > const &sha)
Resize a given regular array to the given shape or check if a given view as the correct shape.
std::decay_t< decltype(get_first_element(std::declval< A const >()))> get_value_t
Get the value type of an array/view or a scalar type.
Definition traits.hpp:212
int get_ld(A const &a)
Get the leading dimension of an nda::MemoryArray with rank 1 or 2 for BLAS/LAPACK calls.
Definition tools.hpp:128
void resize_or_check_work_buffer(A &a, long min_size)
Resize or check the size of a 1D array/view.
Definition tools.hpp:207
vector< get_value_t< A >, heap< mem::get_addr_space< A > > > vector_value_t
Alias for an nda::vector with the same value type and address space as the given type.
Definition tools.hpp:161
vector< get_fp_t< A >, heap< mem::get_addr_space< A > > > vector_fp_t
Alias for an nda::vector with the same address space as the given type and its value type determined ...
Definition tools.hpp:170
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
int geev(A &&a, WR &&wr, WI &&wi, VL &&vl, VR &&vr, char jobvl='N', char jobvr='V', W1 &&work=vector_value_t< A >{})
Interface to the LAPACK geev routine for real matrices.
Definition geev.hpp:77
static constexpr bool have_host_compatible_addr_space
Constexpr variable that is true if all given types have an address space compatible with Host.
Provides a C++ interface for various LAPACK routines.
Macros used in the nda library.
Provides type traits for the nda library.