sisi4s
Loading...
Searching...
No Matches
SlicedCtfTensor.hpp
Go to the documentation of this file.
1#ifndef SLICED_CTF_TENSOR_DEFINED
2#define SLICED_CTF_TENSOR_DEFINED
3
5#include <util/Tensor.hpp>
6#include <initializer_list>
7#include <vector>
8#include <string>
9
10namespace sisi4s {
11template <typename F = double>
13public:
14 SlicedCtfTensor(Tensor<F> &T, const std::initializer_list<int> &list) {
15 create(T, std::vector<int>(list));
16 }
17 template <typename Iterator>
18 SlicedCtfTensor(Tensor<F> &T, Iterator begin, Iterator end) {
19 create(T, std::vector<int>(begin, end));
20 }
21 void create(Tensor<F> &T, const std::vector<int> &slicedDims) {
22 // generate indexNames = "abcd..."
23 std::string indexNames(T.order, 'a');
24 for (int d(0); d < T.order; ++d) indexNames[d] += d;
25 // figure out how many slices we have in each sliced dimension
26 slicedLens.resize(slicedDims.size());
27 int64_t slicesCount = 1;
28 for (unsigned int d(0); d < slicedDims.size(); ++d) {
29 slicesCount *= slicedLens[d] = T.lens[slicedDims[d]];
30 }
31 // do all slices
32 slices.resize(slicesCount);
33 for (int64_t sliceIndex(0); sliceIndex < slicesCount; ++sliceIndex) {
34 // get slice position from its index number
35 int64_t i(sliceIndex);
36 std::vector<int> slicePosition(slicedDims.size());
37 for (unsigned int d(0); d < slicedDims.size(); ++d) {
38 slicePosition[d] = i % slicedLens[d];
39 i /= slicedLens[d];
40 }
41 // start = (0,...,0), end = (lens[0],...,lend[order-1])
42 std::vector<int> start(T.order), end(T.lens, T.lens + T.order);
43 // now enter the slice position in the sliced dimensions
44 for (unsigned int d(0); d < slicedDims.size(); ++d) {
45 start[slicedDims[d]] = slicePosition[d];
46 end[slicedDims[d]] = slicePosition[d] + 1;
47 }
48 slices[sliceIndex] = new Tensor<F>(T.slice(start.data(), end.data()));
49 }
50 }
52 for (uint64_t i(0); i < slices.size(); ++i) {
53 if (slices[i]) delete slices[i];
54 slices[i] = nullptr;
55 }
56 }
57
58 Tensor<F> &operator()(const std::initializer_list<int> &slicePos) {
59 return (*this)(std::vector<int>(slicePos));
60 }
61 template <typename Iterator>
62 Tensor<F> &operator()(Iterator begin, Iterator end) {
63 return (*this)(std::vector<int>(begin, end));
64 }
65 Tensor<F> &operator()(const std::vector<int> &slicePosition) {
66 // get slice number from its position
67 int64_t sliceIndex(0);
68 for (int d(slicedLens.size() - 1); d >= 0; --d) {
69 sliceIndex *= slicedLens[d];
70 sliceIndex += slicePosition[d];
71 }
72 return *slices[sliceIndex];
73 }
74
75 std::vector<int> slicedLens;
76 std::vector<Tensor<F> *> slices;
77};
78} // namespace sisi4s
79
80#endif
Definition SlicedCtfTensor.hpp:12
std::vector< Tensor< F > * > slices
Definition SlicedCtfTensor.hpp:76
Tensor< F > & operator()(Iterator begin, Iterator end)
Definition SlicedCtfTensor.hpp:62
Tensor< F > & operator()(const std::vector< int > &slicePosition)
Definition SlicedCtfTensor.hpp:65
SlicedCtfTensor(Tensor< F > &T, const std::initializer_list< int > &list)
Definition SlicedCtfTensor.hpp:14
~SlicedCtfTensor()
Definition SlicedCtfTensor.hpp:51
SlicedCtfTensor(Tensor< F > &T, Iterator begin, Iterator end)
Definition SlicedCtfTensor.hpp:18
std::vector< int > slicedLens
Definition SlicedCtfTensor.hpp:75
Tensor< F > & operator()(const std::initializer_list< int > &slicePos)
Definition SlicedCtfTensor.hpp:58
void create(Tensor< F > &T, const std::vector< int > &slicedDims)
Definition SlicedCtfTensor.hpp:21
Definition Algorithm.hpp:10
CTF::Tensor< F > Tensor
Definition Tensor.hpp:9