TRIQS/nda 2.0.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
svd.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/gesvd.hpp"
20#include "../macros.hpp"
22#include "../mem/policies.hpp"
23#include "../traits.hpp"
24
25#include <algorithm>
26#include <tuple>
27#include <type_traits>
28#include <utility>
29
30namespace nda::linalg {
31
36
61 template <blas_lapack::BlasArray<2> A>
62 auto svd_in_place(A &&a) { // NOLINT (temporary views are allowed here)
63 using layout_policy = nda::detail::layout_to_policy<typename std::remove_cvref_t<A>::layout_t>::type;
64 constexpr auto addr_space = mem::get_addr_space<A>;
65
66 // vector s and matrices U and V^H
67 auto const [m, n] = a.shape();
68 auto s = vector<get_fp_t<A>, heap<addr_space>>(std::min(m, n));
69 auto U = matrix<get_value_t<A>, layout_policy, heap<addr_space>>(m, m);
70 auto VH = matrix<get_value_t<A>, layout_policy, heap<addr_space>>(n, n);
71
72 // call lapack gesvd
73 int info = lapack::gesvd(a, s, U, VH);
74 if (info != 0) NDA_RUNTIME_ERROR << "Error in nda::linalg::svd_in_place: gesvd returned a non-zero value: info = " << info;
75
76 return std::make_tuple(U, s, VH);
77 }
78
93 template <Matrix A>
95 auto svd(A const &a) { // NOLINT (temporary views are allowed here)
96 return svd_in_place(basic_array{a});
97 }
98
100
101} // 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.
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/cuSOLVER 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.
auto svd(A const &a)
Compute the singular value decomposition (SVD) of a matrix.
Definition svd.hpp:95
auto svd_in_place(A &&a)
Compute the singular value decomposition (SVD) of a matrix in place.
Definition svd.hpp:62
int gesvd(A &&a, S &&s, U &&u, VH &&vh, W1 &&work=vector_value_t< A >{}, W2 &&rwork=vector_fp_t< A >{})
Interface to the LAPACK/cuSOLVER gesvd routine.
Definition gesvd.hpp:71
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
Constexpr variable that is true if type T is either of type 'float', double, std::complex<float>' or ...
Definition traits.hpp:95
Provides definitions of various layout policies.
Macros used in the nda library.
Defines various memory handling policies.
Provides type traits for the nda library.