29 #ifndef _TENSOR_RSVD_IMPL_HPP_
30 #define _TENSOR_RSVD_IMPL_HPP_
43 namespace random_tensor {
49 template <
typename tensor_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>();
81 template <
template <
typename>
class Matrix,
typename C>
84 const size_t target_rank,
const size_t oversamp) {
85 assert(axes_row.
size() > 0);
86 assert(axes_col.
size() > 0);
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();
94 Axes axes = axes_row + axes_col;
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;
111 range(0, rank_row),
Axes(rank_row), q, r);
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);
124 template <
template <
typename>
class Matrix,
typename C>
127 const size_t target_rank) {
128 return rsvd(a, axes_row, axes_col, u, s, vt, target_rank, target_rank);
150 template <
template <
typename>
class Matrix,
typename C,
typename Func1,
152 int rsvd(Func1 &multiply_row, Func2 &multiply_col,
const Shape &shape_row,
155 const size_t oversamp) {
156 const size_t rank_row = shape_row.
size();
157 const size_t rank_col = shape_col.
size();
162 Shape shape_omega = shape_col;
163 shape_omega.
resize(rank_col + 1);
164 shape_omega[rank_col] = target_rank + oversamp;
170 qr(multiply_col(omega),
range(0, rank_row),
Axes(rank_row), q, r);
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);
183 template <
template <
typename>
class Matrix,
typename C,
typename Func1,
185 int rsvd(Func1 &multiply_row, Func2 &multiply_col,
const Shape &shape_row,
188 return rsvd(multiply_row, multiply_col, shape_row, shape_col, u, s, vt,
189 target_rank, target_rank);
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.