xerus
a general purpose tensor library
index.h
Go to the documentation of this file.
1 // Xerus - A General Purpose Tensor Library
2 // Copyright (C) 2014-2017 Benjamin Huber and Sebastian Wolf.
3 //
4 // Xerus is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published
6 // by the Free Software Foundation, either version 3 of the License,
7 // or (at your option) any later version.
8 //
9 // Xerus is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with Xerus. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // For further information on Xerus visit https://libXerus.org
18 // or contact us at contact@libXerus.org.
19 
25 #pragma once
26 
27 #include "basic.h"
28 
29 #include <atomic>
30 #include <bitset>
31 #include <vector>
32 
33 namespace xerus {
34 
43  class Index {
44  public:
45  #ifndef XERUS_DISABLE_RUNTIME_CHECKS
48  #else
49  enum Flag {FIXED, INVERSE_SPAN, FRACTIONAL_SPAN, OPEN, NUM_FLAGS};
51  #endif
52 
53  private:
55  static std::atomic<uint64> idThreadInitCounter;
56 
58  static thread_local uint64 idCounter;
59 
60  public:
63 
65  size_t span = 1;
66 
68  size_t assingedDimension = 0;
69 
71  std::bitset<NUM_FLAGS> flags;
72 
73 
74 
76  Index();
77 
79  Index(const Index& _other) noexcept = default;
80 
82  Index(const int32 _i);
83 
85  Index(const uint32 _i) noexcept;
86 
88  Index(const int64 _i);
89 
91  Index(const uint64 _i) noexcept;
92 
94  explicit Index(const uint64 _valueId, const size_t _span) noexcept;
95 
97  explicit Index(const uint64 _valueId, const size_t _span, const Flag _flag1) noexcept;
98 
99 
100 
102  Index& operator=(const Index&) = default;
103 
105  void set_span(const size_t _degree);
106 
108  size_t actual_span(const size_t _degree) const;
109 
111  bool fixed() const;
112 
114  bool open() const;
115 
119  void open(const bool _open);
120 
123  size_t fixed_position() const;
124 
125 
126 
128  size_t dimension() const;
129 
134  Index operator^(const size_t _span) const;
135 
141  Index operator&(const size_t _span) const;
142 
148  Index operator/(const size_t _span) const;
149 
155  static bool all_open(const std::vector<Index>& _indices);
156  };
157 
159  bool operator==(const Index& _a, const Index& _b);
160 
162  bool operator!=(const Index& _a, const Index& _b);
163 
165  bool operator<(const Index& _a, const Index& _b);
166 
168  std::ostream& operator<<(std::ostream& _out, const xerus::Index& _idx);
169 }
static bool all_open(const std::vector< Index > &_indices)
: Checks whether all indices in _indices are open. This is naturally only usefull for assinged indice...
Definition: index.cpp:153
size_t actual_span(const size_t _degree) const
Returns the span this index actually represents in a tensor of given order.
Definition: index.cpp:80
uint64_t uint64
Definition: standard.h:63
Flag
Enum defining the possible flags an Index my possess.
Definition: index.h:47
bool open() const
Checks whether the index is open.
Definition: index.cpp:111
The main namespace of xerus.
Definition: basic.h:37
Index operator^(const size_t _span) const
: Allow the creation of Indices covering more than one dimension using the power operator. E.g. A(i^2) = B(i^2) + C(i^2), defines A as the entriewise sum of the matrices B and C.
Definition: index.cpp:135
size_t fixed_position() const
: Returns the fixed position of a fixed index.
Definition: index.cpp:129
std::ostream & operator<<(std::ostream &_out, const xerus::Index &_idx)
Allows to pretty print indices, giving the valueId and span.
Definition: index.cpp:175
int64_t int64
Definition: standard.h:58
bool operator==(const Index &_a, const Index &_b)
Two Indices are equal if their valueId coincides. Fixed indices are never equal.
Definition: index.cpp:163
std::bitset< NUM_FLAGS > flags
Bitset of all possible flags the index may possess.
Definition: index.h:71
Index & operator=(const Index &)=default
Indices are default assignable.
void set_span(const size_t _degree)
Returns the span this index actually represents in a tensor of given order.
Definition: index.cpp:64
uint64 valueId
Unqiue ID of the index. In case the fixed flag is set, this is the fixed position.
Definition: index.h:62
size_t assingedDimension
The product of the external dimensions this index correstponds to. Only set for assinged indices...
Definition: index.h:68
bool operator<(const Index &_a, const Index &_b)
The Comparision operator is needed for indices to be orderable in std::set, the valueId is used...
Definition: index.cpp:171
uint32_t uint32
Definition: standard.h:62
Index operator/(const size_t _span) const
: Allow the creation of Indices covering an x-th fraction of the indices. E.g. A(i&0) = B(i/2...
Definition: index.cpp:147
bool operator!=(const Index &_a, const Index &_b)
Two Indices are equal if their valueId coincides. Fixed indices are never equal.
Definition: index.cpp:167
Header file for shorthand notations that are xerus specific but used throughout the library...
Class used to represent indices that can be used to write tensor calculations in index notation...
Definition: index.h:43
Index operator &(const size_t _span) const
: Allow the creation of Indices covering all but x dimensions using the and operator. E.g. A() = B(i&0) * C(i&0), defines A as the full contraction between B and C, indifferent of the actual degree of B and C.
size_t dimension() const
Returns the (mult)Dimension assinged to this index.
Definition: index.cpp:123
size_t span
The span states how many dimensions are covered by the index.
Definition: index.h:65
bool fixed() const
Checks whether the Index represents a fixed number.
Definition: index.cpp:96
int32_t int32
Definition: standard.h:57
Index()
Empty constructor that creates a new Index with new ID. Use this to create indices.
Definition: index.cpp:37