TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
gelss.hpp
Go to the documentation of this file.
1// Copyright (c) 2020--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 "../concepts.hpp"
17#include "../declarations.hpp"
18#include "../macros.hpp"
20#include "../traits.hpp"
21
22#include <algorithm>
23#include <cmath>
24#include <complex>
25#include <type_traits>
26
27namespace nda::lapack {
28
65 template <MemoryMatrix A, MemoryArray B, MemoryVector S>
67 int gelss(A &&a, B &&b, S &&s, double rcond, int &rank) { // NOLINT (temporary views are allowed here)
68 static_assert(std::is_same_v<get_value_t<S>, double>, "Error in nda::lapack::gelss: Singular value array must have elements of type double");
69 static_assert(has_F_layout<A> and has_F_layout<B>, "Error in nda::lapack::gelss: Matrices/arrays must have Fortran layout");
70 static_assert(get_rank<B> == 1 || get_rank<B> == 2, "Error in nda::lapack::gelss: Right hand side must have rank 1 or 2");
71
72 // check the dimensions of the input/output arrays/views and resize if necessary
73 auto const [m, n] = a.shape();
74 auto const k = std::min(m, n);
76 EXPECTS(b.extent(0) == m);
77
78 // arrays/views must be LAPACK compatible
79 EXPECTS(a.indexmap().min_stride() == 1);
80 EXPECTS(b.indexmap().min_stride() == 1);
81 EXPECTS(s.indexmap().min_stride() == 1);
82
83 // first call to get the optimal buffer size
84 using value_type = get_value_t<A>;
85 value_type tmp_lwork{};
86 auto rwork = array<double, 1>(5 * k);
87 int info = 0;
88 int nrhs = (get_rank<B> == 2 ? b.extent(1) : 1);
89 f77::gelss(m, n, nrhs, a.data(), get_ld(a), b.data(), get_ld(b), s.data(), rcond, rank, &tmp_lwork, -1, rwork.data(), info);
90 int lwork = static_cast<int>(std::ceil(std::real(tmp_lwork)));
91
92 // allocate work buffer and perform actual library call
93 array<value_type, 1> work(lwork);
94 f77::gelss(m, n, nrhs, a.data(), get_ld(a), b.data(), get_ld(b), s.data(), rcond, rank, work.data(), lwork, rwork.data(), info);
95
96 return info;
97 }
98
99} // 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.
ValueType const * data() const noexcept
Get a pointer to the actual data (in general this is not the beginning of the memory block for a view...
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.
basic_array< ValueType, Rank, Layout, 'A', ContainerPolicy > array
Alias template of an nda::basic_array with an 'A' 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:186
constexpr int get_rank
Constexpr variable that specifies the rank of an nda::Array or of a contiguous 1-dimensional range.
Definition traits.hpp:126
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:182
int gelss(A &&a, B &&b, S &&s, double rcond, int &rank)
Interface to the LAPACK gelss routine.
Definition gelss.hpp:67
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_blas_lapack_v
Alias for nda::is_double_or_complex_v.
Definition traits.hpp:92
Provides a C++ interface for various LAPACK routines.
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:122
static constexpr bool has_F_layout
Constexpr variable that is true if the given nda::Array type has nda::F_layout.
Definition tools.hpp:73
Macros used in the nda library.
Provides type traits for the nda library.