34#include <itertools/itertools.hpp>
44namespace nda::lapack {
90 int n_var()
const {
return A.extent(1); }
108 if (N > M) NDA_RUNTIME_ERROR <<
"Error in nda::lapack::gelss_worker: Matrix A cannot have more columns than rows";
116 gesvd(A_FL, s_vec, U, VH);
121 for (
long i : range(std::min(M, N))) S_inv(i, i) = 1.0 / s_vec(i);
125 if (N < M) UH_NULL =
dagger(U)(range(N, M), range(M));
138 std::vector<double> err_vec;
140 err = *std::max_element(err_vec.begin(), err_vec.end());
142 return std::make_pair(V_x_InvS_x_UH * B, err);
154 if (M != N) { err = norm(UH_NULL * b) /
sqrt(b.
size()); }
155 return std::make_pair(V_x_InvS_x_UH * b, err);
168 using dcomplex = std::complex<double>;
184 int n_var()
const {
return static_cast<int>(A.extent(1)); }
205 if (not inner_matrix_dim.has_value())
206 NDA_RUNTIME_ERROR <<
"Error in nda::lapack::gelss_worker_hermitian: Inner matrix dimension required for hermitian least square fitting";
207 long d = *inner_matrix_dim;
213 auto inner_adjoint = [d](
auto &M) {
218 NDA_ASSERT2(l[1] % (d * d) == 0,
"Error in nda::lapack::gelss_worker_hermitian: Data shape incompatible with given dimension");
219 long N = l[1] / (d * d);
238 auto B_stack =
vstack(B, B_dag);
239 auto [x, err] = _lss_matrix(B_stack);
242 return {0.5 * (x + inner_adjoint(x)), err};
Provides various algorithms to be used with nda::Array objects.
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.
Layout that specifies how to map multi-dimensional indices to a linear/flat index.
std::array< long, Rank > const & lengths() const noexcept
Get the extents of all dimensions.
Worker class for solving linear least square problems.
array< double, 1 > const & S_vec() const
Get the singular value array.
gelss_worker(matrix< T > A_)
Construct a new worker object for a given matrix .
std::pair< vector< T >, double > operator()(vector_const_view< T > b, std::optional< long >={}) const
Solve the least-square problem for a given right hand side vector .
int n_var() const
Get the number of variables of the given problem.
std::pair< matrix< T >, double > operator()(matrix_const_view< T > B, std::optional< long >={}) const
Solve the least-square problem for a given right hand side matrix .
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)
Lazy, coefficient-wise sqrt function for non-matrix nda::Array types.
auto conj(T t)
Get the complex conjugate of a scalar.
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.
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.
int n_var() const
Get the number of variables of the given problem.
array< double, 1 > const & S_vec() const
Get the singular value array.
std::pair< matrix< dcomplex >, double > operator()(matrix_const_view< dcomplex > B, std::optional< long > inner_matrix_dim={}) const
Solve the least-square problem for a given right hand side matrix .
gelss_worker_hermitian(matrix< dcomplex > A_)
Construct a new worker object for a given matrix .