mptensor v0.4.0
Parallel Library for Tensor Network Methods
Loading...
Searching...
No Matches
tensor.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_HPP_
30#define _TENSOR_HPP_
31
32#include <algorithm>
33#include <cassert>
34#include <iostream>
35#include <string>
36#include <vector>
37
38#include "complex.hpp"
39#include "index.hpp"
40#include "matrix.hpp"
41
42namespace mptensor {
43
44/* Alias */
45typedef Index Axes;
46typedef Index Shape;
47
48/* Class definition */
50
53template <template <typename> class Matrix, typename C>
54class Tensor {
55 public:
56 typedef C value_type;
58 typedef typename Matrix<C>::comm_type comm_type;
63 Tensor();
64 explicit Tensor(const Shape &);
65 explicit Tensor(const comm_type &);
66 Tensor(const comm_type &, const Shape &);
67 Tensor(const comm_type &, const Shape &, size_t upper_rank);
69 Tensor(const comm_type &, const std::vector<C> &);
71
72 const Shape &shape() const;
73 size_t rank() const;
74 size_t ndim() const;
75 size_t local_size() const;
76 size_t get_upper_rank() const;
77 const Axes &get_axes_map() const;
78
79 const Matrix<C> &get_matrix() const;
81
82 const comm_type &get_comm() const;
83 int get_comm_size() const;
84 int get_comm_rank() const;
85
86 Index global_index(size_t i) const;
87 void global_index_fast(size_t i, Index &idx) const;
88 void local_position(const Index &idx, int &comm_rank,
89 size_t &local_idx) const;
90
91 const C &operator[](size_t local_idx) const;
92 C &operator[](size_t local_idx);
93
94 bool get_value(const Index &idx, C &val) const;
95 void set_value(const Index &idx, C val);
96
97 void print_info(std::ostream &out, const std::string &tag = "") const;
98 void print_info_mpi(std::ostream &, const std::string &tag = "") const;
99
100 void save(const std::string &filename) const;
101 void load(const std::string &filename);
102
104
105 template <typename D>
106 Tensor<Matrix, C> &multiply_vector(const std::vector<D> &vec, size_t n_axes);
107 template <typename D0, typename D1>
108 Tensor<Matrix, C> &multiply_vector(const std::vector<D0> &vec0,
109 size_t n_axes0,
110 const std::vector<D1> &vec1,
111 size_t n_axes1);
112 template <typename D0, typename D1, typename D2>
114 const std::vector<D0> &vec0, size_t n_axes0, const std::vector<D1> &vec1,
115 size_t n_axes1, const std::vector<D2> &vec2, size_t n_axes2);
116 template <typename D0, typename D1, typename D2, typename D3>
118 const std::vector<D0> &vec0, size_t n_axes0, const std::vector<D1> &vec1,
119 size_t n_axes1, const std::vector<D2> &vec2, size_t n_axes2,
120 const std::vector<D3> &vec3, size_t n_axes3);
121
122 Tensor<Matrix, C> &set_slice(const Tensor &a, size_t n_axes, size_t i_begin,
123 size_t i_end);
125 const Index &index_end);
126
128 std::vector<C> flatten();
129
135
136 template <typename UnaryOperation>
138
139 void prep_global_to_local() const;
140 void prep_local_to_global() const;
141
142 void make_l2g_map() const;
143 void global_index_l2g_map(size_t lindex, size_t gindex[]) const;
144 void global_index_l2g_map_transpose(size_t lindex, const size_t axes_trans[],
145 size_t index_new[]) const;
146
147 void local_position_fast(size_t g_row, size_t g_col, int &comm_rank,
148 size_t &local_idx) const;
149
150 private:
151 Matrix<C> Mat;
152 Shape Dim;
153
154 size_t upper_rank;
155
157
162 Axes axes_map;
163
164 void init(const Shape &, size_t upper_rank);
165 void init(const Shape &, size_t upper_rank, const Axes &map);
166 void change_configuration(const size_t new_upper_rank,
167 const Axes &new_axes_map);
168 bool local_index(const Index &, size_t &i) const;
169
170 mutable std::vector<size_t> l2g_map_row;
171 mutable std::vector<size_t> l2g_map_col;
172
173 void save_ver_0_2(const char *filename) const;
174 void load_ver_0_2(const char *filename);
175};
176
177/* Operations */
180template <template <typename> class Matrix, typename C>
182template <template <typename> class Matrix, typename C>
184 size_t urank_new);
185template <template <typename> class Matrix, typename C>
187template <template <typename> class Matrix, typename C>
189 size_t i_begin, size_t i_end);
190template <template <typename> class Matrix, typename C>
192 const Index &index_end);
193template <template <typename> class Matrix, typename C>
196
199template <template <typename> class Matrix, typename C>
200C trace(const Tensor<Matrix, C> &a);
201template <template <typename> class Matrix, typename C>
202C trace(const Tensor<Matrix, C> &a, const Axes &axes_1, const Axes &axes_2);
203template <template <typename> class Matrix, typename C>
205 const Axes &axes_a, const Axes &axes_b);
206template <template <typename> class Matrix, typename C>
208 const Axes &axes_2);
209template <template <typename> class Matrix, typename C>
211 const Tensor<Matrix, C> &b, const Axes &axes_a,
212 const Axes &axes_b);
213template <template <typename> class Matrix, typename C>
215 const Tensor<Matrix, C> &b);
217
220template <template <typename> class Matrix, typename C>
221int svd(const Tensor<Matrix, C> &a, std::vector<double> &s);
222template <template <typename> class Matrix, typename C>
224 std::vector<double> &s, Tensor<Matrix, C> &vt);
225template <template <typename> class Matrix, typename C>
226int svd(const Tensor<Matrix, C> &a, const Axes &axes_row, const Axes &axes_col,
227 std::vector<double> &s);
228template <template <typename> class Matrix, typename C>
229int svd(const Tensor<Matrix, C> &a, const Axes &axes_row, const Axes &axes_col,
230 Tensor<Matrix, C> &u, std::vector<double> &s, Tensor<Matrix, C> &vt);
231
232template <template <typename> class Matrix, typename C>
233int psvd(const Tensor<Matrix, C> &a, std::vector<double> &s,
234 const size_t target_rank);
235template <template <typename> class Matrix, typename C>
237 std::vector<double> &s, Tensor<Matrix, C> &vt,
238 const size_t target_rank);
239template <template <typename> class Matrix, typename C>
240int psvd(const Tensor<Matrix, C> &a, const Axes &axes_row, const Axes &axes_col,
241 std::vector<double> &s, const size_t target_rank);
242template <template <typename> class Matrix, typename C>
243int psvd(const Tensor<Matrix, C> &a, const Axes &axes_row, const Axes &axes_col,
244 Tensor<Matrix, C> &u, std::vector<double> &s, Tensor<Matrix, C> &vt,
245 const size_t target_rank);
246
247template <template <typename> class Matrix, typename C>
249template <template <typename> class Matrix, typename C>
250int qr(const Tensor<Matrix, C> &a, const Axes &axes_row, const Axes &axes_col,
252
253template <template <typename> class Matrix, typename C>
254int eigh(const Tensor<Matrix, C> &a, std::vector<double> &eigval,
256template <template <typename> class Matrix, typename C>
257int eigh(const Tensor<Matrix, C> &a, std::vector<double> &eigval);
258template <template <typename> class Matrix, typename C>
259int eigh(const Tensor<Matrix, C> &a, const Axes &axes_row, const Axes &axes_col,
260 std::vector<double> &eigval, Tensor<Matrix, C> &eigvec);
261template <template <typename> class Matrix, typename C>
262int eigh(const Tensor<Matrix, C> &a, const Axes &axes_row, const Axes &axes_col,
263 std::vector<double> &eigval);
264template <template <typename> class Matrix, typename C>
265int eigh(const Tensor<Matrix, C> &a, const Axes &axes_row_a,
266 const Axes &axes_col_a, const Tensor<Matrix, C> &b,
267 const Axes &axes_row_b, const Axes &axes_col_b,
268 std::vector<double> &eigval, Tensor<Matrix, C> &eigvec);
269
270
271template <template <typename> class Matrix, typename C>
272int eig(const Tensor<Matrix, C> &a, std::vector<complex> &eigval,
274template <template <typename> class Matrix, typename C>
275int eig(const Tensor<Matrix, C> &a, std::vector<complex> &eigval);
276template <template <typename> class Matrix, typename C>
277int eig(const Tensor<Matrix, C> &a, const Axes &axes_row, const Axes &axes_col,
278 std::vector<complex> &eigval, Tensor<Matrix, complex> &eigvec);
279template <template <typename> class Matrix, typename C>
280int eig(const Tensor<Matrix, C> &a, const Axes &axes_row, const Axes &axes_col,
281 std::vector<complex> &eigval);
283
286template <template <typename> class Matrix, typename C>
287int solve(const Tensor<Matrix, C> &a, const std::vector<C> &b,
288 std::vector<C> &x);
289template <template <typename> class Matrix, typename C>
290int solve(const Tensor<Matrix, C> &a, const Tensor<Matrix, C> &b,
292template <template <typename> class Matrix, typename C>
293int solve(const Tensor<Matrix, C> &a, const Tensor<Matrix, C> &b,
295 const Axes &axes_row_b, const Axes &axes_col_b);
297
300template <template <typename> class Matrix, typename C>
302template <template <typename> class Matrix, typename C>
304template <template <typename> class Matrix, typename C>
306 const Tensor<Matrix, C> &rhs);
307template <template <typename> class Matrix, typename C>
309 const Tensor<Matrix, C> &rhs);
310template <template <typename> class Matrix, typename C, typename D>
312template <template <typename> class Matrix, typename C, typename D>
314template <template <typename> class Matrix, typename C, typename D>
317
320template <template <typename> class Matrix, typename C>
323template <template <typename> class Matrix, typename C>
326
327template <template <typename> class Matrix, typename C>
328double max(const Tensor<Matrix, C> &t);
329template <template <typename> class Matrix, typename C>
330double min(const Tensor<Matrix, C> &t);
331template <template <typename> class Matrix, typename C>
332double max_abs(const Tensor<Matrix, C> &t);
333template <template <typename> class Matrix, typename C>
334double min_abs(const Tensor<Matrix, C> &t);
336
339template <template <typename> class Matrix, typename C>
340std::ostream &operator<<(std::ostream &out, const Tensor<Matrix, C> &t);
342
343} // namespace mptensor
344
345#include "tensor_impl.hpp"
346#include "rsvd.hpp"
347#include "file_io/save.hpp"
348#include "file_io/load.hpp"
349
350#endif // _TENSOR_HPP_
Definition index.hpp:39
Tensor class. The main object of mptensor.
Definition tensor.hpp:54
void set_value(const Index &idx, C val)
Set an element.
Definition tensor_impl.hpp:570
void print_info(std::ostream &out, const std::string &tag="") const
Output information of tensor.
Definition tensor_impl.hpp:424
size_t rank() const
Rank of tensor.
Definition tensor_impl.hpp:164
void global_index_fast(size_t i, Index &idx) const
Convert local index to global index fast.
Definition tensor_impl.hpp:525
void print_info_mpi(std::ostream &, const std::string &tag="") const
Output information of tensor.
Definition tensor_impl.hpp:442
size_t ndim() const
Rank of tensor.
Definition tensor_impl.hpp:173
void load(const std::string &filename)
Load a tensor from files.
Definition load.hpp:53
void prep_local_to_global() const
Preprocess for fast conversion from local index to global one.
Definition tensor_impl.hpp:258
Tensor< Matrix, C > & operator/=(C rhs)
Scalar-division assignment.
Definition tensor_impl.hpp:1006
void local_position_fast(size_t g_row, size_t g_col, int &comm_rank, size_t &local_idx) const
Definition tensor_impl.hpp:376
Tensor< Matrix, C > & map(UnaryOperation op)
Apply a unary operation to each element.
Definition tensor_impl.hpp:1026
void prep_global_to_local() const
Preprocess for fast conversion from global index to local one.
Definition tensor_impl.hpp:252
const Matrix< C > & get_matrix() const
distributed Matrix
Definition tensor_impl.hpp:203
Tensor< Matrix, C > & set_slice(const Tensor &a, size_t n_axes, size_t i_begin, size_t i_end)
Inverse of slice().
Definition tensor_impl.hpp:846
void local_position(const Index &idx, int &comm_rank, size_t &local_idx) const
Calculate rank which has the given global index and its local index.
Definition tensor_impl.hpp:584
Tensor< Matrix, C > & operator=(C rhs)
Initialize all elements by rhs.
Definition tensor_impl.hpp:1013
C value_type
double or complex
Definition tensor.hpp:56
Tensor< lapack::Matrix, C > gather()
Gather all elements into a non-distributed tensor.
Definition tensor_impl.hpp:955
void make_l2g_map() const
Preprocess for fast conversion from local index to global one.
Definition tensor_impl.hpp:264
size_t local_size() const
Size of local storage.
Definition tensor_impl.hpp:179
void global_index_l2g_map(size_t lindex, size_t gindex[]) const
Convert local index to global index using l2g_map.
Definition tensor_impl.hpp:320
std::vector< C > flatten()
Return a copy of the flattened vector.
Definition tensor_impl.hpp:971
int get_comm_rank() const
Rank of process.
Definition tensor_impl.hpp:228
Tensor< Matrix, C > & multiply_vector(const std::vector< D > &vec, size_t n_axes)
Element-wise vector multiplication.
Definition tensor_impl.hpp:700
int get_comm_size() const
Size of communicator.
Definition tensor_impl.hpp:222
Tensor< Matrix, C > & operator*=(C rhs)
Scalar-multiplication assignment.
Definition tensor_impl.hpp:999
const Axes & get_axes_map() const
Map of axes for lazy evaluation of transpose.
Definition tensor_impl.hpp:197
Index global_index(size_t i) const
Convert local index to global index.
Definition tensor_impl.hpp:495
const comm_type & get_comm() const
Communicator.
Definition tensor_impl.hpp:216
Tensor< Matrix, C > & transpose(const Axes &axes)
Transposition of tensor with lazy evaluation.
Definition tensor_impl.hpp:667
Matrix< C >::comm_type comm_type
type of communicator.
Definition tensor.hpp:58
const Shape & shape() const
Shape of tensor.
Definition tensor_impl.hpp:158
Tensor< Matrix, C > & operator-=(const Tensor &rhs)
Subtraction assignment.
Definition tensor_impl.hpp:990
size_t get_upper_rank() const
Upper rank for matrix representation.
Definition tensor_impl.hpp:185
Tensor< Matrix, C > & operator+=(const Tensor &rhs)
Addition assignment.
Definition tensor_impl.hpp:981
void save(const std::string &filename) const
Save a tensor to files.
Definition save.hpp:53
void global_index_l2g_map_transpose(size_t lindex, const size_t axes_trans[], size_t index_new[]) const
Convert local index to global index of transposed tensor using l2g_map.
Definition tensor_impl.hpp:353
Matrix< C > matrix_type
type of Matrix class
Definition tensor.hpp:57
const C & operator[](size_t local_idx) const
Const array subscript operator.
Definition tensor_impl.hpp:237
bool get_value(const Index &idx, C &val) const
Get an element.
Definition tensor_impl.hpp:552
std::string filename(const std::string &prefix, int proc_size)
Definition common.hpp:32
Define the type of a complex number.
Tensor< Matrix, C > operator*(Tensor< Matrix, C > lhs, D rhs)
Tensor-scalar multiplication.
Definition tensor_impl.hpp:2579
Tensor< Matrix, C > operator-(Tensor< Matrix, C > rhs)
Unary minus.
Definition tensor_impl.hpp:2562
Tensor< Matrix, C > operator/(Tensor< Matrix, C > lhs, D rhs)
Scalar division.
Definition tensor_impl.hpp:2584
std::complex< double > complex
Definition complex.hpp:38
int eig(const Tensor< Matrix, C > &a, std::vector< complex > &eigval, Tensor< Matrix, complex > &eigvec)
Compute the eigenvalues and eigenvectors of a general matrix (rank-2 tensor)
Definition tensor_impl.hpp:2295
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 eigh(const Tensor< Matrix, C > &a, std::vector< double > &eigval, Tensor< Matrix, C > &eigvec)
Definition tensor_impl.hpp:2071
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
int psvd(const Tensor< Matrix, C > &a, std::vector< double > &s, const size_t target_rank)
Partial SVD for rank-2 tensor (matrix)
Definition tensor_impl.hpp:1866
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 > contract(const Tensor< Matrix, C > &a, const Axes &axes_1, const Axes &axes_2)
Partial trace of tensor.
Definition tensor_impl.hpp:1519
Tensor< Matrix, C > kron(const Tensor< Matrix, C > &a, const Tensor< Matrix, C > &b)
Compute the Kronecker product.
Definition tensor_impl.hpp:1612
C trace(const Tensor< Matrix, C > &a)
Trace of matrix (rank-2 tensor)
Definition tensor_impl.hpp:1409
int solve(const Tensor< Matrix, C > &a, const std::vector< C > &b, std::vector< C > &x)
Solve linear equation .
Definition tensor_impl.hpp:2447
double max(const Tensor< Matrix, C > &t)
Return the maximum element. For complex, same as max_abs().
Definition tensor_impl.hpp:2595
double min_abs(const Tensor< Matrix, C > &t)
Return minimum of the absolute value of an element.
Definition tensor_impl.hpp:2610
Tensor< Matrix, C > sqrt(Tensor< Matrix, C > t)
Take square-root of each element.
double max_abs(const Tensor< Matrix, C > &t)
Return maximum of the absolute value of an element.
Definition tensor_impl.hpp:2605
Tensor< Matrix, C > conj(Tensor< Matrix, C > t)
Take conjugate of each element.
double min(const Tensor< Matrix, C > &t)
Return the minimum element. For complex, same as min_abs().
Definition tensor_impl.hpp:2600
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
Tensor< Matrix, C > reshape(const Tensor< Matrix, C > &a, const Shape &shape_new)
Change the shape of tensor.
Definition tensor_impl.hpp:1149
Tensor< Matrix, C > extend(const Tensor< Matrix, C > &a, const Shape &shape_new)
Extend the size of a tensor.
Definition tensor_impl.hpp:1363
Tensor()
Default constructor of tensor.
Definition tensor_impl.hpp:70
header file of Index class
Header file of load function.
List of header files for matrix classes.
Definition complex.hpp:34
std::ostream & operator<<(std::ostream &os, const Index &idx)
Index Shape
Definition tensor.hpp:46
Index Axes
Definition tensor.hpp:45
Index operator+(const Index &, const Index &)
Randomized algorithm for singular value decomposition.
Header file of saver.
Implementation of tensor class.