TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
gelss_worker.hpp
Go to the documentation of this file.
1// Copyright (c) 2020-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: Jason Kaye, Olivier Parcollet, Nils Wentzell
21#pragma once
22
23#include "./gesvd.hpp"
24#include "../algorithms.hpp"
25#include "../basic_array.hpp"
26#include "../declarations.hpp"
27#include "../exceptions.hpp"
30#include "../linalg.hpp"
33
34#include <itertools/itertools.hpp>
35
36#include <algorithm>
37#include <array>
38#include <cmath>
39#include <complex>
40#include <optional>
41#include <utility>
42#include <vector>
43
44namespace nda::lapack {
45
67 template <typename T>
69 // Number of rows (M) and columns (N) of the Matrix A.
70 long M, N;
71
72 // FIXME Do we need to store it ? only use n_var
73 // Matrix to be decomposed by SVD.
74 matrix<T> A;
75
76 // (Pseudo) Inverse of A, i.e. V * Diag(S_vec)^{-1} * UH, for the least square problem.
77 matrix<T> V_x_InvS_x_UH;
78
79 // Part of UH fixing the error of the least square problem.
80 matrix<T> UH_NULL;
81
82 // Array containing the singular values.
83 array<double, 1> s_vec;
84
85 public:
90 int n_var() const { return A.extent(1); }
91
96 [[nodiscard]] array<double, 1> const &S_vec() const { return s_vec; }
97
107 gelss_worker(matrix<T> A_) : M(A_.extent(0)), N(A_.extent(1)), A(std::move(A_)), s_vec(std::min(M, N)) {
108 if (N > M) NDA_RUNTIME_ERROR << "Error in nda::lapack::gelss_worker: Matrix A cannot have more columns than rows";
109
110 // initialize matrices
111 matrix<T, F_layout> A_FL{A};
112 matrix<T, F_layout> U(M, M);
113 matrix<T, F_layout> VH(N, N);
114
115 // calculate the SVD: A = U * Diag(S_vec) * VH
116 gesvd(A_FL, s_vec, U, VH);
117
118 // calculate the matrix V * Diag(S_vec)^{-1} * UH for the least square procedure
119 matrix<double, F_layout> S_inv(N, M);
120 S_inv = 0.;
121 for (long i : range(std::min(M, N))) S_inv(i, i) = 1.0 / s_vec(i);
122 V_x_InvS_x_UH = dagger(VH) * S_inv * dagger(U);
123
124 // read off UH_Null for defining the error of the least square procedure
125 if (N < M) UH_NULL = dagger(U)(range(N, M), range(M));
126 }
127
134 std::pair<matrix<T>, double> operator()(matrix_const_view<T> B, std::optional<long> /* inner_matrix_dim */ = {}) const {
135 using std::sqrt;
136 double err = 0.0;
137 if (M != N) {
138 std::vector<double> err_vec;
139 for (long i : range(B.shape()[1])) err_vec.push_back(frobenius_norm(UH_NULL * B(range::all, range(i, i + 1))) / sqrt(B.shape()[0]));
140 err = *std::max_element(err_vec.begin(), err_vec.end());
141 }
142 return std::make_pair(V_x_InvS_x_UH * B, err);
143 }
144
151 std::pair<vector<T>, double> operator()(vector_const_view<T> b, std::optional<long> /*inner_matrix_dim*/ = {}) const {
152 using std::sqrt;
153 double err = 0.0;
154 if (M != N) { err = norm(UH_NULL * b) / sqrt(b.size()); }
155 return std::make_pair(V_x_InvS_x_UH * b, err);
156 }
157 };
158
166 private:
167 // Complex double type.
168 using dcomplex = std::complex<double>;
169
170 // Matrix to be decomposed by SVD.
172
173 // Solver for the associated real-valued least-squares problem.
175
176 // Solver for the associated real-valued least-squares problem imposing hermiticity.
177 gelss_worker<dcomplex> _lss_matrix;
178
179 public:
184 int n_var() const { return static_cast<int>(A.extent(1)); }
185
190 array<double, 1> const &S_vec() const { return _lss.S_vec(); }
191
196 gelss_worker_hermitian(matrix<dcomplex> A_) : A(std::move(A_)), _lss(A), _lss_matrix(vstack(A, conj(A))) {}
197
204 std::pair<matrix<dcomplex>, double> operator()(matrix_const_view<dcomplex> B, std::optional<long> inner_matrix_dim = {}) const {
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;
208
209 // Construction of an inner 'adjoint' matrix by performing the following steps
210 // * reshape B from (M, M1) to (M, N, d, d)
211 // * for each M and N take the adjoint matrix (d, d)
212 // * reshape to (M, M)
213 auto inner_adjoint = [d](auto &M) {
214 auto idx_map = M.indexmap();
215 auto l = idx_map.lengths();
216 //auto s = idx_map.strides();
217
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);
220
221 // We reshape the Matrix into a dim=4 array and swap the two innermost indices
222
223 // FIXME OLD CODE SURPRESS AFTER PORTING
224 // FIXME We would like to write: transpose(reshape(idx_map, {l[0], N, d, d}), {0, 1, 3, 2})
225 // auto idx_map_inner_transpose = array_view<dcomplex, 4>::layout_t{{l[0], N, d, d}, {s[0], d * d * s[1], s[1], d * s[1]}};
226 // Deep copy
227 //array<dcomplex, 4> arr_dag = conj(array_const_view<dcomplex, 4>{idx_map_inner_transpose, M.storage()});
228 //return matrix<dcomplex>{matrix<dcomplex>::layout_t{l, s}, std::move(arr_dag).storage()};
229
230 // FIXME C++20 remove encode
231 array<dcomplex, 4> arr_dag = conj(permuted_indices_view<encode(std::array{0, 1, 3, 2})>(reshape(M, std::array{l[0], N, d, d})));
232
233 return matrix<dcomplex>{reshape(std::move(arr_dag), l)}; // move into a matrix
234 };
235
236 // Solve the enlarged system vstack(A, A*) * x = vstack(B, B_dag)
237 matrix<dcomplex> B_dag = inner_adjoint(B);
238 auto B_stack = vstack(B, B_dag);
239 auto [x, err] = _lss_matrix(B_stack);
240
241 // Resymmetrize results to cure small hermiticity violations
242 return {0.5 * (x + inner_adjoint(x)), err};
243 }
244 };
245
248} // namespace nda::lapack
Provides various algorithms to be used with nda::Array objects.
Provides the generic class for arrays.
A generic view of a multi-dimensional array.
auto const & shape() const noexcept
Get the shape of the view/array.
long size() const noexcept
Get the total size of the view/array.
A generic multi-dimensional array.
long extent(int i) const noexcept
Get the extent of the ith dimension.
Layout that specifies how to map multi-dimensional indices to a linear/flat index.
Definition idx_map.hpp:103
std::array< long, Rank > const & lengths() const noexcept
Get the extents of all dimensions.
Definition idx_map.hpp:178
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, 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:75
double norm(A const &a, double p=2.0)
Calculate the p-norm of an nda::ArrayOfRank<1> object with scalar values. The p-norm is defined as.
Definition norm.hpp:58
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.
Provides functions to transform the memory layout of an nda::basic_array or nda::basic_array_view.
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.
Worker class for solving linear least square problems for hermitian tail-fitting.
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 .