mptensor v0.6.0
Parallel Library for Tensor Network Methods
Loading...
Searching...
No Matches
tensor_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_IMPL_HPP_
30#define _TENSOR_IMPL_HPP_
31
32#include <algorithm>
33#include <cassert>
34#include <cstdio>
35#include <cstdlib>
36#include <cstring>
37#include <fstream>
38#include <iostream>
39#include <type_traits>
40#include <vector>
41
42#include "mptensor/complex.hpp"
43#include "mptensor/index.hpp"
45#include "mptensor/tensor.hpp"
46
47namespace mptensor {
48
49/* Utilities */
50bool is_no_transpose(const internal::UAxes& axes, const internal::UAxes& axes_map, size_t rank);
51namespace debug {
54bool check_transpose_axes(const internal::UAxes& axes, size_t rank);
55bool check_svd_axes(const internal::UAxes& axes_row, const internal::UAxes& axes_col, size_t rank);
56bool check_trace_axes(const internal::UAxes& axes_1, const internal::UAxes& axes_2, size_t rank);
59bool check_contract_axes(const internal::UAxes& axes_1, const internal::UAxes& axes_2, size_t rank);
60bool check_square(const internal::UShape& shape, size_t urank);
61} // namespace debug
62
63/* ---------- constructors ---------- */
64
66
70template <typename MatrixType>
71Tensor<MatrixType>::Tensor() : Mat(), upper_rank(0), axes_map(){};
72
74
79template <typename MatrixType>
80Tensor<MatrixType>::Tensor(const Shape &shape) : Mat() {
81 init(to_internal_shape(shape), shape.size() / 2);
82};
83
85
88template <typename MatrixType>
90 : Mat(comm), upper_rank(0), axes_map(){};
91
98template <typename MatrixType>
99Tensor<MatrixType>::Tensor(const comm_type &comm, const Shape &shape)
100 : Mat(comm) {
101 init(to_internal_shape(shape), shape.size() / 2);
105
110template <typename MatrixType>
112 const size_t upper_rank)
113 : Mat(comm) {
114 init(to_internal_shape(shape), upper_rank);
116
118template <typename MatrixType>
120 const size_t upper_rank, internal::normalized_t)
121 : Mat(comm) {
122 init(shape, upper_rank);
125
132template <typename MatrixType>
134 const Tensor<lapack::Matrix<value_type>> &t)
135 : Mat(comm) {
136 init(t.internal_shape(), t.get_upper_rank());
137 const size_t n = Mat.local_size();
138 size_t idx;
139 int dummy;
141 g.resize(Dim.size());
142 for (size_t i = 0; i < n; ++i) {
143 global_index_fast(i, g);
144 t.local_position(g, dummy, idx);
145 Mat[i] = t[idx];
146 }
147};
150
155template <typename MatrixType>
156Tensor<MatrixType>::Tensor(const comm_type &comm, const std::vector<value_type> &v)
157 : Mat(comm) {
158 init(internal::UShape(v.size()), 0);
159 const size_t n = Mat.local_size();
161 idx.resize(1);
162 for (size_t i = 0; i < n; ++i) {
163 global_index_fast(i, idx);
164 Mat[i] = v[idx[0]];
168/* ---------- inline member functions ---------- */
171template <typename MatrixType>
173 return to_public(Dim);
177template <typename MatrixType>
179 return Dim;
180};
181
183template <typename MatrixType>
184inline size_t Tensor<MatrixType>::rank() const {
185 return Dim.size();
186};
187
189
192template <typename MatrixType>
193inline size_t Tensor<MatrixType>::ndim() const {
194 return Dim.size();
195};
196
198template <typename MatrixType>
199inline size_t Tensor<MatrixType>::local_size() const {
200 return Mat.local_size();
201};
202
204template <typename MatrixType>
206 return upper_rank;
207};
208
210
216template <typename MatrixType>
218 return axes_map;
219};
220
222template <typename MatrixType>
224 return Mat;
225};
226
228template <typename MatrixType>
230 return Mat;
231};
232
234template <typename MatrixType>
235inline const typename Tensor<MatrixType>::comm_type &
237 return Mat.get_comm();
238};
239
241template <typename MatrixType>
243 return Mat.get_comm_size();
244};
245
247template <typename MatrixType>
249 return Mat.get_comm_rank();
250};
251
253
256template <typename MatrixType>
257inline const typename MatrixType::value_type &Tensor<MatrixType>::operator[](size_t local_idx) const {
258 return Mat[local_idx];
259}
260
262
265template <typename MatrixType>
266inline typename MatrixType::value_type &Tensor<MatrixType>::operator[](size_t local_idx) {
267 return Mat[local_idx];
268}
269
271template <typename MatrixType>
275
277template <typename MatrixType>
281
283template <typename MatrixType>
285 const size_t rank = Dim.size();
286 const size_t rank0 = upper_rank;
287 const size_t rank1 = rank - rank0; // lower_rank
288 const size_t l_row = Mat.local_row_size();
289 const size_t l_col = Mat.local_col_size();
290 // When rank0 (== upper_rank) or rank1 (== lower_rank) is zero, the
291 // corresponding range is empty; taking the base address of an empty range
292 // (&axes_map[rank0] one past the end, or &l2g_map_row[0] on an empty vector)
293 // is undefined behavior and aborts under _GLIBCXX_ASSERTIONS. Guard with a
294 // null pointer that is never dereferenced (the matching loops are skipped).
295 const size_t *axes_map0 = rank0 > 0 ? &(axes_map[0]) : nullptr;
296 const size_t *axes_map1 = rank1 > 0 ? &(axes_map[rank0]) : nullptr;
297
298 l2g_map_row.resize(l_row * rank0);
299 l2g_map_col.resize(l_col * rank1);
300
301#pragma omp parallel default(shared)
302 {
303 // Type int instead of size_t is used because of std::div()
304 std::vector<int> dim0(rank0);
305 std::vector<int> dim1(rank1);
306 for (size_t i = 0; i < rank0; ++i) dim0[i] = int(Dim[axes_map0[i]]);
307 for (size_t i = 0; i < rank1; ++i) dim1[i] = int(Dim[axes_map1[i]]);
308
309 int g_row, g_col; // global matrix row- and column-index
310 std::div_t divresult;
311 size_t *map;
312 if (rank0 > 0) {
313#pragma omp for
314 for (size_t k = 0; k < l_row; ++k) {
315 map = &(l2g_map_row[k * rank0]);
316 g_row = int(Mat.global_row_index(k));
317 for (size_t i = 0; i < rank0; ++i) {
318 divresult = std::div(g_row, dim0[i]);
319 map[i] = divresult.rem;
320 g_row = divresult.quot;
321 }
322 }
323 }
324 if (rank1 > 0) {
325#pragma omp for
326 for (size_t k = 0; k < l_col; ++k) {
327 map = &(l2g_map_col[k * rank1]);
328 g_col = int(Mat.global_col_index(k));
329 for (size_t i = 0; i < rank1; ++i) {
330 divresult = std::div(g_col, dim1[i]);
331 map[i] = divresult.rem;
332 g_col = divresult.quot;
333 }
334 }
335 }
336 }
337
338 return;
339};
340
342
348template <typename MatrixType>
350 size_t gindex[]) const {
351 const size_t rank = Dim.size();
352 const size_t rank0 = upper_rank;
353 const size_t rank1 = rank - rank0; // lower_rank
354 const size_t lindex_row = Mat.local_row_index(lindex);
355 const size_t lindex_col = Mat.local_col_index(lindex);
356 // Guard against empty upper/lower ranges (see make_l2g_map): a null base
357 // pointer is safe because the matching loop below is then empty.
358 const size_t *map_row = rank0 > 0 ? &(l2g_map_row[lindex_row * rank0]) : nullptr;
359 const size_t *map_col = rank1 > 0 ? &(l2g_map_col[lindex_col * rank1]) : nullptr;
360 const size_t *axes_map0 = rank0 > 0 ? &(axes_map[0]) : nullptr;
361 const size_t *axes_map1 = rank1 > 0 ? &(axes_map[rank0]) : nullptr;
362 for (size_t i = 0; i < rank0; ++i) {
364 }
365 for (size_t i = 0; i < rank1; ++i) {
367 }
368 return;
369};
370
372
383template <typename MatrixType>
385 size_t lindex, const size_t axes_trans[], size_t index_new[]) const {
386 const size_t rank = Dim.size();
387 const size_t rank0 = upper_rank;
388 const size_t rank1 = rank - rank0; // lower_rank
389 const size_t lindex_row = Mat.local_row_index(lindex);
390 const size_t lindex_col = Mat.local_col_index(lindex);
391 // Guard against empty upper/lower ranges (see make_l2g_map): a null base
392 // pointer is safe because the matching loop below is then empty.
393 const size_t *map_row = rank0 > 0 ? &(l2g_map_row[lindex_row * rank0]) : nullptr;
394 const size_t *map_col = rank1 > 0 ? &(l2g_map_col[lindex_col * rank1]) : nullptr;
395 // const size_t* axes_map0 = &(axes_map[0]);
396 // const size_t* axes_map1 = &(axes_map[rank0]);
397 for (size_t i = 0; i < rank0; ++i) {
399 // index_new[axes_inv[axes_map0[i]]] = map_row[i];
400 }
401 for (size_t i = 0; i < rank1; ++i) {
403 // index_new[axes_inv[axes_map1[i]]] = map_col[i];
404 }
405 return;
406};
407
408template <typename MatrixType>
410 int &comm_rank,
411 size_t &local_idx) const {
413}
414
415/* ---------- member functions ---------- */
416
418
422template <typename MatrixType>
423inline void Tensor<MatrixType>::init(const internal::UShape &shape, size_t urank) {
424 init(shape, urank, internal::identity_axes(shape.size()));
425}
426
428
433template <typename MatrixType>
434void Tensor<MatrixType>::init(const internal::UShape &shape, size_t urank,
435 const internal::UAxes &map) {
436 Dim = shape;
437 axes_map = map;
438 const size_t rank = Dim.size();
439 assert(urank <= rank);
440 upper_rank = urank;
441
442 size_t n_row = 1;
443 size_t n_col = 1;
444 for (size_t i = 0; i < upper_rank; ++i) n_row *= shape[map[i]];
445 for (size_t i = upper_rank; i < rank; ++i) n_col *= shape[map[i]];
446 Mat.init(n_row, n_col);
447}
448
450
456template <typename MatrixType>
458 const std::string &tag) const {
459 if (tag.size() > 0) {
460 out << tag << ":\t";
461 }
462 out << "Tensor: shape= " << Dim << " upper_rank= " << upper_rank
463 << " axes_map= " << axes_map << "\t";
464 Mat.print_info(out);
465};
466
468
474template <typename MatrixType>
476 const std::string &tag) const {
477 const int mpisize = get_comm_size();
478 const int mpirank = get_comm_rank();
479 const typename Tensor<MatrixType>::comm_type &comm = get_comm();
480 Mat.barrier();
481 for (int i = 0; i < mpisize; ++i) {
482 if (i == mpirank) {
483 if (tag.size() > 0) {
484 out << tag << ":\t";
485 }
486 out << "mpirank: " << mpirank << "\t";
487 print_info(out, "");
488 }
489 Mat.barrier();
490 }
491}
492
494
502template <typename MatrixType>
504 const size_t rank = gindex.size();
505 assert(rank == Dim.size());
506 assert(rank > 0);
507 size_t g_row(0), g_col(0);
508 size_t d_row(1), d_col(1);
509 for (size_t i = 0; i < upper_rank; ++i) {
510 size_t j = axes_map[i];
511 g_row += gindex[j] * d_row;
512 d_row *= Dim[j];
513 }
514 for (size_t i = upper_rank; i < rank; ++i) {
515 size_t j = axes_map[i];
516 g_col += gindex[j] * d_col;
517 d_col *= Dim[j];
518 }
519 return Mat.local_index(g_row, g_col, lindex);
520};
521
523
527template <typename MatrixType>
530 gindex.resize(Dim.size());
531 global_index_fast(lindex, gindex);
532 return to_public(gindex);
533};
534
536
542template <typename MatrixType>
544 const size_t rank = Dim.size();
545 size_t g_row, g_col;
546 Mat.global_index(lindex, g_row, g_col);
547 std::div_t divresult;
548 for (size_t i = 0; i < upper_rank; ++i) {
549 size_t j = axes_map[i];
550 divresult = std::div(int(g_row), int(Dim[j]));
551 gindex[j] = divresult.rem;
552 g_row = divresult.quot;
553 }
554 for (size_t i = upper_rank; i < rank; ++i) {
555 size_t j = axes_map[i];
556 divresult = std::div(int(g_col), int(Dim[j]));
557 gindex[j] = divresult.rem;
558 g_col = divresult.quot;
559 }
560 return;
561};
562
564
569template <typename MatrixType>
571 size_t li;
572 if (local_index(normalize_index(idx, Dim), li)) {
573 val = Mat[li];
574 return true;
575 } else {
576 return false;
577 }
578}
579
581
587template <typename MatrixType>
589 size_t li;
590 if (local_index(normalize_index(idx, Dim), li)) {
591 Mat[li] = val;
592 }
593}
594
596
601template <typename MatrixType>
603 size_t &local_idx) const {
604 const size_t rank = Dim.size();
605 size_t g_row(0), g_col(0);
606 size_t d_row(1), d_col(1);
607 for (size_t i = 0; i < upper_rank; ++i) {
608 size_t j = axes_map[i];
609 g_row += index[j] * d_row;
610 d_row *= Dim[j];
611 }
612 for (size_t i = upper_rank; i < rank; ++i) {
613 size_t j = axes_map[i];
614 g_col += index[j] * d_col;
615 d_col *= Dim[j];
616 }
617 Mat.local_position(g_row, g_col, comm_rank, local_idx);
618 return;
619}
620
622
628template <typename MatrixType>
631 if ((upper_rank == new_upper_rank) && (axes_map == new_axes_map)) return;
632
634
635 const size_t rank = this->rank();
638 dim.resize(rank);
639 axes.resize(rank);
640 for (size_t i = 0; i < rank; ++i) {
641 dim[i] = Dim[new_axes_map[i]];
642 axes[new_axes_map[i]] = i;
643 }
644 init(dim, new_upper_rank);
645 transpose_internal(axes);
646
647 /* create lists of local position and destination rank */
648 const size_t local_size = T_old.local_size();
649 std::vector<int> dest_mpirank(local_size);
650 std::vector<size_t> local_position(local_size);
651
652 T_old.prep_local_to_global();
653 this->prep_global_to_local();
654
655#pragma omp parallel default(shared)
656 {
657 internal::UIndex index;
658 index.resize(rank);
659
660#pragma omp for
661 for (size_t i = 0; i < local_size; ++i) {
662 T_old.global_index_fast(i, index);
663
664 int dest;
665 size_t pos;
666 this->local_position(index, dest, pos);
667
668 local_position[i] = pos;
670 }
671 }
672
673 /* exchange data */
674 replace_matrix_data(T_old.get_matrix(), dest_mpirank, local_position, Mat);
675
676 return;
677}
678
680
684template <typename MatrixType>
686 return transpose_internal(normalize_axes(axes, Dim.size()));
687}
688
690template <typename MatrixType>
692 const internal::UAxes &axes) {
693 const size_t rank = Dim.size();
694 assert(debug::check_transpose_axes(axes, rank));
695
697 internal::UAxes map_now = axes_map;
699 axes_inv.resize(rank);
700 for (size_t i = 0; i < rank; ++i) {
701 axes_inv[axes[i]] = i;
702 }
703 for (size_t i = 0; i < rank; ++i) {
704 Dim[i] = dim_now[axes[i]];
705 axes_map[i] = axes_inv[map_now[i]];
706 }
707 return (*this);
708}
710
712
722template <typename MatrixType>
723template <typename D>
725 std::ptrdiff_t n_axes) {
726 const size_t ax = normalize_axis(n_axes, rank());
727 assert(Dim[ax] <= vec.size());
728 const size_t local_size = this->local_size();
729 prep_local_to_global();
730#pragma omp parallel default(shared)
731 {
733 idx.resize(rank());
734#pragma omp for
735 for (size_t i = 0; i < local_size; ++i) {
736 global_index_fast(i, idx);
737 Mat[i] *= vec[idx[ax]];
738 }
739 }
740 return (*this);
741};
742
744
756template <typename MatrixType>
757template <typename D0, typename D1>
759 const std::vector<D0> &vec0, std::ptrdiff_t n_axes0, const std::vector<D1> &vec1,
760 std::ptrdiff_t n_axes1) {
761 const size_t ax0 = normalize_axis(n_axes0, rank());
762 const size_t ax1 = normalize_axis(n_axes1, rank());
763 assert(Dim[ax0] <= vec0.size());
764 assert(Dim[ax1] <= vec1.size());
765 const size_t local_size = this->local_size();
766 prep_local_to_global();
767#pragma omp parallel default(shared)
768 {
770 idx.resize(rank());
771#pragma omp for
772 for (size_t i = 0; i < local_size; ++i) {
773 global_index_fast(i, idx);
774 Mat[i] *= vec0[idx[ax0]] * vec1[idx[ax1]];
775 }
776 }
777 return (*this);
778};
779
781
795template <typename MatrixType>
796template <typename D0, typename D1, typename D2>
798 const std::vector<D0> &vec0, std::ptrdiff_t n_axes0, const std::vector<D1> &vec1,
799 std::ptrdiff_t n_axes1, const std::vector<D2> &vec2, std::ptrdiff_t n_axes2) {
800 const size_t ax0 = normalize_axis(n_axes0, rank());
801 const size_t ax1 = normalize_axis(n_axes1, rank());
802 const size_t ax2 = normalize_axis(n_axes2, rank());
803 assert(Dim[ax0] <= vec0.size());
804 assert(Dim[ax1] <= vec1.size());
805 assert(Dim[ax2] <= vec2.size());
806 const size_t local_size = this->local_size();
807 prep_local_to_global();
808#pragma omp parallel default(shared)
809 {
811 idx.resize(rank());
812#pragma omp for
813 for (size_t i = 0; i < local_size; ++i) {
814 global_index_fast(i, idx);
815 Mat[i] *= vec0[idx[ax0]] * vec1[idx[ax1]] * vec2[idx[ax2]];
816 }
817 }
818 return (*this);
819};
820
822
838template <typename MatrixType>
839template <typename D0, typename D1, typename D2, typename D3>
841 const std::vector<D0> &vec0, std::ptrdiff_t n_axes0, const std::vector<D1> &vec1,
842 std::ptrdiff_t n_axes1, const std::vector<D2> &vec2, std::ptrdiff_t n_axes2,
843 const std::vector<D3> &vec3, std::ptrdiff_t n_axes3) {
844 const size_t ax0 = normalize_axis(n_axes0, rank());
845 const size_t ax1 = normalize_axis(n_axes1, rank());
846 const size_t ax2 = normalize_axis(n_axes2, rank());
847 const size_t ax3 = normalize_axis(n_axes3, rank());
848 assert(Dim[ax0] <= vec0.size());
849 assert(Dim[ax1] <= vec1.size());
850 assert(Dim[ax2] <= vec2.size());
851 assert(Dim[ax3] <= vec3.size());
852 const size_t local_size = this->local_size();
853 prep_local_to_global();
854#pragma omp parallel default(shared)
855 {
857 idx.resize(rank());
858#pragma omp for
859 for (size_t i = 0; i < local_size; ++i) {
860 global_index_fast(i, idx);
861 Mat[i] *= vec0[idx[ax0]] * vec1[idx[ax1]] * vec2[idx[ax2]] *
862 vec3[idx[ax3]];
863 }
864 }
865 return (*this);
866};
867
869
879template <typename MatrixType>
881 const std::ptrdiff_t n_axes,
882 const std::ptrdiff_t i_begin,
883 const std::ptrdiff_t i_end) {
884 const size_t ax = normalize_axis(n_axes, rank());
885 const std::pair<size_t, size_t> be =
886 internal::normalize_slice_range(i_begin, i_end, Dim[ax], ax);
887 const size_t begin = be.first;
888 assert(rank() == a.rank());
889 assert(be.second - begin == a.internal_shape()[ax]);
890
891 /* create lists of local position and destination rank */
892 const size_t local_size = a.local_size();
893 std::vector<int> dest_mpirank(local_size);
894 std::vector<size_t> local_position(local_size);
895
897 prep_global_to_local();
898
899#pragma omp parallel default(shared)
900 {
901 internal::UIndex index;
902 index.resize(rank());
903#pragma omp for
904 for (size_t i = 0; i < local_size; ++i) {
905 a.global_index_fast(i, index);
906 index[ax] += begin;
907
908 int dest;
909 size_t pos;
910 this->local_position(index, dest, pos);
911
912 local_position[i] = pos;
914 }
915 }
916
917 /* exchange data */
918 replace_matrix_data(a.get_matrix(), dest_mpirank, local_position,
919 get_matrix());
920
921 return (*this);
922}
923
925
935template <typename MatrixType>
937 const Index &index_begin,
938 const Index &index_end) {
939 const size_t nr = rank();
940 assert(nr == a.rank());
942 internal::normalize_slice_ranges(index_begin, index_end, Dim, begin, end);
943 for (size_t r = 0; r < nr; ++r) {
944 assert(end[r] - begin[r] == a.internal_shape()[r]);
945 }
946
947 /* create lists of local position and destination rank */
948 const size_t local_size = a.local_size();
949 std::vector<int> dest_mpirank(local_size);
950 std::vector<size_t> local_position(local_size);
951
953 prep_global_to_local();
954
955#pragma omp parallel default(shared)
956 {
957 internal::UIndex index;
958 index.resize(nr);
959#pragma omp for
960 for (size_t i = 0; i < local_size; ++i) {
961 a.global_index_fast(i, index);
962 for (size_t r = 0; r < nr; ++r) {
963 index[r] += begin[r];
964 }
965
966 int dest;
967 size_t pos;
968 this->local_position(index, dest, pos);
969
970 local_position[i] = pos;
972 }
973 }
974
975 /* exchange data */
976 replace_matrix_data(a.get_matrix(), dest_mpirank, local_position,
977 get_matrix());
978
979 return (*this);
980}
981
982
984
988template <typename MatrixType>
990 const size_t n = rank();
991 if (!(axes_map == internal::identity_axes(n))) {
992 change_configuration(n / 2, internal::identity_axes(n));
993 }
994 // A LAPACK tensor has only a dummy communicator; MPI_Comm may not convert to it.
996 LocalTensor T(typename LocalTensor::comm_type{}, get_matrix().flatten());
997 return internal::reshape_impl(T, Dim);
998}
999
1000
1002
1006template <typename MatrixType>
1007std::vector<typename MatrixType::value_type> Tensor<MatrixType>::flatten() {
1008 const size_t n = rank();
1009 if (!(axes_map == internal::identity_axes(n))) {
1010 change_configuration(n / 2, internal::identity_axes(n));
1011 }
1012 return get_matrix().flatten();
1013}
1014
1016template <typename MatrixType>
1018 assert(Dim == rhs.internal_shape());
1019 change_configuration(rhs.get_upper_rank(), rhs.get_axes_map());
1020 Mat += rhs.get_matrix();
1021 return (*this);
1022}
1023
1025template <typename MatrixType>
1027 assert(Dim == rhs.internal_shape());
1028 change_configuration(rhs.get_upper_rank(), rhs.get_axes_map());
1029 Mat -= rhs.get_matrix();
1030 return (*this);
1031}
1032
1034template <typename MatrixType>
1036 Mat *= rhs;
1037 return (*this);
1038}
1039
1041template <typename MatrixType>
1043 Mat /= rhs;
1044 return (*this);
1045}
1046
1048template <typename MatrixType>
1050 const size_t n = Mat.local_size();
1051 for (size_t i = 0; i < n; ++i) Mat[i] = rhs;
1052 return (*this);
1053}
1054
1056
1060template <typename MatrixType>
1061template <typename UnaryOperation>
1063 Mat.map(op);
1064 return (*this);
1065}
1066
1067/* ---------- non-member functions (Opeations) ---------- */
1068
1070
1076template <typename MatrixType>
1078 return T.transpose(axes);
1079}
1080
1082
1091template <typename MatrixType>
1093 size_t urank_new) {
1094 return internal::transpose_impl(T, normalize_axes(axes, T.rank()), urank_new);
1095}
1096
1098
1109template <typename MatrixType>
1111 return internal::reshape_impl(T, to_internal_shape(shape_new));
1112}
1113
1114namespace internal {
1115template <typename MatrixType>
1117 size_t urank_new) {
1118 const size_t rank = T.rank();
1119 assert(debug::check_transpose_axes(axes, rank));
1120 assert(urank_new <= rank);
1121
1122 if (urank_new == T.get_upper_rank()) {
1123 if (is_no_transpose(axes, T.get_axes_map(), rank)) return T;
1124 }
1125
1126 /* new index dimension */
1127 UShape dim_old = T.internal_shape();
1129 dim_new.resize(rank);
1130 for (size_t r = 0; r < rank; ++r) {
1131 dim_new[r] = dim_old[axes[r]];
1132 }
1133
1134 /* new tensor */
1136
1137 /* create lists of local position and destination rank */
1138 const size_t local_size = T.local_size();
1139 std::vector<int> dest_mpirank(local_size);
1140 std::vector<size_t> local_position(local_size);
1141
1142 T.make_l2g_map();
1143 T_new.prep_global_to_local();
1144
1145#pragma omp parallel default(shared)
1146 {
1147 // Assuming axes_map of T_new is [0,1,2,...]
1148 std::vector<size_t> d_offset(rank);
1149 size_t d_row(1), d_col(1);
1150 for (size_t r = 0; r < urank_new; ++r) {
1151 d_offset[r] = d_row;
1152 d_row *= dim_new[r];
1153 }
1154 for (size_t r = urank_new; r < rank; ++r) {
1155 d_offset[r] = d_col;
1156 d_col *= dim_new[r];
1157 }
1158
1159 UAxes axes_map = T.get_axes_map();
1160 std::vector<size_t> axes_inv(rank);
1161 std::vector<size_t> axes_trans(rank);
1162 for (size_t r = 0; r < rank; ++r) {
1163 axes_inv[axes[r]] = r;
1164 }
1165 for (size_t r = 0; r < rank; ++r) {
1166 axes_trans[r] = axes_inv[axes_map[r]];
1167 }
1168
1169 std::vector<size_t> index_new(rank);
1170
1171#pragma omp for
1172 for (size_t i = 0; i < local_size; ++i) {
1173 T.global_index_l2g_map_transpose(i, axes_trans.data(), index_new.data());
1174
1175 size_t g_row(0),
1176 g_col(0); // indices of global matrix for transposed tensor
1177 for (size_t r = 0; r < urank_new; ++r) {
1178 g_row += index_new[r] * d_offset[r];
1179 }
1180 for (size_t r = urank_new; r < rank; ++r) {
1181 g_col += index_new[r] * d_offset[r];
1182 }
1183
1184 T_new.local_position_fast(g_row, g_col, dest_mpirank[i],
1185 local_position[i]);
1186 }
1187 }
1188
1189 /* exchange data */
1190 replace_matrix_data(T.get_matrix(), dest_mpirank, local_position,
1191 T_new.get_matrix());
1192
1193 return T_new;
1194};
1195
1196template <typename MatrixType>
1198 assert(debug::check_total_size(shape_new, T.internal_shape()));
1199
1200 const UShape &shape = T.internal_shape();
1201 const size_t rank = shape.size();
1202 const size_t rank_new = shape_new.size();
1203
1204 /* initialize new tensor */
1205 Tensor<MatrixType> T_new(T.get_comm(), shape_new, shape_new.size() / 2,
1206 normalized);
1207
1208 /* create lists of local position and destination rank */
1209 const size_t local_size = T.local_size();
1210 std::vector<int> dest_mpirank(local_size);
1211 std::vector<size_t> local_position(local_size);
1212
1213 T.prep_local_to_global();
1214 T_new.prep_global_to_local();
1215
1216#pragma omp parallel default(shared)
1217 {
1218 UIndex index, index_new;
1219 index.resize(rank);
1220 index_new.resize(rank_new);
1221#pragma omp for
1222 for (size_t i = 0; i < local_size; ++i) {
1223 T.global_index_fast(i, index);
1224 size_t global_position(0);
1225 size_t dim(1);
1226 for (size_t r = 0; r < rank; ++r) {
1227 global_position += index[r] * dim;
1228 dim *= shape[r];
1229 }
1230
1231 for (size_t r = 0; r < rank_new; ++r) {
1234 }
1235
1236 int dest;
1237 size_t pos;
1238 T_new.local_position(index_new, dest, pos);
1239
1240 local_position[i] = pos;
1241 dest_mpirank[i] = dest;
1242 }
1243 }
1244
1245 /* exchange data */
1246 replace_matrix_data(T.get_matrix(), dest_mpirank, local_position,
1247 T_new.get_matrix());
1248
1249 return T_new;
1250};
1251} // namespace internal
1252
1254
1264template <typename MatrixType>
1266 std::ptrdiff_t i_begin, std::ptrdiff_t i_end) {
1267 const int mpisize = T.get_comm_size();
1268 const internal::UShape &shape = T.internal_shape();
1269 const size_t ax = normalize_axis(n_axes, T.rank());
1270 const std::pair<size_t, size_t> be =
1271 internal::normalize_slice_range(i_begin, i_end, shape[ax], ax);
1272 const size_t begin = be.first;
1273 const size_t end = be.second;
1274
1276 shape_new[ax] = end - begin;
1277
1278 /* initialize new tensor */
1279 Tensor<MatrixType> T_new(T.get_comm(), shape_new, shape_new.size() / 2,
1280 internal::normalized);
1281
1282 /* create lists of local position and destination rank */
1283 const size_t local_size = T.local_size();
1284 std::vector<int> dest_mpirank(local_size);
1285 std::vector<size_t> local_position(local_size);
1286
1287 T.prep_local_to_global();
1288 T_new.prep_global_to_local();
1289
1290#pragma omp parallel default(shared)
1291 {
1292 internal::UIndex index;
1293 index.resize(T.rank());
1294#pragma omp for
1295 for (size_t i = 0; i < local_size; ++i) {
1296 T.global_index_fast(i, index);
1297 if (index[ax] >= begin && index[ax] < end) {
1298 index[ax] -= begin;
1299
1300 int dest;
1301 size_t pos;
1302 T_new.local_position(index, dest, pos);
1303
1304 local_position[i] = pos;
1305 dest_mpirank[i] = dest;
1306 } else {
1307 // not send
1308 local_position[i] = 0;
1309 dest_mpirank[i] = mpisize;
1310 }
1311 }
1312 }
1313
1314 /* exchange data */
1315 replace_matrix_data(T.get_matrix(), dest_mpirank, local_position,
1316 T_new.get_matrix());
1317
1318 return T_new;
1319}
1320
1322
1335template <typename MatrixType>
1337 const Index &index_end) {
1338 const int mpisize = T.get_comm_size();
1339 const internal::UShape &shape = T.internal_shape();
1340 const size_t rank = T.rank();
1342 internal::normalize_slice_ranges(index_begin, index_end, shape, begin, end);
1343
1345 shape_new.resize(rank);
1346 for (size_t r = 0; r < rank; ++r) shape_new[r] = end[r] - begin[r];
1347
1348 /* initialize new tensor */
1349 Tensor<MatrixType> T_new(T.get_comm(), shape_new, shape_new.size() / 2,
1350 internal::normalized);
1351
1352 /* create lists of local position and destination rank */
1353 const size_t local_size = T.local_size();
1354 std::vector<int> dest_mpirank(local_size);
1355 std::vector<size_t> local_position(local_size);
1356
1357 T.prep_local_to_global();
1358 T_new.prep_global_to_local();
1359
1360#pragma omp parallel default(shared)
1361 {
1362 internal::UIndex index;
1363 index.resize(T.rank());
1364#pragma omp for
1365 for (size_t i = 0; i < local_size; ++i) {
1366 T.global_index_fast(i, index);
1367
1368 bool is_send = true;
1369 for (size_t r = 0; r < rank; ++r) {
1370 const size_t idx = index[r];
1371 if (idx >= begin[r] && idx < end[r]) {
1372 index[r] -= begin[r];
1373 } else {
1374 is_send = false;
1375 break;
1376 }
1377 }
1378
1379 if (is_send) {
1380 int dest;
1381 size_t pos;
1382 T_new.local_position(index, dest, pos);
1383
1384 local_position[i] = pos;
1385 dest_mpirank[i] = dest;
1386 } else {
1387 // not send
1388 local_position[i] = 0;
1389 dest_mpirank[i] = mpisize;
1390 }
1391 }
1392 }
1393
1394 /* exchange data */
1395 replace_matrix_data(T.get_matrix(), dest_mpirank, local_position,
1396 T_new.get_matrix());
1397
1398 return T_new;
1399};
1400
1402
1410template <typename MatrixType>
1413 assert(T.rank() == ushape.size());
1414 assert(debug::check_extend(T.internal_shape(), ushape));
1415
1416 /* initialize new tensor */
1417 Tensor<MatrixType> T_new(T.get_comm(), ushape, ushape.size() / 2,
1418 internal::normalized);
1419
1420 /* create lists of local position and destination rank */
1421 const size_t local_size = T.local_size();
1422 std::vector<int> dest_mpirank(local_size);
1423 std::vector<size_t> local_position(local_size);
1424
1425 T.prep_local_to_global();
1426 T_new.prep_global_to_local();
1427
1428#pragma omp parallel default(shared)
1429 {
1430 internal::UIndex index;
1431 index.resize(T.rank());
1432#pragma omp for
1433 for (size_t i = 0; i < local_size; ++i) {
1434 T.global_index_fast(i, index);
1435
1436 int dest;
1437 size_t pos;
1438 T_new.local_position(index, dest, pos);
1439
1440 local_position[i] = pos;
1441 dest_mpirank[i] = dest;
1442 }
1443 }
1444
1445 /* exchange data */
1446 replace_matrix_data(T.get_matrix(), dest_mpirank, local_position,
1447 T_new.get_matrix());
1448
1449 return T_new;
1450}
1451
1453
1458template <typename MatrixType>
1459typename MatrixType::value_type trace(const Tensor<MatrixType> &M) {
1460 assert(M.rank() == 2);
1461 return matrix_trace(M.get_matrix());
1462};
1463
1465
1478template <typename MatrixType>
1479typename MatrixType::value_type trace(const Tensor<MatrixType> &T, const Axes &axes_1, const Axes &axes_2) {
1480 const internal::UAxes ax1 = normalize_axes(axes_1, T.rank());
1481 const internal::UAxes ax2 = normalize_axes(axes_2, T.rank());
1482 assert(ax1.size() == ax2.size());
1483 assert(ax1.size() + ax2.size() == T.rank());
1484 assert(debug::check_trace_axes(ax1, ax2, T.rank()));
1485
1486 if (T.rank() == 2) return trace(T);
1487
1488 const size_t n = T.local_size();
1489 const size_t l = ax1.size();
1490 internal::UIndex index;
1491 bool check;
1492 typename MatrixType::value_type sum(0.0);
1493
1494 index.resize(T.rank());
1495 for (size_t i = 0; i < n; ++i) {
1496 T.global_index_fast(i, index);
1497 check = true;
1498 for (size_t k = 0; k < l; ++k) {
1499 if (index[ax1[k]] != index[ax2[k]]) {
1500 check = false;
1501 break;
1502 }
1503 }
1504 if (check) {
1505 sum += T[i];
1506 }
1507 }
1508
1509 return T.get_matrix().allreduce_sum(sum);
1510};
1511
1513
1526template <typename MatrixType>
1527typename MatrixType::value_type trace(const Tensor<MatrixType> &A, const Tensor<MatrixType> &B,
1528 const Axes &axes_a, const Axes &axes_b) {
1529 const internal::UAxes ax_a = normalize_axes(axes_a, A.rank());
1530 const internal::UAxes ax_b = normalize_axes(axes_b, B.rank());
1531 assert(A.rank() == B.rank());
1532 assert(A.rank() == ax_a.size());
1533 assert(B.rank() == ax_b.size());
1534 assert(debug::check_trace_axes(ax_a, ax_b, A.internal_shape(), B.internal_shape()));
1535
1536 const size_t rank = A.rank();
1539 internal::UAxes axes_map = A.get_axes_map();
1540 axes.resize(rank);
1541 axes_a_inv.resize(rank);
1542
1543 for (size_t i = 0; i < rank; ++i) {
1544 axes_a_inv[ax_a[i]] = i;
1545 }
1546 for (size_t i = 0; i < rank; ++i) {
1547 axes[i] = ax_b[axes_a_inv[axes_map[i]]];
1548 }
1549
1550 const size_t n = A.local_size();
1551 Tensor<MatrixType> B_t = internal::transpose_impl(B, axes, A.get_upper_rank());
1552 typename MatrixType::value_type sum(0.0);
1553
1554 for (size_t i = 0; i < n; ++i) {
1555 sum += A[i] * B_t[i];
1556 }
1557
1558 return A.get_matrix().allreduce_sum(sum);
1559}
1560
1562
1572template <typename MatrixType>
1574 const Axes &axes_2) {
1575 const int mpisize = T.get_comm_size();
1576 const internal::UAxes ax1 = normalize_axes(axes_1, T.rank());
1577 const internal::UAxes ax2 = normalize_axes(axes_2, T.rank());
1578 assert(ax1.size() == ax2.size());
1579 assert(ax1.size() + ax2.size() < T.rank());
1580 if (ax1.size() == 0) return T;
1581 assert(debug::check_contract_axes(ax1, ax2, T.rank()));
1582
1583 internal::UShape shape = T.internal_shape();
1586 {
1588 v.sort();
1589 size_t k = 0;
1590 size_t n = v.size();
1591 for (size_t i = 0; i < T.rank(); ++i) {
1592 if (k < n && i == v[k]) {
1593 ++k;
1594 } else {
1595 shape_new.push(shape[i]);
1596 axes_new.push(i);
1597 }
1598 }
1599 }
1600
1601 /* initialize new tensor */
1602 Tensor<MatrixType> T_new(T.get_comm(), shape_new, shape_new.size() / 2,
1603 internal::normalized);
1604
1605 /* create lists of local position and destination rank */
1606 std::vector<int> dest_mpirank(T.local_size());
1607 std::vector<size_t> local_position(T.local_size());
1608
1610 index.resize(T.rank());
1611 index_new.resize(T_new.rank());
1612 bool check;
1613 const size_t n = ax1.size();
1614
1615 for (size_t i = 0; i < T.local_size(); ++i) {
1616 T.global_index_fast(i, index);
1617 check = true;
1618 for (size_t k = 0; k < n; ++k) {
1619 if (index[ax1[k]] != index[ax2[k]]) {
1620 check = false;
1621 continue;
1622 }
1623 }
1624
1625 if (check) {
1626 for (size_t k = 0; k < index_new.size(); ++k) {
1627 index_new[k] = index[axes_new[k]];
1628 }
1629
1630 int dest;
1631 size_t pos;
1632 T_new.local_position(index_new, dest, pos);
1633
1634 local_position[i] = pos;
1635 dest_mpirank[i] = dest;
1636 } else {
1637 // not send
1638 local_position[i] = 0;
1639 dest_mpirank[i] = mpisize;
1640 }
1641 }
1642
1643 /* sum data */
1644 sum_matrix_data(T.get_matrix(), dest_mpirank, local_position,
1645 T_new.get_matrix());
1646
1647 return T_new;
1648}
1649
1651
1668template <typename MatrixType>
1670 assert(a.rank() == b.rank());
1671 assert(a.get_comm() == b.get_comm());
1672
1674 const internal::UShape shape_b = b.internal_shape();
1676 const size_t n = shape_a.size();
1678 axes_trans.resize(2 * n);
1679 for (size_t i = 0; i < shape_b.size(); ++i) {
1680 shape_c[i] *= shape_b[i];
1681 axes_trans[2 * i] = i;
1682 axes_trans[2 * i + 1] = i + n;
1683 }
1684
1686 tensordot(internal::reshape_impl(a, shape_a + internal::UShape(1)),
1687 internal::reshape_impl(b, internal::UShape(1) + shape_b), Axes(n),
1688 Axes(0));
1689 ab.transpose(to_public(axes_trans));
1690 return internal::reshape_impl(ab, shape_c);
1691};
1692
1694
1705template <typename MatrixType>
1707 const Tensor<MatrixType> &b, const Axes &axes_a,
1708 const Axes &axes_b) {
1710 const internal::UAxes ax_b = normalize_axes(axes_b, b.rank());
1711 assert(ax_a.size() == ax_b.size());
1712 assert(a.get_comm() == b.get_comm());
1714 const internal::UShape shape_b = b.internal_shape();
1715 for (size_t i = 0; i < ax_a.size(); ++i) {
1716 assert(shape_a[ax_a[i]] == shape_b[ax_b[i]]);
1717 }
1718
1719 const typename Tensor<MatrixType>::comm_type &comm = a.get_comm();
1720
1722 const size_t rank_row_c = a.rank() - ax_a.size();
1723 const size_t rank_col_c = b.rank() - ax_b.size();
1724 shape_c.resize(rank_row_c + rank_col_c);
1725
1728 size_t urank_a;
1729 size_t urank_b;
1730
1731 {
1732 const size_t rank = a.rank();
1733 const size_t rank_row = rank - ax_a.size();
1734 const size_t rank_col = ax_a.size();
1735 std::vector<size_t> v(rank);
1736 for (size_t i = 0; i < rank; ++i) v[i] = i;
1737 for (size_t i = 0; i < rank_col; ++i) v[ax_a[i]] = rank;
1738 std::sort(v.begin(), v.end());
1739 for (size_t i = 0; i < rank_col; ++i) v[rank_row + i] = ax_a[i];
1740
1741 trans_axes_a.assign(rank, v.data());
1742 urank_a = rank_row;
1743
1744 for (size_t i = 0; i < rank_row; ++i) shape_c[i] = shape_a[v[i]];
1745 }
1746
1747 {
1748 const size_t rank = b.rank();
1749 const size_t rank_row = ax_b.size();
1750 const size_t rank_col = rank - ax_b.size();
1751 std::vector<size_t> v(rank);
1752 for (size_t i = 0; i < rank; ++i) v[i] = i;
1753 for (size_t i = 0; i < rank_row; ++i) v[ax_b[i]] = 0;
1754 std::sort(v.begin(), v.end());
1755 for (size_t i = 0; i < rank_row; ++i) v[i] = ax_b[i];
1756
1757 trans_axes_b.assign(rank, v.data());
1758 urank_b = rank_row;
1759
1760 for (size_t i = 0; i < rank_col; ++i)
1762 }
1763
1764 Tensor<MatrixType> c(comm, shape_c, rank_row_c, internal::normalized);
1765 matrix_product(internal::transpose_impl(a, trans_axes_a, urank_a).get_matrix(),
1766 internal::transpose_impl(b, trans_axes_b, urank_b).get_matrix(),
1767 c.get_matrix());
1768
1769 return c;
1770};
1771
1773
1781template <typename MatrixType>
1782int svd(const Tensor<MatrixType> &a, std::vector<double> &s) {
1783 assert(a.rank() == 2);
1784 int info;
1785 info = svd(a, Axes(0), Axes(1), s);
1786 return info;
1787}
1788
1790
1802template <typename MatrixType>
1804 std::vector<double> &s, Tensor<MatrixType> &vt) {
1805 assert(a.rank() == 2);
1806 int info;
1807 info = svd(a, Axes(0), Axes(1), u, s, vt);
1808 return info;
1809}
1810
1812
1822template <typename MatrixType>
1823int svd(const Tensor<MatrixType> &a, const Axes &axes_row, const Axes &axes_col,
1824 std::vector<double> &s) {
1827 const internal::UAxes axes = row + col;
1828 assert(row.size() > 0);
1829 assert(col.size() > 0);
1830 assert(debug::check_svd_axes(row, col, a.rank()));
1831
1832 const size_t rank = a.rank();
1833 const size_t urank = row.size();
1834
1835
1836 Tensor<MatrixType> a_t = internal::transpose_impl(a, axes, urank);
1837 const internal::UShape &shape = a_t.internal_shape();
1838
1839 size_t d_row(1), d_col(1);
1840 for (size_t i = 0; i < urank; ++i) d_row *= shape[i];
1841 for (size_t i = urank; i < rank; ++i) d_col *= shape[i];
1842 size_t size = (d_row < d_col) ? d_row : d_col;
1843
1844 s.resize(size);
1845
1846 int info;
1847 info = matrix_svd(a_t.get_matrix(), s);
1848
1849 return info;
1850}
1851
1853
1872template <typename MatrixType>
1873int svd(const Tensor<MatrixType> &a, const Axes &axes_row, const Axes &axes_col,
1874 Tensor<MatrixType> &u, std::vector<double> &s, Tensor<MatrixType> &vt) {
1877 const internal::UAxes axes = row + col;
1878 assert(row.size() > 0);
1879 assert(col.size() > 0);
1880 assert(debug::check_svd_axes(row, col, a.rank()));
1881
1882 const size_t rank = a.rank();
1883 const size_t urank = row.size();
1884
1885
1886 Tensor<MatrixType> a_t = internal::transpose_impl(a, axes, urank);
1887 const internal::UShape &shape = a_t.internal_shape();
1888
1889 size_t d_row(1), d_col(1);
1890 for (size_t i = 0; i < urank; ++i) d_row *= shape[i];
1891 for (size_t i = urank; i < rank; ++i) d_col *= shape[i];
1892 size_t size = (d_row < d_col) ? d_row : d_col;
1893
1895 shape_u.resize(urank + 1);
1896 for (size_t i = 0; i < urank; ++i) shape_u[i] = shape[i];
1897 shape_u[urank] = size;
1898
1900 shape_vt.resize(rank - urank + 1);
1901 shape_vt[0] = size;
1902 for (size_t i = urank; i < rank; ++i) shape_vt[i - urank + 1] = shape[i];
1903
1904 size_t urank_u = urank;
1905 size_t urank_vt = 1;
1906
1907 u = Tensor<MatrixType>(a.get_comm(), shape_u, urank_u, internal::normalized);
1908 vt = Tensor<MatrixType>(a.get_comm(), shape_vt, urank_vt, internal::normalized);
1909 s.resize(size);
1910
1911 int info;
1912 info = matrix_svd(a_t.get_matrix(), u.get_matrix(), s, vt.get_matrix());
1913
1914 return info;
1915}
1916
1918
1929template <typename MatrixType>
1930int psvd(const Tensor<MatrixType> &a, std::vector<double> &s,
1931 const size_t target_rank) {
1932 if (target_rank == 0) {
1933 throw std::invalid_argument("mptensor: psvd() target_rank must be positive");
1934 }
1935 int info = svd(a, s);
1936 if (s.size() > target_rank) s.resize(target_rank);
1937 return info;
1938}
1939
1941
1956template <typename MatrixType>
1958 std::vector<double> &s, Tensor<MatrixType> &vt,
1959 const size_t target_rank) {
1960 if (target_rank == 0) {
1961 throw std::invalid_argument("mptensor: psvd() target_rank must be positive");
1962 }
1963 int info = svd(a, u, s, vt);
1964 if (s.size() > target_rank) {
1965 s.resize(target_rank);
1966 u = slice(u, 1, 0, target_rank);
1967 vt = slice(vt, 0, 0, target_rank);
1968 }
1969 return info;
1970}
1971
1973
1986template <typename MatrixType>
1987int psvd(const Tensor<MatrixType> &a, const Axes &axes_row, const Axes &axes_col,
1988 std::vector<double> &s, const size_t target_rank) {
1989 if (target_rank == 0) {
1990 throw std::invalid_argument("mptensor: psvd() target_rank must be positive");
1991 }
1992 int info = svd(a, axes_row, axes_col, s);
1993 if (s.size() > target_rank) s.resize(target_rank);
1994 return info;
1995}
1996
1998
2020template <typename MatrixType>
2021int psvd(const Tensor<MatrixType> &a, const Axes &axes_row, const Axes &axes_col,
2022 Tensor<MatrixType> &u, std::vector<double> &s, Tensor<MatrixType> &vt,
2023 const size_t target_rank) {
2024 if (target_rank == 0) {
2025 throw std::invalid_argument("mptensor: psvd() target_rank must be positive");
2026 }
2027 int info = svd(a, axes_row, axes_col, u, s, vt);
2028 if (s.size() > target_rank) {
2029 s.resize(target_rank);
2030 u = slice(u, axes_row.size(), 0, target_rank);
2031 vt = slice(vt, 0, 0, target_rank);
2032 }
2033 return info;
2034}
2035
2037
2049template <typename MatrixType>
2051 assert(a.rank() == 2);
2052 int info;
2053 info = qr(a, Axes(0), Axes(1), q, r);
2054 return info;
2055}
2056
2058
2081template <typename MatrixType>
2082int qr(const Tensor<MatrixType> &a, const Axes &axes_row, const Axes &axes_col,
2086 const internal::UAxes axes = row + col;
2087 assert(row.size() > 0);
2088 assert(col.size() > 0);
2089 assert(debug::check_svd_axes(row, col, a.rank()));
2090
2091 const size_t rank = a.rank();
2092 const size_t urank = row.size();
2094
2095 internal::UShape shape;
2096 shape.resize(rank);
2097 for (size_t i = 0; i < rank; ++i) shape[i] = shape_a[axes[i]];
2098
2099 size_t d_row(1), d_col(1);
2100 for (size_t i = 0; i < urank; ++i) d_row *= shape[i];
2101 for (size_t i = urank; i < rank; ++i) d_col *= shape[i];
2102 size_t size = (d_row < d_col) ? d_row : d_col;
2103
2104 // rank-2 tensors (matrices)
2106 internal::reshape_impl(internal::transpose_impl(a, axes, urank), internal::UShape(d_row, d_col));
2107 Tensor<MatrixType> mat_r(mat_q.get_comm(), mat_q.internal_shape(), 1, internal::normalized);
2108
2109 // QR decomposition. Elements of mat_q change from a to q.
2110 int info;
2111 info = matrix_qr(mat_q.get_matrix(), mat_r.get_matrix());
2112
2113 // Get shape of q and r.
2115 shape_q.resize(urank + 1);
2116 for (size_t i = 0; i < urank; ++i) shape_q[i] = shape[i];
2117 shape_q[urank] = size;
2118
2120 shape_r.resize(rank - urank + 1);
2121 shape_r[0] = size;
2122 for (size_t i = urank; i < rank; ++i) shape_r[i - urank + 1] = shape[i];
2123
2124 // Reshape q and r.
2125 if (d_row > d_col) {
2126 q = internal::reshape_impl(mat_q, shape_q);
2127 r = internal::reshape_impl(slice(mat_r, 0, 0, size), shape_r);
2128 } else if (d_row < d_col) {
2129 q = internal::reshape_impl(slice(mat_q, 1, 0, size), shape_q);
2130 r = internal::reshape_impl(mat_r, shape_r);
2131 } else { // d_row==d_col
2132 q = internal::reshape_impl(mat_q, shape_q);
2133 r = internal::reshape_impl(mat_r, shape_r);
2134 }
2135
2136 return info;
2137}
2138
2141
2148template <typename MatrixType>
2149int eigh(const Tensor<MatrixType> &a, std::vector<double> &w,
2151 assert(a.rank() == 2);
2152 const internal::UShape shape = a.internal_shape();
2153 assert(shape[0] == shape[1]);
2154
2155 Tensor<MatrixType> a_t = internal::transpose_impl(a, internal::UAxes(0, 1), 1);
2156
2157 size_t n = shape[0];
2158 w.resize(n);
2159 z = Tensor<MatrixType>(a.get_comm(), internal::UShape(n, n), 1, internal::normalized);
2160
2161 int info;
2162 info = matrix_eigh(a_t.get_matrix(), w, z.get_matrix());
2163 return info;
2164};
2165
2168
2174template <typename MatrixType>
2175int eigh(const Tensor<MatrixType> &a, std::vector<double> &w) {
2176 assert(a.rank() == 2);
2177 const internal::UShape shape = a.internal_shape();
2178 assert(shape[0] == shape[1]);
2179
2180 Tensor<MatrixType> a_t = internal::transpose_impl(a, internal::UAxes(0, 1), 1);
2181
2182 size_t n = shape[0];
2183 w.resize(n);
2184
2185 int info;
2186 info = matrix_eigh(a_t.get_matrix(), w);
2187 return info;
2188};
2189
2192
2212template <typename MatrixType>
2213int eigh(const Tensor<MatrixType> &a, const Axes &axes_row, const Axes &axes_col,
2214 std::vector<double> &w, Tensor<MatrixType> &z) {
2217 const internal::UAxes axes = row + col;
2218 assert(row.size() > 0);
2219 assert(col.size() > 0);
2220
2221 const size_t rank = a.rank();
2222 const size_t urank = row.size();
2223
2224
2225 Tensor<MatrixType> a_t = internal::transpose_impl(a, axes, urank);
2226 const internal::UShape &shape = a_t.internal_shape();
2227
2228 size_t d_row(1), d_col(1);
2229 for (size_t i = 0; i < urank; ++i) d_row *= shape[i];
2230 for (size_t i = urank; i < rank; ++i) d_col *= shape[i];
2231 size_t size = (d_row < d_col) ? d_row : d_col;
2232
2233 assert(d_row == d_col);
2234
2236 shape_z.resize(urank + 1);
2237 for (size_t i = 0; i < urank; ++i) shape_z[i] = shape[i];
2238 shape_z[urank] = size;
2239
2240 z = Tensor<MatrixType>(a.get_comm(), shape_z, urank, internal::normalized);
2241 w.resize(size);
2242
2243 int info;
2244 info = matrix_eigh(a_t.get_matrix(), w, z.get_matrix());
2245 return info;
2246};
2247
2249
2259template <typename MatrixType>
2260int eigh(const Tensor<MatrixType> &a, const Axes &axes_row, const Axes &axes_col,
2261 std::vector<double> &w) {
2264 const internal::UAxes axes = row + col;
2265 assert(row.size() > 0);
2266 assert(col.size() > 0);
2267
2268 const size_t rank = a.rank();
2269 const size_t urank = row.size();
2270
2271
2272 Tensor<MatrixType> a_t = internal::transpose_impl(a, axes, urank);
2273 const internal::UShape &shape = a_t.internal_shape();
2274
2275 size_t d_row(1), d_col(1);
2276 for (size_t i = 0; i < urank; ++i) d_row *= shape[i];
2277 for (size_t i = urank; i < rank; ++i) d_col *= shape[i];
2278 size_t size = (d_row < d_col) ? d_row : d_col;
2279 w.resize(size);
2280
2281 assert(d_row == d_col);
2282
2283 int info;
2284 info = matrix_eigh(a_t.get_matrix(), w);
2285 return info;
2286};
2287
2290
2321template <typename MatrixType>
2323 const Axes &axes_col_a, const Tensor<MatrixType> &b,
2324 const Axes &axes_row_b, const Axes &axes_col_b, std::vector<double> &w,
2332 assert(row_a.size() > 0);
2333 assert(col_a.size() > 0);
2334 assert(row_b.size() > 0);
2335 assert(col_b.size() > 0);
2336
2337 const size_t rank_a = a.rank();
2338 const size_t rank_b = b.rank();
2339 const size_t urank_a = row_a.size();
2340 const size_t urank_b = row_b.size();
2341 Tensor<MatrixType> a_t = internal::transpose_impl(a, axes_a, urank_a);
2342 Tensor<MatrixType> b_t = internal::transpose_impl(b, axes_b, urank_b);
2343 const internal::UShape &shape_a = a_t.internal_shape();
2344 const internal::UShape &shape_b = b_t.internal_shape();
2345 size_t d_row_a(1), d_col_a(1);
2346 size_t d_row_b(1), d_col_b(1);
2347 for (size_t i = 0; i < urank_a; ++i) d_row_a *= shape_a[i];
2348 for (size_t i = 0; i < urank_b; ++i) d_row_b *= shape_b[i];
2349 for (size_t i = urank_a; i < rank_a; ++i) d_col_a *= shape_a[i];
2350 for (size_t i = urank_b; i < rank_b; ++i) d_col_b *= shape_b[i];
2351
2355 size_t size = d_row_a;
2356
2359 for (size_t i = urank_a; i < rank_a; ++i) shape_z[i - urank_a] = shape_a[i];
2360 shape_z[rank_a - urank_a] = size;
2361
2362 z = Tensor<MatrixType>(a.get_comm(), shape_z, urank_a, internal::normalized);
2363 w.resize(size);
2364
2365 int info;
2366 info = matrix_eigh(a_t.get_matrix(), b_t.get_matrix(), w, z.get_matrix());
2367
2368 return info;
2369};
2370
2372
2380template <typename MatrixType>
2381int eig(const Tensor<MatrixType> &a, std::vector<complex> &w,
2383 assert(a.rank() == 2);
2384 const internal::UShape shape = a.internal_shape();
2385 assert(shape[0] == shape[1]);
2386
2387 Tensor<lapack::Matrix<typename MatrixType::value_type>> a_t = internal::transpose_impl(a, internal::UAxes(0, 1), 1).gather();
2388
2389 size_t n = shape[0];
2390 w.resize(n);
2391
2392 // A LAPACK tensor has only a dummy communicator; MPI_Comm may not convert to it.
2394 internal::normalized);
2395
2396 int info;
2397 info = matrix_eig(a_t.get_matrix(), w, z_t.get_matrix());
2398
2400
2401 return info;
2402};
2403
2405
2412template <typename MatrixType>
2413int eig(const Tensor<MatrixType> &a, std::vector<complex> &w) {
2414 assert(a.rank() == 2);
2415 const internal::UShape shape = a.internal_shape();
2416 assert(shape[0] == shape[1]);
2417
2418 Tensor<lapack::Matrix<typename MatrixType::value_type>> a_t = internal::transpose_impl(a, internal::UAxes(0, 1), 1).gather();
2419
2420 size_t n = shape[0];
2421 w.resize(n);
2422
2423 int info;
2424 info = matrix_eig(a_t.get_matrix(), w);
2425 return info;
2426};
2427
2429
2449template <typename MatrixType>
2450int eig(const Tensor<MatrixType> &a, const Axes &axes_row, const Axes &axes_col,
2451 std::vector<complex> &w, typename Tensor<MatrixType>::template rebind<complex> &z) {
2454 const internal::UAxes axes = row + col;
2455 assert(row.size() > 0);
2456 assert(col.size() > 0);
2457
2458 const size_t rank = a.rank();
2459 const size_t urank = row.size();
2460
2461
2462 Tensor<lapack::Matrix<typename MatrixType::value_type>> a_t = internal::transpose_impl(a, axes, urank).gather();
2463 const internal::UShape &shape = a_t.internal_shape();
2464
2465 size_t d_row(1), d_col(1);
2466 for (size_t i = 0; i < urank; ++i) d_row *= shape[i];
2467 for (size_t i = urank; i < rank; ++i) d_col *= shape[i];
2468 size_t size = (d_row < d_col) ? d_row : d_col;
2469
2470 assert(d_row == d_col);
2471
2473 shape_z.resize(urank + 1);
2474 for (size_t i = 0; i < urank; ++i) shape_z[i] = shape[i];
2475 shape_z[urank] = size;
2476
2477 // A LAPACK tensor has only a dummy communicator; MPI_Comm may not convert to it.
2479 internal::normalized);
2480
2481 w.resize(size);
2482
2483 int info;
2484 info = matrix_eig(a_t.get_matrix(), w, z_t.get_matrix());
2485
2487
2488 return info;
2489};
2490
2492
2502template <typename MatrixType>
2503int eig(const Tensor<MatrixType> &a, const Axes &axes_row, const Axes &axes_col,
2504 std::vector<complex> &w) {
2507 const internal::UAxes axes = row + col;
2508 assert(row.size() > 0);
2509 assert(col.size() > 0);
2510
2511 const size_t rank = a.rank();
2512 const size_t urank = row.size();
2513
2514
2515 Tensor<lapack::Matrix<typename MatrixType::value_type>> a_t = internal::transpose_impl(a, axes, urank).gather();
2516 const internal::UShape &shape = a_t.internal_shape();
2517
2518 size_t d_row(1), d_col(1);
2519 for (size_t i = 0; i < urank; ++i) d_row *= shape[i];
2520 for (size_t i = urank; i < rank; ++i) d_col *= shape[i];
2521 size_t size = (d_row < d_col) ? d_row : d_col;
2522 w.resize(size);
2523
2524 assert(d_row == d_col);
2525
2526 int info;
2527 info = matrix_eig(a_t.get_matrix(), w);
2528 return info;
2529};
2530
2531
2533
2540template <typename MatrixType>
2541int solve(const Tensor<MatrixType> &a, const std::vector<typename MatrixType::value_type> &b,
2542 std::vector<typename MatrixType::value_type> &x) {
2543 assert(a.rank() == 2);
2544 assert(a.internal_shape()[0] == a.internal_shape()[1]);
2545 assert(a.internal_shape()[0] == b.size());
2546
2548 int info;
2549 info = solve(a, Tensor<MatrixType>(a.get_comm(), b), x_t, Axes(0), Axes(1),
2550 Axes(0), Axes());
2551 x = x_t.flatten();
2552
2553 return info;
2554};
2555
2557
2564template <typename MatrixType>
2567 assert(a.rank() == 2);
2568 assert(b.rank() == 2 || b.rank() == 1);
2569 assert(a.internal_shape()[0] == a.internal_shape()[1]);
2570 assert(a.internal_shape()[0] == b.internal_shape()[0]);
2571 int info;
2572
2573 if (b.rank() == 2) {
2574 info = solve(a, b, x, Axes(0), Axes(1), Axes(0), Axes(1));
2575 } else {
2576 info = solve(a, b, x, Axes(0), Axes(1), Axes(0), Axes());
2577 }
2578
2579 return info;
2580};
2581
2583
2610template <typename MatrixType>
2613 const Axes &axes_row_b, const Axes &axes_col_b) {
2620 assert(a.rank() == row_a.size() + col_a.size());
2621 assert(b.rank() == row_b.size() + col_b.size());
2622 assert(row_a.size() > 0);
2623 assert(col_a.size() > 0);
2624 assert(row_b.size() > 0);
2625 assert(col_b.size() >= 0);
2626
2627 // size_t rank_a = a.rank();
2628 // size_t rank_b = b.rank();
2629 size_t rank_row_a = row_a.size();
2630 size_t rank_col_a = col_a.size();
2631 size_t rank_row_b = row_b.size();
2632 size_t rank_col_b = col_b.size();
2633
2634 Tensor<MatrixType> a_t = internal::transpose_impl(a, axes_a, rank_row_a);
2635 Tensor<MatrixType> b_t = internal::transpose_impl(b, axes_b, rank_row_b);
2636 const internal::UShape &shape_a = a_t.internal_shape();
2637 const internal::UShape &shape_b = b_t.internal_shape();
2638
2639 assert(debug::check_square(shape_a, rank_row_a));
2640
2643 for (size_t i = 0; i < rank_col_a; ++i) shape_x[i] = shape_a[i + rank_row_a];
2644 for (size_t i = 0; i < rank_col_b; ++i)
2646
2647 int info;
2648 info = matrix_solve(a_t.get_matrix(), b_t.get_matrix());
2649 x = internal::reshape_impl(b_t, shape_x);
2650 return info;
2651};
2652
2654template <typename MatrixType>
2659template <typename MatrixType>
2661 return (rhs *= -1.0);
2662}
2664template <typename MatrixType>
2666 const Tensor<MatrixType> &rhs) {
2667 return (lhs += rhs);
2668}
2670template <typename MatrixType>
2672 const Tensor<MatrixType> &rhs) {
2673 return (lhs -= rhs);
2674}
2676template <typename MatrixType, typename D>
2678 return (lhs *= rhs);
2679}
2681template <typename MatrixType, typename D>
2683 return (lhs /= rhs);
2684}
2686template <typename MatrixType, typename D>
2688 return (rhs *= lhs);
2689}
2690
2692template <typename MatrixType>
2693inline double max(const Tensor<MatrixType> &t) {
2694 return max(t.get_matrix());
2695}
2697template <typename MatrixType>
2698inline double min(const Tensor<MatrixType> &t) {
2699 return min(t.get_matrix());
2700}
2702template <typename MatrixType>
2703inline double max_abs(const Tensor<MatrixType> &t) {
2704 return max_abs(t.get_matrix());
2705}
2707template <typename MatrixType>
2708inline double min_abs(const Tensor<MatrixType> &t) {
2709 return min_abs(t.get_matrix());
2710}
2711
2713template <typename MatrixType>
2715 using value_type = typename MatrixType::value_type;
2716 if constexpr (std::is_same_v<value_type, double>) {
2717 return t.map(static_cast<double (*)(double)>(&std::sqrt));
2718 } else {
2719 return t.map(static_cast<complex (*)(const complex &)>(&std::sqrt));
2720 }
2721}
2722template <typename MatrixType>
2724 using value_type = typename MatrixType::value_type;
2725 if constexpr (std::is_same_v<value_type, double>) {
2726 return t;
2727 } else {
2728 return t.map(static_cast<complex (*)(const complex &)>(&std::conj));
2729 }
2730}
2732
2734
2738template <typename MatrixType>
2739std::ostream &operator<<(std::ostream &out, const Tensor<MatrixType> &t) {
2740 std::size_t dim = t.ndim();
2741 if (t.get_comm_size() == 1) {
2742 std::size_t size = t.local_size();
2743 const internal::UShape &shape = t.internal_shape();
2744 std::vector<std::size_t> accum(dim + 1, 1);
2745 for (size_t d = dim; d != 0; --d) accum[d - 1] = accum[d] * shape[d - 1];
2746
2747 Index idx;
2748 idx.resize(dim);
2749 for (size_t i = 0; i < size; ++i) {
2750 for (size_t d = 0; d < dim; ++d) {
2751 idx[d] = static_cast<std::ptrdiff_t>((i % accum[d]) / accum[d + 1]);
2752 }
2753 if (idx[dim - 1] == 0) {
2754 size_t count = 0;
2755 for (size_t d = dim; d != 0 && idx[d - 1] == 0; --d) ++count;
2756 for (size_t i = 0; i < dim - count; ++i) out << ' ';
2757 for (size_t i = 0; i < count; ++i) out << '[';
2758 }
2759
2760 using value_type = typename MatrixType::value_type;
2761 value_type val;
2762 t.get_value(idx, val);
2763 out << ' ' << val << ' ';
2764
2765 if (static_cast<size_t>(idx[dim - 1]) == shape[dim - 1] - 1) {
2766 size_t count = 0;
2767 for (size_t d = dim; d != 0 && static_cast<size_t>(idx[d - 1]) == shape[d - 1] - 1; --d) ++count;
2768 for (size_t i = 0; i < count; ++i) out << ']';
2769 if (count < dim) {
2770 for (size_t i = 0; i < count; ++i) out << '\n';
2771 } else {
2772 out << '\n';
2773 }
2774 }
2775 }
2776 } else {
2777 if (t.get_comm_rank() == 0) {
2778 for (size_t d = 0; d < dim; ++d) out << '[';
2779 out << " distributed tensor ... ";
2780 for (size_t d = 0; d < dim; ++d) out << ']';
2781 out << '\n';
2782 }
2783 }
2784 return out;
2785}
2786
2787} // namespace mptensor
2788
2789#endif // _TENSOR_IMPL_HPP_
Definition index.hpp:102
void resize(size_t n)
Definition index.hpp:137
void sort()
Definition index.hpp:139
size_t size() const
Definition index.hpp:135
Tensor class. The main object of mptensor.
Definition tensor.hpp:82
typename MatrixType::value_type value_type
double or complex
Definition tensor.hpp:84
size_t rank() const
Rank of tensor.
Definition tensor_impl.hpp:184
const comm_type & get_comm() const
Communicator.
Definition tensor_impl.hpp:236
size_t local_size() const
Size of local storage.
Definition tensor_impl.hpp:199
Tensor< MatrixType > & map(UnaryOperation op)
Apply a unary operation to each element.
Definition tensor_impl.hpp:1062
Shape shape() const
Shape of tensor.
Definition tensor_impl.hpp:172
bool get_value(const Index &idx, value_type &val) const
Get an element.
Definition tensor_impl.hpp:570
void prep_local_to_global() const
Preprocess for fast conversion from local index to global one.
Definition tensor_impl.hpp:278
void print_info(std::ostream &out, const std::string &tag="") const
Output information of tensor.
Definition tensor_impl.hpp:457
size_t ndim() const
Rank of tensor.
Definition tensor_impl.hpp:193
int get_comm_rank() const
Rank of process.
Definition tensor_impl.hpp:248
const internal::UShape & internal_shape() const
Shape as the internal type.
Definition tensor_impl.hpp:178
void local_position(const internal::UIndex &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:602
void prep_global_to_local() const
Preprocess for fast conversion from global index to local one.
Definition tensor_impl.hpp:272
const MatrixType & get_matrix() const
distributed Matrix
Definition tensor_impl.hpp:223
int get_comm_size() const
Size of communicator.
Definition tensor_impl.hpp:242
void global_index_fast(size_t i, internal::UIndex &idx) const
Convert local index to global index fast.
Definition tensor_impl.hpp:543
typename MatrixType::comm_type comm_type
type of communicator. MPI_Comm or int.
Definition tensor.hpp:86
int comm_type
Definition matrix_lapack.hpp:48
Define the type of a complex number.
Tensor< MatrixType > operator/(Tensor< MatrixType > lhs, D rhs)
Scalar division.
Definition tensor_impl.hpp:2682
Tensor< MatrixType > operator*(Tensor< MatrixType > lhs, D rhs)
Tensor-scalar multiplication.
Definition tensor_impl.hpp:2677
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:2381
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:1930
int eigh(const Tensor< MatrixType > &a, std::vector< double > &eigval, Tensor< MatrixType > &eigvec)
Definition tensor_impl.hpp:2149
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 to_public(const internal::UIndex &u)
Definition index.hpp:382
internal::UAxes normalize_axes(const Axes &axes, size_t rank)
Definition index.hpp:323
size_t normalize_index(std::ptrdiff_t i, size_t n)
Definition index.hpp:335
size_t normalize_axis(std::ptrdiff_t a, size_t rank)
Definition index.hpp:317
BasicIndex< T > operator+(const BasicIndex< T > &lhs, const BasicIndex< T > &rhs)
Definition index.hpp:203
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
UIndex UAxes
Definition index.hpp:224
Tensor< MatrixType > reshape_impl(const Tensor< MatrixType > &a, const UShape &shape_new)
Definition tensor_impl.hpp:1197
MatrixType::value_type trace(const Tensor< MatrixType > &a)
Trace of matrix (rank-2 tensor)
Definition tensor_impl.hpp:1459
Tensor< MatrixType > contract(const Tensor< MatrixType > &a, const Axes &axes_1, const Axes &axes_2)
Partial trace of tensor.
Definition tensor_impl.hpp:1573
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 > kron(const Tensor< MatrixType > &a, const Tensor< MatrixType > &b)
Compute the Kronecker product.
Definition tensor_impl.hpp:1669
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:2541
Tensor< MatrixType > conj(Tensor< MatrixType > t)
Take conjugate of each element.
Tensor< MatrixType > extend(const Tensor< MatrixType > &a, const Shape &shape_new)
Extend the size of a tensor.
Definition tensor_impl.hpp:1411
Tensor< MatrixType > transpose(Tensor< MatrixType > a, const Axes &axes)
Transposition of tensor with lazy evaluation.
Definition tensor_impl.hpp:1077
Tensor< MatrixType > reshape(const Tensor< MatrixType > &a, const Shape &shape_new)
Change the shape of tensor.
Definition tensor_impl.hpp:1110
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
Tensor()
Default constructor of tensor.
Definition tensor_impl.hpp:71
header file of BasicIndex class template
List of header files for matrix classes.
mptensor::complex complex
Definition matrix_lapack.cc:36
bool check_contract_axes(const internal::UAxes &axes_1, const internal::UAxes &axes_2, size_t rank)
Definition tensor.cc:114
bool check_square(const internal::UShape &shape, size_t urank)
Definition tensor.cc:125
bool check_svd_axes(const internal::UAxes &axes_row, const internal::UAxes &axes_col, size_t rank)
Definition tensor.cc:76
bool check_transpose_axes(const internal::UAxes &axes, size_t rank)
Definition tensor.cc:66
bool check_extend(const internal::UShape &s_old, const internal::UShape &s_new)
Definition tensor.cc:57
bool check_trace_axes(const internal::UAxes &axes_1, const internal::UAxes &axes_2, size_t rank)
Definition tensor.cc:86
bool check_total_size(const internal::UShape &s1, const internal::UShape &s2)
Definition tensor.cc:49
void replace_matrix_data(const Matrix< C > &M, const std::vector< int > &dest_rank, const std::vector< size_t > &local_position, Matrix< C > &M_new)
Definition complex.hpp:34
bool is_no_transpose(const internal::UAxes &axes, const internal::UAxes &axes_map, size_t rank)
Definition tensor.cc:35
Definition tensor.hpp:50
Tensor class.
double operator-(Timer &t1, Timer &t0)
Definition timer.hpp:64