-
Notifications
You must be signed in to change notification settings - Fork 161
/
indexset.h
899 lines (743 loc) · 19.3 KB
/
indexset.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
//
// Copyright 2018 The Simons Foundation, Inc. - All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifndef __ITENSOR_INDEXSET_H
#define __ITENSOR_INDEXSET_H
#include <algorithm>
#include "itensor/util/safe_ptr.h"
#include "itensor/index.h"
#include "itensor/tensor/contract.h"
#include "itensor/tensor/range.h"
#include "itensor/tensor/types.h"
#include "itensor/tensor/permutation.h"
namespace itensor {
class IndexSet;
using IndexSetBuilder = RangeBuilderT<IndexSet>;
template<typename I>
class IndexSetIter;
//
// IndexSet
//
// When constructed from a collection of indices,
// (as an explicit set of arguments or via
// a container) puts the indices with m>1 to the
// front and those with m==1 at the back but otherwise
// keeps the indices in the order given.
//
void
checkQNConsistent(IndexSet const&);
class IndexSet : public RangeT<Index>
{
public:
using extent_type = index_type;
using range_type = RangeT<Index>;
using parent = RangeT<Index>;
using size_type = typename range_type::size_type;
using storage_type = typename range_type::storage_type;
using value_type = Index;
using iterator = IndexSetIter<Index>;
using const_iterator = IndexSetIter<const Index>;
public:
IndexSet() { }
// construct from 1 or more indices
template <typename... Inds>
explicit
IndexSet(Index const& i1,
Inds&&... inds)
: parent(i1,std::forward<Inds>(inds)...)
{
checkQNConsistent(*this);
}
IndexSet(std::initializer_list<Index> const& ii)
: parent(ii)
{
checkQNConsistent(*this);
}
IndexSet(std::vector<Index> const& ii)
: parent(ii)
{
checkQNConsistent(*this);
}
template<size_t N>
IndexSet(std::array<Index,N> const& ii)
: parent(ii)
{
checkQNConsistent(*this);
}
template<typename IndxContainer>
explicit
IndexSet(IndxContainer && ii)
: parent(std::forward<IndxContainer>(ii))
{
checkQNConsistent(*this);
}
explicit
IndexSet(storage_type && store)
: parent(std::move(store))
{
checkQNConsistent(*this);
}
// construct from 2 IndexSets
IndexSet(IndexSet const& is1,
IndexSet const& is2)
{
auto N1 = is1.order();
auto N2 = is2.order();
auto N = N1+N2;
auto inds = IndexSetBuilder(N);
for( auto n1 : range1(N1) )
inds.nextIndex(std::move(is1(n1)));
for( auto n2 : range1(N2) )
inds.nextIndex(std::move(is2(n2)));
*this = inds.build();
checkQNConsistent(*this);
}
// construct from an Index and IndexSet
IndexSet(Index const& i,
IndexSet const& is)
{
auto N = is.order();
auto inds = IndexSetBuilder(N+1);
inds.nextIndex(std::move(i));
for( auto n : range1(N) )
inds.nextIndex(std::move(is(n)));
*this = inds.build();
checkQNConsistent(*this);
}
// construct from an Index and IndexSet
IndexSet(IndexSet const& is,
Index const& i)
{
auto N = is.order();
auto inds = IndexSetBuilder(N+1);
for( auto n : range1(N) )
inds.nextIndex(std::move(is(n)));
inds.nextIndex(std::move(i));
*this = inds.build();
checkQNConsistent(*this);
}
explicit operator bool() const { return !parent::empty(); }
long
extent(size_type i) const { return parent::extent(i); }
size_type
stride(size_type i) const { return parent::stride(i); }
// Get the number of indices
long
order() const { return parent::order(); }
// Get the number of indices (alternative)
long
length() const { return this->order(); }
// 0-indexed access
Index &
operator[](size_type i)
{
#ifdef DEBUG
if(i >= parent::size()) throw ITError("IndexSet[i] arg out of range");
#endif
return parent::index(i);
}
// 1-indexed access
Index &
operator()(size_type I)
{
#ifdef DEBUG
if(I < 1 || I > parent::size()) throw ITError("IndexSet(i) arg out of range");
#endif
return operator[](I-1);
}
// Deprecated
Index &
index(size_type I) { return operator()(I); }
// 0-indexed access
Index const&
operator[](size_type i) const
{
#ifdef DEBUG
if(i >= parent::size()) throw ITError("IndexSet[i] arg out of range");
#endif
return parent::index(i);
}
// 1-indexed access
Index const&
operator()(size_type I) const
{
#ifdef DEBUG
if(I < 1 || I > parent::size()) throw ITError("IndexSet(i) arg out of range");
#endif
return operator[](I-1);
}
// Deprecated
Index const&
index(size_type I) const { return operator()(I); }
parent const&
range() const { return *this; }
IndexSet&
dag();
void
swap(IndexSet & other) { parent::swap(other); }
Index const&
front() const { return parent::front().ind; }
Index const&
back() const { return parent::back().ind; }
iterator
begin();
iterator
end();
const_iterator
begin() const;
const_iterator
end() const;
const_iterator
cbegin() const;
const_iterator
cend() const;
//
// Tag methods
//
IndexSet&
setTags(TagSet const& tsnew);
IndexSet&
setTags(TagSet const& tsnew,
IndexSet const& ismatch);
template<typename... VarArgs>
IndexSet&
setTags(TagSet const& tsnew,
Index const& imatch1,
VarArgs&&... vargs)
{
setTags(tsnew,IndexSet(imatch1,std::forward<VarArgs>(vargs)...));
return *this;
}
IndexSet&
setTags(TagSet const& tsnew,
TagSet const& tsmatch);
IndexSet&
noTags();
IndexSet&
noTags(IndexSet const& ismatch);
template<typename... VarArgs>
IndexSet&
noTags(Index const& imatch1,
VarArgs&&... vargs)
{
noTags(IndexSet(imatch1,std::forward<VarArgs>(vargs)...));
return *this;
}
IndexSet&
noTags(TagSet const& tsmatch);
IndexSet&
addTags(TagSet const& tsadd);
IndexSet&
addTags(TagSet const& tsadd,
IndexSet const& ismatch);
template<typename... VarArgs>
IndexSet&
addTags(TagSet const& tsadd,
Index const& imatch1,
VarArgs&&... vargs)
{
addTags(tsadd,IndexSet(imatch1,std::forward<VarArgs>(vargs)...));
return *this;
}
IndexSet&
addTags(TagSet const& tsadd,
TagSet const& tsmatch);
IndexSet&
removeTags(TagSet const& tsremove);
IndexSet&
removeTags(TagSet const& tsremove,
IndexSet const& ismatch);
template<typename... VarArgs>
IndexSet&
removeTags(TagSet const& tsremove,
Index const& imatch1,
VarArgs&&... vargs)
{
removeTags(tsremove,IndexSet(imatch1,std::forward<VarArgs>(vargs)...));
return *this;
}
IndexSet&
removeTags(TagSet const& tsremove,
TagSet const& tsmatch);
IndexSet&
replaceTags(TagSet const& tsold,
TagSet const& tsnew);
IndexSet&
replaceTags(TagSet const& tsold,
TagSet const& tsnew,
IndexSet const& ismatch);
template<typename... VarArgs>
IndexSet&
replaceTags(TagSet const& tsold,
TagSet const& tsnew,
Index const& imatch1,
VarArgs&&... vargs)
{
replaceTags(tsold,tsnew,IndexSet(imatch1,std::forward<VarArgs>(vargs)...));
return *this;
}
IndexSet&
replaceTags(TagSet const& tsold,
TagSet const& tsnew,
TagSet const& tsmatch);
template<typename... VarArgs>
IndexSet&
swapTags(TagSet const& ts1,
TagSet const& ts2,
VarArgs&&... vargs);
//
// Integer tag convenience functions
//
//
// Set the integer tag of indices to plnew
//
IndexSet&
setPrime(int plnew);
IndexSet&
setPrime(int plnew,
IndexSet const& ismatch);
template<typename... VarArgs>
IndexSet&
setPrime(int plnew,
Index const& imatch1,
VarArgs&&... vargs)
{
setPrime(plnew,IndexSet(imatch1,std::forward<VarArgs>(vargs)...));
return *this;
}
IndexSet&
setPrime(int plnew,
TagSet const& tsmatch);
template<typename... VarArgs>
IndexSet&
noPrime(VarArgs&&... vargs)
{
setPrime(0,std::forward<VarArgs>(vargs)...);
return *this;
}
//
// Increase the integer tag of indices by plinc
//
IndexSet&
prime(int plinc);
IndexSet&
prime()
{
prime(1);
return *this;
}
IndexSet&
prime(int plinc,
IndexSet const& ismatch);
IndexSet&
prime(IndexSet const& ismatch)
{
prime(1,ismatch);
return *this;
}
template<typename... VarArgs>
IndexSet&
prime(int plinc,
Index const& imatch1,
VarArgs&&... vargs)
{
prime(plinc,IndexSet(imatch1,std::forward<VarArgs>(vargs)...));
return *this;
}
template<typename... VarArgs>
IndexSet&
prime(Index const& imatch1,
VarArgs&&... vargs)
{
prime(IndexSet(imatch1,std::forward<VarArgs>(vargs)...));
return *this;
}
IndexSet&
prime(int plinc,
TagSet const& tsmatch);
IndexSet&
prime(TagSet const& tsmatch)
{
prime(1,tsmatch);
return *this;
}
// Remove QNs from all indices in the IndexSet
IndexSet&
removeQNs();
//
// Deprecated
//
long
r() const { return this->order(); }
void
prime(Index const& imatch,
int plinc)
{
Error("Error: .prime(Index,int) is no longer supported, use .prime(int,Index) instead.");
}
};
void
read(std::istream& s, IndexSet & is);
void
write(std::ostream& s, IndexSet const& is);
auto inline
rangeBegin(IndexSet const& is) -> decltype(is.range().begin())
{
return is.range().begin();
}
auto inline
rangeEnd(IndexSet const& is) -> decltype(is.range().end())
{
return is.range().end();
}
long
order(IndexSet const& is);
long
length(IndexSet const& is);
IndexSet
dag(IndexSet is);
template<typename... VarArgs>
IndexSet
setTags(IndexSet A,
VarArgs&&... vargs)
{
A.setTags(std::forward<VarArgs>(vargs)...);
return A;
}
template<typename... VarArgs>
IndexSet
noTags(IndexSet A,
VarArgs&&... vargs)
{
A.noTags(std::forward<VarArgs>(vargs)...);
return A;
}
template<typename... VarArgs>
IndexSet
addTags(IndexSet A,
VarArgs&&... vargs)
{
A.addTags(std::forward<VarArgs>(vargs)...);
return A;
}
template<typename... VarArgs>
IndexSet
removeTags(IndexSet A,
VarArgs&&... vargs)
{
A.removeTags(std::forward<VarArgs>(vargs)...);
return A;
}
template<typename... VarArgs>
IndexSet
replaceTags(IndexSet A,
VarArgs&&... vargs)
{
A.replaceTags(std::forward<VarArgs>(vargs)...);
return A;
}
template<typename... VarArgs>
IndexSet
swapTags(IndexSet A,
VarArgs&&... vargs)
{
A.swapTags(std::forward<VarArgs>(vargs)...);
return A;
}
template<typename... VarArgs>
IndexSet
prime(IndexSet A,
VarArgs&&... vargs)
{
A.prime(std::forward<VarArgs>(vargs)...);
return A;
}
template<typename... VarArgs>
IndexSet
setPrime(IndexSet A,
VarArgs&&... vargs)
{
A.setPrime(std::forward<VarArgs>(vargs)...);
return A;
}
template<typename... VarArgs>
IndexSet
noPrime(IndexSet A,
VarArgs&&... vargs)
{
A.noPrime(std::forward<VarArgs>(vargs)...);
return A;
}
//Replace all indices with 'similar' indices
//with the same properties but which don't compare equal
//to the indices they replace (using sim(Index) function)
IndexSet
sim(IndexSet is);
IndexSet
sim(IndexSet is,
IndexSet const& ismatch);
IndexSet
sim(IndexSet is,
TagSet const& tsmatch);
//
// IndexSet helper methods
//
//
// Given IndexSet iset and Index I,
// return int j such that iset[j] == I.
// If not found, returns -1
//
int
indexPosition(IndexSet const& is,
Index const& imatch);
std::vector<int>
indexPositions(IndexSet const& is,
IndexSet const& ismatch);
Arrow
dir(IndexSet const& is, Index const& I);
// Return true if the Index `imatch` is in
// `is`
bool
hasIndex(IndexSet const& is,
Index const& imatch);
// Return true if all indices of `ismatch`
// are in `is`
bool
hasInds(IndexSet const& is,
IndexSet const& ismatch);
// Return true if IndexSet `is1` and `is2` have
// the same indices
bool
hasSameInds(IndexSet const& is1,
IndexSet const& is2);
// IndexSets are equal if they are the
// same size and contain equal indices
// in equal ordering (i.e. equals({i,j},{i,j}) -> true)
// but equals({i,j},{j,i}) -> false).
// For set equality, you can use hasSameInds(is1,is2).
bool
equals(IndexSet const& is1,
IndexSet const& is2);
long
minDim(IndexSet const& iset);
long
maxDim(IndexSet const& iset);
void
contractIS(IndexSet const& Lis,
IndexSet const& Ris,
IndexSet & Nis,
bool sortResult = false);
template<class LabelT>
void
contractIS(IndexSet const& Lis,
LabelT const& Lind,
IndexSet const& Ris,
LabelT const& Rind,
IndexSet & Nis,
LabelT & Nind,
bool sortResult = false);
template<class LabelT>
void
contractISReplaceIndex(IndexSet const& Lis,
LabelT const& Lind,
IndexSet const& Ris,
LabelT const& Rind,
IndexSet & Nis);
template<class LabelT>
void
ncprod(IndexSet const& Lis,
LabelT const& Lind,
IndexSet const& Ris,
LabelT const& Rind,
IndexSet & Nis,
LabelT & Nind);
std::ostream&
operator<<(std::ostream& s, IndexSet const& is);
template<typename index_type_>
class IndexSetIter
{
public:
using index_type = stdx::remove_const_t<index_type_>;
using value_type = index_type;
using reference = index_type_&;
using difference_type = std::ptrdiff_t;
using pointer = index_type_*;
using iterator_category = std::random_access_iterator_tag;
using indexset_type = stdx::conditional_t<std::is_const<index_type_>::value,
const IndexSet,
IndexSet>;
using range_ptr = typename RangeT<index_type>::value_type*;
using const_range_ptr = const typename RangeT<index_type>::value_type*;
using data_ptr = stdx::conditional_t<std::is_const<index_type_>::value,
const_range_ptr,
range_ptr>;
private:
size_t off_ = 0;
indexset_type* p_;
public:
IndexSetIter() : p_(nullptr) { }
explicit
IndexSetIter(indexset_type & is) : p_(&is) { }
size_t
offset() const { return off_; }
IndexSetIter&
operator++()
{
++off_;
return *this;
}
IndexSetIter
operator++(int)
{
auto tmp = *this; //save copy of this
++off_;
return tmp;
}
IndexSetIter&
operator+=(difference_type x)
{
off_ += x;
return *this;
}
IndexSetIter&
operator--( )
{
--off_;
return *this;
}
IndexSetIter
operator--(int)
{
auto tmp = *this; //save copy of this
--off_;
return tmp;
}
IndexSetIter&
operator-=(difference_type x)
{
off_ -= x;
return *this;
}
reference
operator[](difference_type n) { return p_->operator[](n); }
reference
operator*() { return p_->operator[](off_); }
pointer
operator->() { return &(p_->operator[](off_)); }
IndexSetIter static
makeEnd(indexset_type & is)
{
IndexSetIter end;
end.p_ = &is;
end.off_ = is.size();
return end;
}
};
template <typename T>
bool
operator==(const IndexSetIter<T>& x, const IndexSetIter<T>& y)
{
return x.offset() == y.offset();
}
template <typename T>
bool
operator!=(const IndexSetIter<T>& x, const IndexSetIter<T>& y)
{
return x.offset() != y.offset();
}
template <typename T>
bool
operator<(const IndexSetIter<T>& x, const IndexSetIter<T>& y)
{
return x.offset() < y.offset();
}
template <typename T>
IndexSetIter<T>
operator+(IndexSetIter<T> x,
typename IndexSetIter<T>::difference_type d)
{
return x += d;
}
template <typename T>
IndexSetIter<T>
operator+(typename IndexSetIter<T>::difference_type d,
IndexSetIter<T> x)
{
return x += d;
}
bool
hasQNs(IndexSet const& is);
void
checkIndexSet(IndexSet const& is);
void
checkIndexPositions(std::vector<int> const& is);
//
// IndexSet set operations
//
// Find the Indices containing tags in the specified TagSet
IndexSet
findInds(IndexSet const& is,
TagSet const& tsmatch);
// Convert an order one IndexSet into an Index
// Throws an error if more than one Index is in the IndexSet
// If no indices are found, returns a null Index
Index
findIndex(IndexSet const& is);
// Find the Index containing tags in the specified TagSet
// Throws an error if more than one Index is found
// If no indices are found, returns a null Index
Index
findIndex(IndexSet const& is,
TagSet const& tsmatch);
// Find the Indices not containing tags in the specified TagSet
// Same as uniqueInds(is,findInds(is,tsmatch))
IndexSet
findIndsExcept(IndexSet const& is,
TagSet const& tsmatch);
// Intersection of two IndexSets
IndexSet
commonInds(IndexSet const& is1,
IndexSet const& is2);
// Union of two IndexSets (is1+is2)
// Preserves the ordering of the original
// IndexSets
IndexSet
unionInds(IndexSet const& is1,
IndexSet const& is2);
IndexSet
unionInds(Index const& i,
IndexSet const& is);
IndexSet
unionInds(IndexSet const& is,
Index const& i);
IndexSet
unionInds(std::vector<IndexSet> const& is1);
// Difference of two IndexSets (is1-is2)
IndexSet
uniqueInds(IndexSet const& is1,
IndexSet const& is2);
// Difference of IndexSet from a set
// of other IndexSets (is1-(is2+is3+...))
IndexSet
uniqueInds(IndexSet const& is1,
std::vector<IndexSet> const& is2);
// Symmetric difference of two IndexSets
// (union(is1-is2,is2-is1))
IndexSet
noncommonInds(IndexSet const& is1,
IndexSet const& is2);
} //namespace itensor
#include "itensor/indexset_impl.h"
#endif