mptensor v0.5.1
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 "mptensor/complex.hpp"
39#include "mptensor/index.hpp"
41
42namespace mptensor {
43
44/* Alias */
45using Axes = Index;
46using Shape = Index;
47
48/* Class definition */
50
53template <typename MatrixType>
54class Tensor {
55 public:
56 using value_type = typename MatrixType::value_type;
58 using comm_type = typename MatrixType::comm_type;
59
60 template <typename D>
62
65 Tensor();
66 explicit Tensor(const Shape &);
67 explicit Tensor(const comm_type &);
68 Tensor(const comm_type &, const Shape &);
69 Tensor(const comm_type &, const Shape &, size_t upper_rank);
70 Tensor(const comm_type &, const Tensor<lapack::Matrix<value_type>> &);
71 Tensor(const comm_type &, const std::vector<value_type> &);
73
74 const Shape &shape() const;
75 size_t rank() const;
76 size_t ndim() const;
77 size_t local_size() const;
78 size_t get_upper_rank() const;
79 const Axes &get_axes_map() const;
80
81 const MatrixType &get_matrix() const;
83
84 const comm_type &get_comm() const;
85 int get_comm_size() const;
86 int get_comm_rank() const;
87
88 Index global_index(size_t i) const;
89 void global_index_fast(size_t i, Index &idx) const;
90 void local_position(const Index &idx, int &comm_rank,
91 size_t &local_idx) const;
92
93 const value_type &operator[](size_t local_idx) const;
95
96 bool get_value(const Index &idx, value_type &val) const;
97 void set_value(const Index &idx, value_type val);
98
99 void print_info(std::ostream &out, const std::string &tag = "") const;
100 void print_info_mpi(std::ostream &, const std::string &tag = "") const;
101
102 void save(const std::string &filename) const;
103 void load(const std::string &filename);
104
106
107 template <typename D>
108 Tensor<MatrixType> &multiply_vector(const std::vector<D> &vec, size_t n_axes);
109 template <typename D0, typename D1>
110 Tensor<MatrixType> &multiply_vector(const std::vector<D0> &vec0,
111 size_t n_axes0,
112 const std::vector<D1> &vec1,
113 size_t n_axes1);
114 template <typename D0, typename D1, typename D2>
116 const std::vector<D0> &vec0, size_t n_axes0, const std::vector<D1> &vec1,
117 size_t n_axes1, const std::vector<D2> &vec2, size_t n_axes2);
118 template <typename D0, typename D1, typename D2, typename D3>
120 const std::vector<D0> &vec0, size_t n_axes0, const std::vector<D1> &vec1,
121 size_t n_axes1, const std::vector<D2> &vec2, size_t n_axes2,
122 const std::vector<D3> &vec3, size_t n_axes3);
123
124 Tensor<MatrixType> &set_slice(const Tensor &a, size_t n_axes, size_t i_begin,
125 size_t i_end);
127 const Index &index_end);
128
130 std::vector<value_type> flatten();
131
137
138 template <typename UnaryOperation>
140
141 void prep_global_to_local() const;
142 void prep_local_to_global() const;
143
144 void make_l2g_map() const;
145 void global_index_l2g_map(size_t lindex, size_t gindex[]) const;
146 void global_index_l2g_map_transpose(size_t lindex, const size_t axes_trans[],
147 size_t index_new[]) const;
148
149 void local_position_fast(size_t g_row, size_t g_col, int &comm_rank,
150 size_t &local_idx) const;
151
152 private:
153 MatrixType Mat;
154 Shape Dim;
155
156 size_t upper_rank;
157
159
164 Axes axes_map;
165
166 void init(const Shape &, size_t upper_rank);
167 void init(const Shape &, size_t upper_rank, const Axes &map);
168 void change_configuration(const size_t new_upper_rank,
169 const Axes &new_axes_map);
170 bool local_index(const Index &, size_t &i) const;
171
172 mutable std::vector<size_t> l2g_map_row;
173 mutable std::vector<size_t> l2g_map_col;
174
175 void save_ver_0_2(const char *filename) const;
176 void load_ver_0_2(const char *filename);
177};
178
179/* Operations */
182template <typename MatrixType>
184template <typename MatrixType>
186 size_t urank_new);
187template <typename MatrixType>
189template <typename MatrixType>
191 size_t i_begin, size_t i_end);
192template <typename MatrixType>
194 const Index &index_end);
195template <typename MatrixType>
198
201template <typename MatrixType>
202typename MatrixType::value_type trace(const Tensor<MatrixType> &a);
203template <typename MatrixType>
204typename MatrixType::value_type trace(const Tensor<MatrixType> &a, const Axes &axes_1, const Axes &axes_2);
205template <typename MatrixType>
206typename MatrixType::value_type trace(const Tensor<MatrixType> &a, const Tensor<MatrixType> &b,
207 const Axes &axes_a, const Axes &axes_b);
208template <typename MatrixType>
210 const Axes &axes_2);
211template <typename MatrixType>
213 const Tensor<MatrixType> &b, const Axes &axes_a,
214 const Axes &axes_b);
215template <typename MatrixType>
217 const Tensor<MatrixType> &b);
219
222template <typename MatrixType>
223int svd(const Tensor<MatrixType> &a, std::vector<double> &s);
224template <typename MatrixType>
226 std::vector<double> &s, Tensor<MatrixType> &vt);
227template <typename MatrixType>
228int svd(const Tensor<MatrixType> &a, const Axes &axes_row, const Axes &axes_col,
229 std::vector<double> &s);
230template <typename MatrixType>
231int svd(const Tensor<MatrixType> &a, const Axes &axes_row, const Axes &axes_col,
232 Tensor<MatrixType> &u, std::vector<double> &s, Tensor<MatrixType> &vt);
233
234template <typename MatrixType>
235int psvd(const Tensor<MatrixType> &a, std::vector<double> &s,
236 const size_t target_rank);
237template <typename MatrixType>
239 std::vector<double> &s, Tensor<MatrixType> &vt,
240 const size_t target_rank);
241template <typename MatrixType>
242int psvd(const Tensor<MatrixType> &a, const Axes &axes_row, const Axes &axes_col,
243 std::vector<double> &s, const size_t target_rank);
244template <typename MatrixType>
245int psvd(const Tensor<MatrixType> &a, const Axes &axes_row, const Axes &axes_col,
246 Tensor<MatrixType> &u, std::vector<double> &s, Tensor<MatrixType> &vt,
247 const size_t target_rank);
248
249template <typename MatrixType>
251template <typename MatrixType>
252int qr(const Tensor<MatrixType> &a, const Axes &axes_row, const Axes &axes_col,
254
255template <typename MatrixType>
256int eigh(const Tensor<MatrixType> &a, std::vector<double> &eigval,
258template <typename MatrixType>
259int eigh(const Tensor<MatrixType> &a, std::vector<double> &eigval);
260template <typename MatrixType>
261int eigh(const Tensor<MatrixType> &a, const Axes &axes_row, const Axes &axes_col,
262 std::vector<double> &eigval, Tensor<MatrixType> &eigvec);
263template <typename MatrixType>
264int eigh(const Tensor<MatrixType> &a, const Axes &axes_row, const Axes &axes_col,
265 std::vector<double> &eigval);
266template <typename MatrixType>
267int eigh(const Tensor<MatrixType> &a, const Axes &axes_row_a,
268 const Axes &axes_col_a, const Tensor<MatrixType> &b,
269 const Axes &axes_row_b, const Axes &axes_col_b,
270 std::vector<double> &eigval, Tensor<MatrixType> &eigvec);
271
272
273template <typename MatrixType>
274int eig(const Tensor<MatrixType> &a, std::vector<complex> &eigval,
276template <typename MatrixType>
277int eig(const Tensor<MatrixType> &a, std::vector<complex> &eigval);
278template <typename MatrixType>
279int eig(const Tensor<MatrixType> &a, const Axes &axes_row, const Axes &axes_col,
280 std::vector<complex> &eigval,
282template <typename MatrixType>
283int eig(const Tensor<MatrixType> &a, const Axes &axes_row, const Axes &axes_col,
284 std::vector<complex> &eigval);
286
289template <typename MatrixType>
290int solve(const Tensor<MatrixType> &a,
291 const std::vector<typename MatrixType::value_type> &b,
292 std::vector<typename MatrixType::value_type> &x);
293template <typename MatrixType>
294int solve(const Tensor<MatrixType> &a, const Tensor<MatrixType> &b,
296template <typename MatrixType>
297int solve(const Tensor<MatrixType> &a, const Tensor<MatrixType> &b,
299 const Axes &axes_row_b, const Axes &axes_col_b);
301
304template <typename MatrixType>
306template <typename MatrixType>
308template <typename MatrixType>
310 const Tensor<MatrixType> &rhs);
311template <typename MatrixType>
313 const Tensor<MatrixType> &rhs);
314template <typename MatrixType, typename D>
316template <typename MatrixType, typename D>
318template <typename MatrixType, typename D>
321
324template <typename MatrixType>
327template <typename MatrixType>
330
331template <typename MatrixType>
332double max(const Tensor<MatrixType> &t);
333template <typename MatrixType>
334double min(const Tensor<MatrixType> &t);
335template <typename MatrixType>
336double max_abs(const Tensor<MatrixType> &t);
337template <typename MatrixType>
338double min_abs(const Tensor<MatrixType> &t);
340
343template <typename MatrixType>
344std::ostream &operator<<(std::ostream &out, const Tensor<MatrixType> &t);
346
347} // namespace mptensor
348
350
351#endif // _TENSOR_HPP_
Definition index.hpp:39
Tensor class. The main object of mptensor.
Definition tensor.hpp:54
Tensor< MatrixType > & transpose(const Axes &axes)
Transposition of tensor with lazy evaluation.
Definition tensor_impl.hpp:681
typename MatrixType::value_type value_type
double or complex
Definition tensor.hpp:56
size_t rank() const
Rank of tensor.
Definition tensor_impl.hpp:165
const comm_type & get_comm() const
Communicator.
Definition tensor_impl.hpp:217
const Axes & get_axes_map() const
Map of axes for lazy evaluation of transpose.
Definition tensor_impl.hpp:198
size_t local_size() const
Size of local storage.
Definition tensor_impl.hpp:180
std::vector< value_type > flatten()
Return a copy of the flattened vector.
Definition tensor_impl.hpp:985
Tensor< MatrixType > & map(UnaryOperation op)
Apply a unary operation to each element.
Definition tensor_impl.hpp:1040
void make_l2g_map() const
Preprocess for fast conversion from local index to global one.
Definition tensor_impl.hpp:265
const value_type & operator[](size_t local_idx) const
Const array subscript operator.
Definition tensor_impl.hpp:238
void global_index_fast(size_t i, Index &idx) const
Convert local index to global index fast.
Definition tensor_impl.hpp:539
Tensor< MatrixType > & operator=(value_type rhs)
Initialize all elements by rhs.
Definition tensor_impl.hpp:1027
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:365
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:598
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:330
Tensor< lapack::Matrix< value_type > > gather()
Gather all elements into a non-distributed tensor.
Definition tensor_impl.hpp:969
bool get_value(const Index &idx, value_type &val) const
Get an element.
Definition tensor_impl.hpp:566
Tensor< typename MatrixType::template rebind< D > > rebind
Tensor with scalar type replaced by D.
Definition tensor.hpp:61
Tensor< MatrixType > & operator-=(const Tensor &rhs)
Subtraction assignment.
Definition tensor_impl.hpp:1004
void prep_local_to_global() const
Preprocess for fast conversion from local index to global one.
Definition tensor_impl.hpp:259
void print_info_mpi(std::ostream &, const std::string &tag="") const
Output information of tensor.
Definition tensor_impl.hpp:456
void print_info(std::ostream &out, const std::string &tag="") const
Output information of tensor.
Definition tensor_impl.hpp:438
size_t ndim() const
Rank of tensor.
Definition tensor_impl.hpp:174
Tensor< MatrixType > & multiply_vector(const std::vector< D > &vec, size_t n_axes)
Element-wise vector multiplication.
Definition tensor_impl.hpp:714
void set_value(const Index &idx, value_type val)
Set an element.
Definition tensor_impl.hpp:584
Tensor< MatrixType > & operator/=(value_type rhs)
Scalar-division assignment.
Definition tensor_impl.hpp:1020
int get_comm_rank() const
Rank of process.
Definition tensor_impl.hpp:229
void local_position_fast(size_t g_row, size_t g_col, int &comm_rank, size_t &local_idx) const
Definition tensor_impl.hpp:390
Index global_index(size_t i) const
Convert local index to global index.
Definition tensor_impl.hpp:509
void prep_global_to_local() const
Preprocess for fast conversion from global index to local one.
Definition tensor_impl.hpp:253
Tensor< MatrixType > & operator*=(value_type rhs)
Scalar-multiplication assignment.
Definition tensor_impl.hpp:1013
Tensor< MatrixType > & set_slice(const Tensor &a, size_t n_axes, size_t i_begin, size_t i_end)
Inverse of slice().
Definition tensor_impl.hpp:860
const Shape & shape() const
Shape of tensor.
Definition tensor_impl.hpp:159
MatrixType matrix_type
type of Matrix class
Definition tensor.hpp:57
void load(const std::string &filename)
Load a tensor from files.
Definition load.hpp:53
size_t get_upper_rank() const
Upper rank for matrix representation.
Definition tensor_impl.hpp:186
Tensor< MatrixType > & operator+=(const Tensor &rhs)
Addition assignment.
Definition tensor_impl.hpp:995
const MatrixType & get_matrix() const
distributed Matrix
Definition tensor_impl.hpp:204
int get_comm_size() const
Size of communicator.
Definition tensor_impl.hpp:223
typename MatrixType::comm_type comm_type
type of communicator. MPI_Comm or int.
Definition tensor.hpp:58
std::string filename(const std::string &prefix, int proc_size)
Definition common.hpp:32
Define the type of a complex number.
Tensor< MatrixType > operator/(Tensor< MatrixType > lhs, D rhs)
Scalar division.
Definition tensor_impl.hpp:2598
Tensor< MatrixType > operator-(Tensor< MatrixType > rhs)
Unary minus.
Definition tensor_impl.hpp:2576
Tensor< MatrixType > operator*(Tensor< MatrixType > lhs, D rhs)
Tensor-scalar multiplication.
Definition tensor_impl.hpp:2593
std::complex< double > complex
Definition complex.hpp:38
int eig(const Tensor< MatrixType > &a, std::vector< complex > &eigval, typename Tensor< MatrixType >::template rebind< complex > &eigvec)
Compute the eigenvalues and eigenvectors of a general matrix (rank-2 tensor)
Definition tensor_impl.hpp:2309
int psvd(const Tensor< MatrixType > &a, std::vector< double > &s, const size_t target_rank)
Partial SVD for rank-2 tensor (matrix)
Definition tensor_impl.hpp:1880
int eigh(const Tensor< MatrixType > &a, std::vector< double > &eigval, Tensor< MatrixType > &eigvec)
Definition tensor_impl.hpp:2085
int qr(const Tensor< MatrixType > &a, Tensor< MatrixType > &q, Tensor< MatrixType > &r)
QR decomposition of matrix (rank-2 tensor)
Definition tensor_impl.hpp:1988
int svd(const Tensor< MatrixType > &a, std::vector< double > &s)
Singular value decomposition for rank-2 tensor (matrix)
Definition tensor_impl.hpp:1736
MatrixType::value_type trace(const Tensor< MatrixType > &a)
Trace of matrix (rank-2 tensor)
Definition tensor_impl.hpp:1423
Tensor< MatrixType > contract(const Tensor< MatrixType > &a, const Axes &axes_1, const Axes &axes_2)
Partial trace of tensor.
Definition tensor_impl.hpp:1533
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:1662
Tensor< MatrixType > kron(const Tensor< MatrixType > &a, const Tensor< MatrixType > &b)
Compute the Kronecker product.
Definition tensor_impl.hpp:1626
int solve(const Tensor< MatrixType > &a, const std::vector< typename MatrixType::value_type > &b, std::vector< typename MatrixType::value_type > &x)
Solve linear equation .
Definition tensor_impl.hpp:2461
Tensor< MatrixType > conj(Tensor< MatrixType > t)
Take conjugate of each element.
double min_abs(const Tensor< MatrixType > &t)
Return minimum of the absolute value of an element.
Definition tensor_impl.hpp:2624
Tensor< MatrixType > sqrt(Tensor< MatrixType > t)
Take square-root of each element.
double min(const Tensor< MatrixType > &t)
Return the minimum element. For complex, same as min_abs().
Definition tensor_impl.hpp:2614
double max_abs(const Tensor< MatrixType > &t)
Return maximum of the absolute value of an element.
Definition tensor_impl.hpp:2619
double max(const Tensor< MatrixType > &t)
Return the maximum element. For complex, same as max_abs().
Definition tensor_impl.hpp:2609
Tensor< MatrixType > extend(const Tensor< MatrixType > &a, const Shape &shape_new)
Extend the size of a tensor.
Definition tensor_impl.hpp:1377
Tensor< MatrixType > transpose(Tensor< MatrixType > a, const Axes &axes)
Transposition of tensor with lazy evaluation.
Definition tensor_impl.hpp:1055
Tensor< MatrixType > reshape(const Tensor< MatrixType > &a, const Shape &shape_new)
Change the shape of tensor.
Definition tensor_impl.hpp:1163
Tensor< MatrixType > slice(const Tensor< MatrixType > &a, size_t n_axes, size_t i_begin, size_t i_end)
Slice a tensor.
Definition tensor_impl.hpp:1229
Tensor()
Default constructor of tensor.
Definition tensor_impl.hpp:71
header file of Index class
List of header files for matrix classes.
Definition complex.hpp:34
std::ostream & operator<<(std::ostream &os, const Index &idx)
Index operator+(const Index &, const Index &)
Implementation of tensor class.