TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
svd.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
21
22#pragma once
23
24#include "../basic_array.hpp"
25#include "../blas/tools.hpp"
26#include "../concepts.hpp"
27#include "../declarations.hpp"
28#include "../exceptions.hpp"
29#include "../lapack/gesvd.hpp"
31#include "../macros.hpp"
33#include "../mem/policies.hpp"
34#include "../traits.hpp"
35
36#include <algorithm>
37#include <tuple>
38#include <type_traits>
39#include <utility>
40
41namespace nda::linalg {
42
47
71 template <MemoryMatrix A>
73 auto svd_in_place(A &&a) { // NOLINT (temporary views are allowed here)
74 using layout_policy = nda::detail::layout_to_policy<typename std::remove_cvref_t<A>::layout_t>::type;
75 constexpr auto addr_space = nda::mem::get_addr_space<A>;
76
77 // vector s and matrices U and V^H
78 auto const [m, n] = a.shape();
79 auto s = vector<double, heap<addr_space>>(std::min(m, n));
80 auto U = matrix<get_value_t<A>, layout_policy, heap<addr_space>>(m, m);
81 auto VH = matrix<get_value_t<A>, layout_policy, heap<addr_space>>(n, n);
82
83 // call lapack gesvd
84 int info = lapack::gesvd(a, s, U, VH);
85 if (info != 0) NDA_RUNTIME_ERROR << "Error in nda::svd_in_place: gesvd returned a non-zero value: info = " << info;
86
87 return std::make_tuple(U, s, VH);
88 }
89
112 template <Matrix A>
114 auto svd(A const &a) { // NOLINT (temporary views are allowed here)
115 return svd_in_place(basic_array{a});
116 }
117
119
120} // namespace nda::linalg
Provides definitions and type traits involving the different memory address spaces supported by nda.
Provides the generic class for arrays.
A generic multi-dimensional array.
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.
Provides a generic interface to the LAPACK gesvd routine.
basic_array< ValueType, 1, C_layout, 'V', ContainerPolicy > vector
Alias template of an nda::basic_array with rank 1 and a 'V' algebra.
basic_array< ValueType, 2, Layout, 'M', ContainerPolicy > matrix
Alias template of an nda::basic_array with rank 2 and an 'M' algebra.
int gesvd(A &&a, S &&s, U &&u, VT &&vt)
Interface to the LAPACK gesvd routine.
Definition gesvd.hpp:67
auto svd(A const &a)
Compute the singular value decomposition (SVD) of a matrix.
Definition svd.hpp:114
auto svd_in_place(A &&a)
Compute the singular value decomposition (SVD) of a matrix in place.
Definition svd.hpp:73
static constexpr AddressSpace get_addr_space
Variable template providing the address space for different types.
heap_basic< mem::mallocator< AdrSp > > heap
Alias template of the nda::heap_basic policy using an nda::mem::mallocator.
Definition policies.hpp:52
constexpr bool is_blas_lapack_v
Alias for nda::is_double_or_complex_v.
Definition traits.hpp:92
Provides definitions of various layout policies.
Macros used in the nda library.
Defines various memory handling policies.
Provides various traits and utilities for the BLAS interface.
Provides type traits for the nda library.