TBTK
Need a break? Support the development by playing Polarity Puzzles
PropertyConverter.h
Go to the documentation of this file.
1 /* Copyright 2019 Kristofer Björnson
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
24 #ifndef COM_DAFER45_TBTK_PROPERTY_CONVERTER
25 #define COM_DAFER45_TBTK_PROPERTY_CONVERTER
26 
27 #include "TBTK/AnnotatedArray.h"
28 #include "TBTK/Property/AbstractProperty.h"
29 #include "TBTK/TBTKMacros.h"
30 
31 namespace TBTK{
32 
72 public:
78  template<typename DataType>
80  const Property::AbstractProperty<DataType> &abstractProperty
81  );
82 
106  template<typename DataType>
108  const Property::AbstractProperty<DataType> &abstractProperty,
109  const Index &pattern
110  );
111 };
112 
113 template<typename DataType>
115  const Property::AbstractProperty<DataType> &abstractProperty
116 ){
118  = abstractProperty.getIndexDescriptor().getFormat();
119 
120  switch(format){
121  case IndexDescriptor::Format::None:
122  {
123  Array<DataType> array({abstractProperty.getBlockSize()});
124  std::vector<std::vector<Subindex>> axis(1);
125  for(unsigned int n = 0; n < abstractProperty.getBlockSize(); n++){
126  array[{n}] = abstractProperty(n);
127  axis[0].push_back(n);
128  }
129  return AnnotatedArray<DataType, Subindex>(array, axis);
130  }
131  case IndexDescriptor::Format::Ranges:
132  {
133  std::vector<int> ranges
134  = abstractProperty.getIndexDescriptor().getRanges();
135  if(abstractProperty.getBlockSize() != 1)
136  ranges.push_back(abstractProperty.getBlockSize());
137  std::vector<unsigned int> rangesUnsignedInt;
138  for(unsigned int n = 0; n < ranges.size(); n++)
139  rangesUnsignedInt.push_back(ranges[n]);
140 
141  Array<DataType> array = Array<DataType>::create(rangesUnsignedInt);
142  const std::vector<DataType> &data = abstractProperty.getData();
143  for(unsigned int n = 0; n < abstractProperty.getSize(); n++)
144  array[n] = data[n];
145 
146  std::vector<std::vector<Subindex>> axes(ranges.size());
147  for(unsigned int n = 0; n < ranges.size(); n++)
148  for(unsigned int c = 0; c < (unsigned int)ranges[n]; c++)
149  axes[n].push_back(c);
150 
151  return AnnotatedArray<DataType, Subindex>(array, axes);
152  }
153  default:
154  TBTKExit(
155  "PropertyConverter::convert()",
156  "Unsupported format. Only IndexDescriptor::Format::None and"
157  << " IndexDescriptor::Format::Ranges supported.",
158  ""
159  );
160  }
161 }
162 
163 template<typename DataType>
165  const Property::AbstractProperty<DataType> &abstractProperty,
166  const Index &pattern
167 ){
169  = abstractProperty.getIndexDescriptor().getFormat();
170 
171  switch(format){
172  case IndexDescriptor::Format::Custom:
173  {
174  std::vector<unsigned int> wildcardPositions;
175  for(unsigned int n = 0; n < pattern.getSize(); n++)
176  if(pattern[n].isWildcard())
177  wildcardPositions.push_back(n);
178 
179  const IndexTree &indexTree
180  = abstractProperty.getIndexDescriptor().getIndexTree();
181  std::vector<Index> indexList = indexTree.getIndexList(pattern);
182 
183  std::vector<Subindex> minSubindices;
184  std::vector<Subindex> maxSubindices;
185  for(unsigned int n = 0; n < wildcardPositions.size(); n++){
186  Subindex min = indexList[0][wildcardPositions[n]];
187  Subindex max = indexList[0][wildcardPositions[n]];
188  for(unsigned int c = 1; c < indexList.size(); c++){
189  Subindex subindex
190  = indexList[c][wildcardPositions[n]];
191  if(min > subindex)
192  min = subindex;
193  if(max < subindex)
194  max = subindex;
195  }
196  minSubindices.push_back(min);
197  maxSubindices.push_back(max);
198  }
199  if(abstractProperty.getBlockSize() > 1){
200  minSubindices.push_back(0);
201  maxSubindices.push_back(
202  abstractProperty.getBlockSize() - 1
203  );
204  }
205 
206  std::vector<unsigned int> ranges;
207  for(unsigned int n = 0; n < minSubindices.size(); n++){
208  ranges.push_back(
209  maxSubindices[n] - minSubindices[n] + 1
210  );
211  }
213  ranges,
214  DataType(0)
215  );
216  for(unsigned int n = 0; n < indexList.size(); n++){
217  const Index index = indexList[n];
218  std::vector<unsigned int> arrayIndices;
219  for(unsigned int c = 0; c < wildcardPositions.size(); c++){
220  arrayIndices.push_back(
221  index[wildcardPositions[c]]
222  - minSubindices[c]
223  );
224  }
225  if(abstractProperty.getBlockSize() > 1){
226  for(
227  unsigned int c = 0;
228  c < abstractProperty.getBlockSize();
229  c++
230  ){
231  std::vector<
232  unsigned int
233  > blockExtendedArrayIndices
234  = arrayIndices;
235  blockExtendedArrayIndices.push_back(c);
236 
237  array[blockExtendedArrayIndices]
238  = abstractProperty(index, c);
239  }
240  }
241  else{
242  array[arrayIndices] = abstractProperty(index);
243  }
244  }
245 
246  std::vector<std::vector<Subindex>> axes(ranges.size());
247  for(
248  unsigned int n = 0;
249  n < ranges.size();
250  n++
251  ){
252  for(
253  unsigned int c = 0;
254  c < (unsigned int)ranges[n];
255  c++
256  ){
257  axes[n].push_back(minSubindices[n] + c);
258  }
259  }
260 
261  return AnnotatedArray<DataType, Subindex>(array, axes);
262  }
263  default:
264  TBTKExit(
265  "PropertyConverter::convert()",
266  "Unsupported format. Only IndexDescriptor::Format::None and"
267  << " IndexDescriptor::Format::Ranges supported.",
268  ""
269  );
270  }
271 }
272 
273 }; //End of namesapce TBTK
274 
275 #endif
TBTK::Array
Multi-dimensional array.
Definition: Array.h:98
TBTK::AnnotatedArray
Array with additional information about its axes.
Definition: AnnotatedArray.h:41
TBTK::IndexTree::getIndexList
std::vector< Index > getIndexList(const Index &pattern) const
TBTK::Property::AbstractProperty::getIndexDescriptor
const IndexDescriptor & getIndexDescriptor() const
Definition: AbstractProperty.h:597
TBTK::Property::AbstractProperty::getData
const std::vector< DataType > & getData() const
Definition: AbstractProperty.h:567
TBTK::Property::AbstractProperty::getSize
unsigned int getSize() const
Definition: AbstractProperty.h:560
AnnotatedArray.h
Array with additional information about its axes.
TBTK::IndexDescriptor::Format
Format
Definition: IndexDescriptor.h:42
TBTK::Property::AbstractProperty::getBlockSize
unsigned int getBlockSize() const
Definition: AbstractProperty.h:555
TBTK::IndexTree
Data structure for mapping physical indices to linear indices.
Definition: IndexTree.h:35
TBTK::Subindex
An entry in an Index.
Definition: Subindex.h:91
TBTK::Property::AbstractProperty
Abstract Property class.
Definition: AbstractProperty.h:101
TBTKMacros.h
Precompiler macros.
TBTK::Index::getSize
unsigned int getSize() const
Definition: Index.h:482
TBTK::PropertyConverter::convert
static AnnotatedArray< DataType, Subindex > convert(const Property::AbstractProperty< DataType > &abstractProperty)
Definition: PropertyConverter.h:114
TBTK::Array::create
static Array create(const std::vector< unsigned int > &ranges)
Definition: Array.h:585
TBTK::Index
Physical index.
Definition: Index.h:44
TBTK::PropertyConverter
Convert Properties to AnnotatedArrays.
Definition: PropertyConverter.h:71