mptensor v0.4.0
Parallel Library for Tensor Network Methods
Loading...
Searching...
No Matches
rsvd_impl.hpp
Go to the documentation of this file.
1/*
2 mptensor - Parallel Library for Tensor Network Methods
3
4 Copyright 2016 Satoshi Morita
5
6 mptensor is free software: you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation, either version 3 of the
9 License, or (at your option) any later version.
10
11 mptensor is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with mptensor. If not, see
18 <https://www.gnu.org/licenses/>.
19*/
20
29#ifndef _TENSOR_RSVD_IMPL_HPP_
30#define _TENSOR_RSVD_IMPL_HPP_
31
32#include <cassert>
33#include <vector>
34
35#include "complex.hpp"
36#include "index.hpp"
37#include "matrix.hpp"
38#include "rsvd.hpp"
39#include "tensor.hpp"
40
41namespace mptensor {
42
43namespace random_tensor {
44
45template <typename C>
47void set_seed(unsigned int seed);
48
49template <typename tensor_t>
50void fill(tensor_t &t) {
51 const size_t n = t.local_size();
52 for (size_t i = 0; i < n; ++i) {
54 }
55}
56
57} // namespace random_tensor
58
60
81template <template <typename> class Matrix, typename C>
82int rsvd(const Tensor<Matrix, C> &a, const Axes &axes_row, const Axes &axes_col,
83 Tensor<Matrix, C> &u, std::vector<double> &s, Tensor<Matrix, C> &vt,
84 const size_t target_rank, const size_t oversamp) {
85 assert(axes_row.size() > 0);
86 assert(axes_col.size() > 0);
88
89 const size_t rank = a.rank();
90 const size_t rank_row = axes_row.size();
91 const size_t rank_col = axes_col.size();
92 int info;
93
96 const Shape &shape = a_t.shape();
97
99 {
102 for (size_t i = 0; i < rank_col; ++i) shape_omega[i] = shape[i + rank_row];
104
105 // Tensor<Matrix,C> omega = random_tensor<C>(a.get_comm(), shape_omega);
108
111 range(0, rank_row), Axes(rank_row), q, r);
112 }
113
115 Axes(0), range(1, rank_col + 1), u, s, vt);
116 s.resize(target_rank);
117 u = slice(u, 1, 0, target_rank);
118 vt = slice(vt, 0, 0, target_rank);
119 u = tensordot(q, u, Axes(rank_row), Axes(0));
120 return info;
121};
122
124template <template <typename> class Matrix, typename C>
125int rsvd(const Tensor<Matrix, C> &a, const Axes &axes_row, const Axes &axes_col,
126 Tensor<Matrix, C> &u, std::vector<double> &s, Tensor<Matrix, C> &vt,
127 const size_t target_rank) {
129};
130
132
150template <template <typename> class Matrix, typename C, typename Func1,
151 typename Func2>
153 const Shape &shape_col, Tensor<Matrix, C> &u, std::vector<double> &s,
154 Tensor<Matrix, C> &vt, const size_t target_rank,
155 const size_t oversamp) {
156 const size_t rank_row = shape_row.size();
157 const size_t rank_col = shape_col.size();
158
159 int info;
161 {
165
168
171 }
172
173 info = svd(multiply_row(conj(q)), Axes(0), range(1, rank_col + 1), u, s, vt);
174 s.resize(target_rank);
175 u = slice(u, 1, 0, target_rank);
176 vt = slice(vt, 0, 0, target_rank);
177 u = tensordot(q, u, Axes(rank_row), Axes(0));
178
179 return info;
180}
181
183template <template <typename> class Matrix, typename C, typename Func1,
184 typename Func2>
186 const Shape &shape_col, Tensor<Matrix, C> &u, std::vector<double> &s,
187 Tensor<Matrix, C> &vt, const size_t target_rank) {
190};
191
193
197inline void set_seed(unsigned int seed) { random_tensor::set_seed(seed); };
198
199} // namespace mptensor
200
201#endif // _TENSOR_RSVD_IMPL_HPP_
Definition index.hpp:39
void resize(size_t n)
Definition index.hpp:81
size_t rank() const
Rank of tensor.
Definition tensor_impl.hpp:164
const comm_type & get_comm() const
Communicator.
Definition tensor_impl.hpp:216
Define the type of a complex number.
std::complex< double > complex
Definition complex.hpp:38
int svd(const Tensor< Matrix, C > &a, std::vector< double > &s)
Singular value decomposition for rank-2 tensor (matrix)
Definition tensor_impl.hpp:1722
int qr(const Tensor< Matrix, C > &a, Tensor< Matrix, C > &q, Tensor< Matrix, C > &r)
QR decomposition of matrix (rank-2 tensor)
Definition tensor_impl.hpp:1974
Tensor< Matrix, C > tensordot(const Tensor< Matrix, C > &a, const Tensor< Matrix, C > &b, const Axes &axes_a, const Axes &axes_b)
Compute tensor dot product.
Definition tensor_impl.hpp:1648
Tensor< Matrix, C > conj(Tensor< Matrix, C > t)
Take conjugate of each element.
int rsvd(const Tensor< Matrix, C > &a, const Axes &axes_row, const Axes &axes_col, Tensor< Matrix, C > &u, std::vector< double > &s, Tensor< Matrix, C > &vt, const size_t target_rank, const size_t oversamp)
Singular value decomposition by randomized algorithm.
Definition rsvd_impl.hpp:82
void set_seed(unsigned int seed)
Set seed for random number generator.
Definition rsvd_impl.hpp:197
Tensor< Matrix, C > transpose(Tensor< Matrix, C > a, const Axes &axes)
Transposition of tensor with lazy evaluation.
Definition tensor_impl.hpp:1041
Tensor< Matrix, C > slice(const Tensor< Matrix, C > &a, size_t n_axes, size_t i_begin, size_t i_end)
Slice a tensor.
Definition tensor_impl.hpp:1215
header file of Index class
List of header files for matrix classes.
mptensor::complex complex
Definition matrix_lapack.cc:36
bool check_svd_axes(const Axes &axes_row, const Axes &axes_col, size_t rank)
Definition tensor.cc:76
void set_seed(unsigned int seed)
void fill(tensor_t &t)
Definition rsvd_impl.hpp:50
Definition complex.hpp:34
Index range(const size_t start, const size_t stop)
Create an increasing sequence. it is similar to range() in python.
Definition index.cc:91
Index Axes
Definition tensor.hpp:45
Randomized algorithm for singular value decomposition.
Tensor class.