mptensor v0.6.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 "mptensor/complex.hpp"
36#include "mptensor/index.hpp"
38#include "mptensor/rsvd.hpp"
39#include "mptensor/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 <typename MatrixType>
82int rsvd(const Tensor<MatrixType> &a, const Axes &axes_row, const Axes &axes_col,
83 Tensor<MatrixType> &u, std::vector<double> &s, Tensor<MatrixType> &vt,
84 const size_t target_rank, const size_t oversamp) {
85 if (target_rank == 0) {
86 throw std::invalid_argument("mptensor: rsvd() target_rank must be positive");
87 }
90 assert(row.size() > 0);
91 assert(col.size() > 0);
93
94 const size_t rank = a.rank();
95 const size_t rank_row = row.size();
96 const size_t rank_col = col.size();
97 int info;
98
99 const internal::UAxes axes = row + col;
101 const internal::UShape &shape = a_t.internal_shape();
102
104 {
107 for (size_t i = 0; i < rank_col; ++i) shape_omega[i] = shape[i + rank_row];
109
110 // Tensor<Matrix,C> omega = random_tensor<C>(a.get_comm(), shape_omega);
113
116 range(0, rank_row), Axes(rank_row), q, r);
117 }
118
120 Axes(0), range(1, rank_col + 1), u, s, vt);
121 s.resize(target_rank);
122 u = slice(u, 1, 0, target_rank);
123 vt = slice(vt, 0, 0, target_rank);
124 u = tensordot(q, u, Axes(rank_row), Axes(0));
125 return info;
126};
127
129template <typename MatrixType>
130int rsvd(const Tensor<MatrixType> &a, const Axes &axes_row, const Axes &axes_col,
131 Tensor<MatrixType> &u, std::vector<double> &s, Tensor<MatrixType> &vt,
132 const size_t target_rank) {
134};
135
137
155template <typename MatrixType, typename Func1,
156 typename Func2>
158 const Shape &shape_col, Tensor<MatrixType> &u, std::vector<double> &s,
159 Tensor<MatrixType> &vt, const size_t target_rank,
160 const size_t oversamp) {
161 if (target_rank == 0) {
162 throw std::invalid_argument("mptensor: rsvd() target_rank must be positive");
163 }
164 (void)to_internal_shape(shape_row); // validate
165 const size_t rank_row = shape_row.size();
166 const size_t rank_col = shape_col.size();
167
168 int info;
170 {
172 shape_omega.resize(rank_col + 1);
174
177
180 }
181
182 info = svd(multiply_row(conj(q)), Axes(0), range(1, rank_col + 1), u, s, vt);
183 s.resize(target_rank);
184 u = slice(u, 1, 0, target_rank);
185 vt = slice(vt, 0, 0, target_rank);
186 u = tensordot(q, u, Axes(rank_row), Axes(0));
187
188 return info;
189}
190
192template <typename MatrixType, typename Func1,
193 typename Func2>
195 const Shape &shape_col, Tensor<MatrixType> &u, std::vector<double> &s,
196 Tensor<MatrixType> &vt, const size_t target_rank) {
199};
200
202
206inline void set_seed(unsigned int seed) { random_tensor::set_seed(seed); };
207
208} // namespace mptensor
209
210#endif // _TENSOR_RSVD_IMPL_HPP_
Definition index.hpp:102
void resize(size_t n)
Definition index.hpp:137
size_t size() const
Definition index.hpp:135
size_t rank() const
Rank of tensor.
Definition tensor_impl.hpp:184
const comm_type & get_comm() const
Communicator.
Definition tensor_impl.hpp:236
Define the type of a complex number.
std::complex< double > complex
Definition complex.hpp:38
int qr(const Tensor< MatrixType > &a, Tensor< MatrixType > &q, Tensor< MatrixType > &r)
QR decomposition of matrix (rank-2 tensor)
Definition tensor_impl.hpp:2050
int svd(const Tensor< MatrixType > &a, std::vector< double > &s)
Singular value decomposition for rank-2 tensor (matrix)
Definition tensor_impl.hpp:1782
internal::UShape to_internal_shape(const Shape &s)
Definition index.hpp:366
Index range(const std::ptrdiff_t start, const std::ptrdiff_t stop, const std::ptrdiff_t step)
Arithmetic sequence start, start+step, ... up to (not including) stop.
Definition index.hpp:401
internal::UAxes normalize_axes(const Axes &axes, size_t rank)
Definition index.hpp:323
Index Axes
Definition index.hpp:212
Tensor< MatrixType > transpose_impl(const Tensor< MatrixType > &a, const UAxes &axes, size_t urank_new)
Definition tensor_impl.hpp:1116
constexpr normalized_t normalized
Definition tensor.hpp:55
Tensor< MatrixType > tensordot(const Tensor< MatrixType > &a, const Tensor< MatrixType > &b, const Axes &axes_a, const Axes &axes_b)
Compute tensor dot product.
Definition tensor_impl.hpp:1706
Tensor< MatrixType > conj(Tensor< MatrixType > t)
Take conjugate of each element.
void set_seed(unsigned int seed)
Set seed for random number generator.
Definition rsvd_impl.hpp:206
int rsvd(const Tensor< MatrixType > &a, const Axes &axes_row, const Axes &axes_col, Tensor< MatrixType > &u, std::vector< double > &s, Tensor< MatrixType > &vt, const size_t target_rank, const size_t oversamp)
Singular value decomposition by randomized algorithm.
Definition rsvd_impl.hpp:82
Tensor< MatrixType > slice(const Tensor< MatrixType > &a, std::ptrdiff_t n_axes, std::ptrdiff_t i_begin, std::ptrdiff_t i_end)
Slice a tensor.
Definition tensor_impl.hpp:1265
header file of BasicIndex class template
List of header files for matrix classes.
mptensor::complex complex
Definition matrix_lapack.cc:36
bool check_svd_axes(const internal::UAxes &axes_row, const internal::UAxes &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
Randomized algorithm for singular value decomposition.
Tensor class.