mptensor  v0.3.0
Parallel Library for Tensor Network Methods
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 
41 namespace mptensor {
42 
43 namespace random_tensor {
44 
45 template <typename C>
47 void set_seed(unsigned int seed);
48 
49 template <typename tensor_t>
50 void fill(tensor_t &t) {
51  const size_t n = t.local_size();
52  for (size_t i = 0; i < n; ++i) {
53  t[i] = uniform_dist<typename tensor_t::value_type>();
54  }
55 }
56 
57 } // namespace random_tensor
58 
60 
81 template <template <typename> class Matrix, typename C>
82 int 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);
87  assert(debug::check_svd_axes(axes_row, axes_col, a.rank()));
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 
94  Axes axes = axes_row + axes_col;
95  Tensor<Matrix, C> a_t = transpose(a, axes, rank_row);
96  const Shape &shape = a_t.shape();
97 
99  {
100  Shape shape_omega;
101  shape_omega.resize(rank_col + 1);
102  for (size_t i = 0; i < rank_col; ++i) shape_omega[i] = shape[i + rank_row];
103  shape_omega[rank_col] = target_rank + oversamp;
104 
105  // Tensor<Matrix,C> omega = random_tensor<C>(a.get_comm(), shape_omega);
106  Tensor<Matrix, C> omega(a.get_comm(), shape_omega, rank_col);
107  random_tensor::fill(omega);
108 
110  qr(tensordot(a_t, omega, range(rank_row, rank), range(0, rank_col)),
111  range(0, rank_row), Axes(rank_row), q, r);
112  }
113 
114  info = svd(tensordot(conj(q), a_t, range(0, rank_row), range(0, rank_row)),
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 
124 template <template <typename> class Matrix, typename C>
125 int 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) {
128  return rsvd(a, axes_row, axes_col, u, s, vt, target_rank, target_rank);
129 };
130 
132 
150 template <template <typename> class Matrix, typename C, typename Func1,
151  typename Func2>
152 int rsvd(Func1 &multiply_row, Func2 &multiply_col, const Shape &shape_row,
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  {
162  Shape shape_omega = shape_col;
163  shape_omega.resize(rank_col + 1);
164  shape_omega[rank_col] = target_rank + oversamp;
165 
166  Tensor<Matrix, C> omega(u.get_comm(), shape_omega, rank_col);
167  random_tensor::fill(omega);
168 
170  qr(multiply_col(omega), range(0, rank_row), Axes(rank_row), q, r);
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 
183 template <template <typename> class Matrix, typename C, typename Func1,
184  typename Func2>
185 int rsvd(Func1 &multiply_row, Func2 &multiply_col, const Shape &shape_row,
186  const Shape &shape_col, Tensor<Matrix, C> &u, std::vector<double> &s,
187  Tensor<Matrix, C> &vt, const size_t target_rank) {
188  return rsvd(multiply_row, multiply_col, shape_row, shape_col, u, s, vt,
189  target_rank, target_rank);
190 };
191 
193 
197 inline 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 size() const
Definition: index.hpp:79
Tensor class. The main object of mptensor.
Definition: tensor.hpp:54
size_t rank() const
Rank of tensor.
Definition: tensor_impl.hpp:164
const comm_type & get_comm() const
Communicator.
Definition: tensor_impl.hpp:216
const Shape & shape() const
Shape of tensor.
Definition: tensor_impl.hpp:158
Define the type of a complex number.
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.
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
tuple shape
Definition: output.py:28
Randomized algorithm for singular value decomposition.
Tensor class.