25#include <itertools/itertools.hpp>
35namespace nda::lapack {
89 int n_var()
const {
return N_; }
107 if (N_ > M_) NDA_RUNTIME_ERROR <<
"Error in nda::lapack::gelss_worker: Matrix A cannot have more columns than rows";
115 gesvd(A_work, s_, U, V_H);
120 for (
long i : range(s_.size())) S_plus(i, i) = 1.0 / s_(i);
124 if (N_ < M_) U_N_H_ =
dagger(U)(range(N_, M_), range(M_));
145 std::vector<double> err_vec;
147 err = *std::ranges::max_element(err_vec);
149 return std::pair<matrix<T>,
double>{A_plus_ * B, err};
166 return std::pair<vector<T>,
double>{A_plus_ * b, err};
210 int n_var()
const {
return lss_herm_.n_var(); }
236 if (not inner_matrix_dim.has_value())
237 NDA_RUNTIME_ERROR <<
"Error in nda::lapack::gelss_worker_hermitian: Inner matrix dimension required for hermitian least square fitting";
238 long d = *inner_matrix_dim;
244 auto inner_adjoint = [d](
auto &C) {
245 NDA_ASSERT2(C.shape()[1] % (d * d) == 0,
"Error in nda::lapack::gelss_worker_hermitian: Data shape incompatible with inner matrix dimension");
246 auto shape = C.shape();
249 long N = shape[1] / (d * d);
260 auto B_dag = inner_adjoint(B);
261 auto [x, err] = lss_herm_(
vstack(B, B_dag));
264 return std::pair<matrix<std::complex<double>>,
double>{0.5 * (x + inner_adjoint(x)), err};
269 gelss_worker<std::complex<double>> lss_;
272 gelss_worker<std::complex<double>> lss_herm_;
Provides various algorithms to be used with nda::Array objects.
Provides lazy expressions for nda::Array types.
Provides the generic class for arrays.
auto const & shape() const noexcept
Get the shape of the view/array.
long size() const noexcept
Get the total size of the view/array.
int n_var() const
Get the number of variables of the given problem.
auto operator()(matrix_const_view< std::complex< double > > B, std::optional< long > inner_matrix_dim={}) const
Solve the least squares problem for a given right hand side matrix .
array< double, 1 > const & S_vec() const
Get the singular values of the original matrix .
gelss_worker_hermitian(matrix_const_view< std::complex< double > > A)
Construct a new worker object for a given matrix .
auto operator()(vector_const_view< T > b, std::optional< long >={}) const
Solve the least squares problem for a given right hand side vector .
array< double, 1 > const & S_vec() const
Get the singular values, i.e. the diagonal elements of the matrix .
gelss_worker(matrix_const_view< T > A)
Construct a new worker object for a given matrix .
auto operator()(matrix_const_view< T > B, std::optional< long >={}) const
Solve the least squares problem for a given right hand side matrix .
int n_var() const
Get the number of variables of the given problem, i.e. the size of the vector .
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.
auto permuted_indices_view(A &&a)
Permute the indices/dimensions of an nda::basic_array or nda::basic_array_view.
auto reshape(A &&a, std::array< Int, R > const &new_shape)
Reshape an nda::basic_array or nda::basic_array_view.
matrix< get_value_t< A > > vstack(A const &a, B const &b)
Stack two 2-dimensional arrays/views vertically.
auto sqrt(A &&a)
Function sqrt for non-matrix nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types...
ArrayOfRank< 2 > auto dagger(M const &m)
Get the conjugate transpose of 2-dimensional array/view.
double frobenius_norm(A const &a)
Calculate the Frobenius norm of a 2-dimensional array.
decltype(auto) conj(A &&a)
Function conj for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types with a com...
basic_array< ValueType, Rank, Layout, 'A', ContainerPolicy > array
Alias template of an nda::basic_array with an 'A' algebra.
basic_array_view< ValueType const, 1, Layout, 'V', default_accessor, borrowed<> > vector_const_view
Same as nda::vector_view except for const value types.
basic_array_view< ValueType const, 2, Layout, 'M', default_accessor, borrowed<> > matrix_const_view
Same as nda::matrix_view except for const value types.
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.
constexpr uint64_t encode(std::array< int, N > const &a)
Encode a std::array<int, N> in a uint64_t.
Provides definitions of various layout policies.
Includes all relevant headers for the linear algebra functionality.
Provides some custom implementations of standard mathematical functions used for lazy,...
Provides functions to create and manipulate matrices, i.e. arrays/view with 'M' algebra.