Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RTensor.hxx
Go to the documentation of this file.
1#ifndef TMVA_RTENSOR
2#define TMVA_RTENSOR
3
4#include <vector>
5#include <cstddef> // std::size_t
6#include <stdexcept> // std::runtime_error
7#include <sstream> // std::stringstream
8#include <memory> // std::shared_ptr
9#include <type_traits> // std::is_convertible
10#include <algorithm> // std::reverse
11#include <iterator> // std::random_access_iterator_tag
12
13namespace TMVA {
14namespace Experimental {
15
16/// Memory layout type
17enum class MemoryLayout : uint8_t {
18 RowMajor = 0x01,
19 ColumnMajor = 0x02
20};
21
22namespace Internal {
23
24/// \brief Get size of tensor from shape vector
25/// \param[in] shape Shape vector
26/// \return Size of contiguous memory
27template <typename T>
28inline std::size_t GetSizeFromShape(const T &shape)
29{
30 if (shape.size() == 0)
31 return 0;
32 std::size_t size = 1;
33 for (auto &s : shape)
34 size *= s;
35 return size;
36}
37
38/// \brief Compute strides from shape vector.
39/// \param[in] shape Shape vector
40/// \param[in] layout Memory layout
41/// \return Size of contiguous memory
42///
43/// This information is needed for the multi-dimensional indexing. See here:
44/// https://en.wikipedia.org/wiki/Row-_and_column-major_order
45/// https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.strides.html
46template <typename T>
47inline std::vector<std::size_t> ComputeStridesFromShape(const T &shape, MemoryLayout layout)
48{
49 const auto size = shape.size();
50 T strides(size);
51 if (layout == MemoryLayout::RowMajor) {
52 for (std::size_t i = 0; i < size; i++) {
53 if (i == 0) {
54 strides[size - 1 - i] = 1;
55 } else {
56 strides[size - 1 - i] = strides[size - 1 - i + 1] * shape[size - 1 - i + 1];
57 }
58 }
59 } else if (layout == MemoryLayout::ColumnMajor) {
60 for (std::size_t i = 0; i < size; i++) {
61 if (i == 0) {
62 strides[i] = 1;
63 } else {
64 strides[i] = strides[i - 1] * shape[i - 1];
65 }
66 }
67 } else {
68 std::stringstream ss;
69 ss << "Memory layout type is not valid for calculating strides.";
70 throw std::runtime_error(ss.str());
71 }
72 return strides;
73}
74
75/// \brief Compute indices from global index
76/// \param[in] shape Shape vector
77/// \param[in] idx Global index
78/// \param[in] layout Memory layout
79/// \return Indice vector
80template <typename T>
81inline T ComputeIndicesFromGlobalIndex(const T& shape, MemoryLayout layout, const typename T::value_type idx)
82{
83 const auto size = shape.size();
84 auto strides = ComputeStridesFromShape(shape, layout);
85 T indices(size);
86 auto r = idx;
87 for (std::size_t i = 0; i < size; i++) {
88 indices[i] = int(r / strides[i]);
89 r = r % strides[i];
90 }
91 return indices;
92}
93
94/// \brief Compute global index from indices
95/// \param[in] strides Strides vector
96/// \param[in] idx Indice vector
97/// \return Global index
98template <typename U, typename V>
99inline std::size_t ComputeGlobalIndex(const U& strides, const V& idx)
100{
101 std::size_t globalIndex = 0;
102 const auto size = idx.size();
103 for (std::size_t i = 0; i < size; i++) {
104 globalIndex += strides[size - 1 - i] * idx[size - 1 - i];
105 }
106 return globalIndex;
107}
108
109/// \brief Type checking for all types of a parameter pack, e.g., used in combination with std::is_convertible
110template <class... Ts>
111struct and_types : std::true_type {
112};
113
114template <class T0, class... Ts>
115struct and_types<T0, Ts...> : std::integral_constant<bool, T0() && and_types<Ts...>()> {
116};
117
118/// \brief Copy slice of a tensor recursively from here to there
119/// \param[in] here Source tensor
120/// \param[in] there Target tensor (slice of source tensor)
121/// \param[in] mins Minimum of indices for each dimension
122/// \param[in] maxs Maximum of indices for each dimension
123/// \param[in] idx Current indices
124/// \param[in] active Active index needed to stop the recursion
125///
126/// Copy the content of a slice of a tensor from source to target. This is done
127/// by recursively iterating over the ranges of the slice for each dimension.
128template <typename T>
129void RecursiveCopy(const T &here, T &there,
130 const std::vector<std::size_t> &mins, const std::vector<std::size_t> &maxs,
131 std::vector<std::size_t> idx, std::size_t active)
132{
133 const auto size = idx.size();
134 for (std::size_t i = mins[active]; i < maxs[active]; i++) {
135 idx[active] = i;
136 if (active == size - 1) {
137 auto idxThere = idx;
138 for (std::size_t j = 0; j < size; j++) {
139 idxThere[j] -= mins[j];
140 }
141 there(idxThere) = here(idx);
142 } else {
143 Internal::RecursiveCopy(here, there, mins, maxs, idx, active + 1);
144 }
145 }
146}
147
148} // namespace TMVA::Experimental::Internal
149
150/// \class TMVA::Experimental::RTensor
151/// \brief RTensor is a container with contiguous memory and shape information.
152/// \tparam T Data-type of the tensor
153///
154/// An RTensor is a vector-like container, which has additional shape information.
155/// The elements of the multi-dimensional container can be accessed by their
156/// indices in a coherent way without taking care about the one-dimensional memory
157/// layout of the contiguous storage. This also allows to manipulate the shape
158/// of the container without moving the actual elements in memory. Another feature
159/// is that an RTensor can own the underlying contiguous memory but can also represent
160/// only a view on existing data without owning it.
161template <typename V, typename C = std::vector<V>>
162class RTensor {
163public:
164 // Typedefs
165 using Value_t = V;
166 using Shape_t = std::vector<std::size_t>;
168 using Slice_t = std::vector<Shape_t>;
169 using Container_t = C;
170
171private:
174 std::size_t fSize;
177 std::shared_ptr<Container_t> fContainer;
178
179protected:
180 void ReshapeInplace(const Shape_t &shape);
181
182public:
183 // Constructors
184
185 /// \brief Construct a tensor as view on data
186 /// \param[in] data Pointer to data contiguous in memory
187 /// \param[in] shape Shape vector
188 /// \param[in] layout Memory layout
189 RTensor(Value_t *data, Shape_t shape, MemoryLayout layout = MemoryLayout::RowMajor)
190 : fShape(shape), fLayout(layout), fData(data), fContainer(nullptr)
191 {
194 }
195
196 /// \brief Construct a tensor as view on data
197 /// \param[in] data Pointer to data contiguous in memory
198 /// \param[in] shape Shape vector
199 /// \param[in] strides Strides vector
200 /// \param[in] layout Memory layout
201 RTensor(Value_t *data, Shape_t shape, Shape_t strides, MemoryLayout layout = MemoryLayout::RowMajor)
202 : fShape(shape), fStrides(strides), fLayout(layout), fData(data), fContainer(nullptr)
203 {
205 }
206
207 /// \brief Construct a tensor owning externally provided data
208 /// \param[in] container Shared pointer to data container
209 /// \param[in] shape Shape vector
210 /// \param[in] layout Memory layout
211 RTensor(std::shared_ptr<Container_t> container, Shape_t shape,
212 MemoryLayout layout = MemoryLayout::RowMajor)
213 : fShape(shape), fLayout(layout), fContainer(container)
214 {
217 fData = &(*container->begin());
218 }
219
220 /// \brief Construct a tensor owning data initialized with new container
221 /// \param[in] shape Shape vector
222 /// \param[in] layout Memory layout
223 RTensor(Shape_t shape, MemoryLayout layout = MemoryLayout::RowMajor)
224 : fShape(shape), fLayout(layout)
225 {
226 // TODO: Document how data pointer is determined using STL iterator interface.
227 // TODO: Sanitize given container type with type traits
230 fContainer = std::make_shared<Container_t>(fSize);
231 fData = &(*fContainer->begin());
232 }
233
234 // Access elements
236 const Value_t &operator() (const Index_t &idx) const;
237 template <typename... Idx> Value_t &operator()(Idx... idx);
238 template <typename... Idx> const Value_t &operator() (Idx... idx) const;
239
240 // Access properties
241 std::size_t GetSize() const { return fSize; }
242 const Shape_t &GetShape() const { return fShape; }
243 const Shape_t &GetStrides() const { return fStrides; }
244 Value_t *GetData() { return fData; }
245 const Value_t *GetData() const { return fData; }
246 std::shared_ptr<Container_t> GetContainer() { return fContainer; }
247 const std::shared_ptr<Container_t> GetContainer() const { return fContainer; }
249 bool IsView() const { return fContainer == nullptr; }
250 bool IsOwner() const { return !IsView(); }
251
252 // Copy
253 RTensor<Value_t, Container_t> Copy(MemoryLayout layout = MemoryLayout::RowMajor) const;
254
255 // Transformations
261
262 // Iterator class
263 class Iterator {
264 private:
266 Index_t::value_type fGlobalIndex;
267 public:
268 using iterator_category = std::random_access_iterator_tag;
270 using difference_type = std::ptrdiff_t;
271 using pointer = Value_t *;
273
274 Iterator(RTensor<Value_t, Container_t>& x, typename Index_t::value_type idx) : fTensor(x), fGlobalIndex(idx) {}
275 Iterator& operator++() { fGlobalIndex++; return *this; }
276 Iterator operator++(int) { auto tmp = *this; operator++(); return tmp; }
277 Iterator& operator--() { fGlobalIndex--; return *this; }
278 Iterator operator--(int) { auto tmp = *this; operator--(); return tmp; }
282 Iterator& operator+=(difference_type rhs) { fGlobalIndex += rhs; return *this; }
283 Iterator& operator-=(difference_type rhs) { fGlobalIndex -= rhs; return *this; }
285 {
287 return fTensor(idx);
288 }
289 bool operator==(const Iterator& rhs) const
290 {
291 if (fGlobalIndex == rhs.GetGlobalIndex()) return true;
292 return false;
293 }
294 bool operator!=(const Iterator& rhs) const { return !operator==(rhs); };
295 bool operator>(const Iterator& rhs) const { return fGlobalIndex > rhs.GetGlobalIndex(); }
296 bool operator<(const Iterator& rhs) const { return fGlobalIndex < rhs.GetGlobalIndex(); }
297 bool operator>=(const Iterator& rhs) const { return fGlobalIndex >= rhs.GetGlobalIndex(); }
298 bool operator<=(const Iterator& rhs) const { return fGlobalIndex <= rhs.GetGlobalIndex(); }
299 typename Index_t::value_type GetGlobalIndex() const { return fGlobalIndex; };
300 };
301
302 // Iterator interface
303 // TODO: Document that the iterator always iterates following the physical memory layout.
304 Iterator begin() noexcept {
305 return Iterator(*this, 0);
306 }
307 Iterator end() noexcept {
308 return Iterator(*this, fSize);
309 }
310};
311
312/// \brief Reshape tensor in place
313/// \param[in] shape Shape vector
314/// Reshape tensor without changing the overall size
315template <typename Value_t, typename Container_t>
317{
318 const auto size = Internal::GetSizeFromShape(shape);
319 if (size != fSize) {
320 std::stringstream ss;
321 ss << "Cannot reshape tensor with size " << fSize << " into shape { ";
322 for (std::size_t i = 0; i < shape.size(); i++) {
323 if (i != shape.size() - 1) {
324 ss << shape[i] << ", ";
325 } else {
326 ss << shape[i] << " }.";
327 }
328 }
329 throw std::runtime_error(ss.str());
330 }
331
332 // Compute new strides from shape
333 auto strides = Internal::ComputeStridesFromShape(shape, fLayout);
334 fShape = shape;
335 fStrides = strides;
336}
337
338
339/// \brief Access elements
340/// \param[in] idx Index vector
341/// \return Reference to element
342template <typename Value_t, typename Container_t>
344{
345 const auto globalIndex = Internal::ComputeGlobalIndex(fStrides, idx);
346 return fData[globalIndex];
347}
348
349/// \brief Access elements
350/// \param[in] idx Index vector
351/// \return Reference to element
352template <typename Value_t, typename Container_t>
354{
355 const auto globalIndex = Internal::ComputeGlobalIndex(fStrides, idx);
356 return fData[globalIndex];
357}
358
359/// \brief Access elements
360/// \param[in] idx Indices
361/// \return Reference to element
362template <typename Value_t, typename Container_t>
363template <typename... Idx>
365{
367 "Indices are not convertible to std::size_t.");
368 return operator()({static_cast<std::size_t>(idx)...});
369}
370
371/// \brief Access elements
372/// \param[in] idx Indices
373/// \return Reference to element
374template <typename Value_t, typename Container_t>
375template <typename... Idx>
377{
379 "Indices are not convertible to std::size_t.");
380 return operator()({static_cast<std::size_t>(idx)...});
381}
382
383/// \brief Transpose
384/// \returns New RTensor
385/// The tensor is transposed by inverting the associated memory layout from row-
386/// major to column-major and vice versa. Therefore, the underlying data is not
387/// touched.
388template <typename Value_t, typename Container_t>
390{
391 MemoryLayout layout;
392 // Transpose by inverting memory layout
393 if (fLayout == MemoryLayout::RowMajor) {
394 layout = MemoryLayout::ColumnMajor;
395 } else if (fLayout == MemoryLayout::ColumnMajor) {
396 layout = MemoryLayout::RowMajor;
397 } else {
398 throw std::runtime_error("Memory layout is not known.");
399 }
400
401 // Create copy of container
402 RTensor<Value_t, Container_t> x(fData, fShape, fStrides, layout);
403
404 // Reverse shape
405 std::reverse(x.fShape.begin(), x.fShape.end());
406
407 // Reverse strides
408 std::reverse(x.fStrides.begin(), x.fStrides.end());
409
410 return x;
411}
412
413/// \brief Squeeze dimensions
414/// \returns New RTensor
415/// Squeeze removes the dimensions of size one from the shape.
416template <typename Value_t, typename Container_t>
418{
419 // Remove dimensions of one and associated strides
420 Shape_t shape;
421 Shape_t strides;
422 for (std::size_t i = 0; i < fShape.size(); i++) {
423 if (fShape[i] != 1) {
424 shape.emplace_back(fShape[i]);
425 strides.emplace_back(fStrides[i]);
426 }
427 }
428
429 // If all dimensions are 1, we need to keep one.
430 // This does not apply if the inital shape is already empty. Then, return
431 // the empty shape.
432 if (shape.size() == 0 && fShape.size() != 0) {
433 shape.emplace_back(1);
434 strides.emplace_back(1);
435 }
436
437 // Create copy, attach new shape and strides and return
439 x.fShape = shape;
440 x.fStrides = strides;
441 return x;
442}
443
444/// \brief Expand dimensions
445/// \param[in] idx Index in shape vector where dimension is added
446/// \returns New RTensor
447/// Inserts a dimension of one into the shape.
448template <typename Value_t, typename Container_t>
450{
451 // Compose shape vector with additional dimensions and adjust strides
452 const int len = fShape.size();
453 auto shape = fShape;
454 auto strides = fStrides;
455 if (idx < 0) {
456 if (len + idx + 1 < 0) {
457 throw std::runtime_error("Given negative index is invalid.");
458 }
459 shape.insert(shape.end() + 1 + idx, 1);
460 strides.insert(strides.begin() + 1 + idx, 1);
461 } else {
462 if (idx > len) {
463 throw std::runtime_error("Given index is invalid.");
464 }
465 shape.insert(shape.begin() + idx, 1);
466 strides.insert(strides.begin() + idx, 1);
467 }
468
469 // Create copy, attach new shape and strides and return
471 x.fShape = shape;
472 x.fStrides = strides;
473 return x;
474}
475
476/// \brief Reshape tensor
477/// \param[in] shape Shape vector
478/// \returns New RTensor
479/// Reshape tensor without changing the overall size
480template <typename Value_t, typename Container_t>
482{
483 // Create copy, replace and return
485 x.ReshapeInplace(shape);
486 return x;
487}
488
489/// \brief Create a slice of the tensor
490/// \param[in] slice Slice vector
491/// \returns New RTensor
492/// A slice is a subset of the tensor defined by a vector of pairs of indices.
493template <typename Value_t, typename Container_t>
495{
496 // Sanitize size of slice
497 const auto sliceSize = slice.size();
498 const auto shapeSize = fShape.size();
499 if (sliceSize != shapeSize) {
500 std::stringstream ss;
501 ss << "Size of slice (" << sliceSize << ") is unequal number of dimensions (" << shapeSize << ").";
502 throw std::runtime_error(ss.str());
503 }
504
505 // Sanitize slice indices
506 // TODO: Sanitize slice indices
507 /*
508 for (std::size_t i = 0; i < sliceSize; i++) {
509 }
510 */
511
512 // Convert -1 in slice to proper pair of indices
513 // TODO
514
515 // Recompute shape and size
516 Shape_t shape(sliceSize);
517 for (std::size_t i = 0; i < sliceSize; i++) {
518 shape[i] = slice[i][1] - slice[i][0];
519 }
520 auto size = Internal::GetSizeFromShape(shape);
521
522 // Determine first element contributing to the slice and get the data pointer
523 Value_t *data;
524 Shape_t idx(sliceSize);
525 for (std::size_t i = 0; i < sliceSize; i++) {
526 idx[i] = slice[i][0];
527 }
528 data = &operator()(idx);
529
530 // Create copy and modify properties
532 x.fData = data;
533 x.fShape = shape;
534 x.fSize = size;
535
536 // Squeeze tensor and return
537 return x.Squeeze();
538}
539
540/// Copy RTensor to new object
541/// \param[in] layout Memory layout of the new RTensor
542/// \returns New RTensor
543/// The operation copies all elements of the current RTensor to a new RTensor
544/// with the given layout contiguous in memory. Note that this copies by default
545/// to a row major memory layout.
546template <typename Value_t, typename Container_t>
548{
549 // Create new tensor with zeros owning the memory
551
552 // Copy over the elements from this tensor
553 const auto mins = Shape_t(fShape.size());
554 const auto maxs = fShape;
555 auto idx = mins;
556 Internal::RecursiveCopy(*this, r, mins, maxs, idx, 0);
557
558 return r;
559}
560
561/// \brief Pretty printing
562/// \param[in] os Output stream
563/// \param[in] x RTensor
564/// \return Modified output stream
565template <typename T>
566std::ostream &operator<<(std::ostream &os, RTensor<T> &x)
567{
568 const auto shapeSize = x.GetShape().size();
569 if (shapeSize == 1) {
570 os << "{ ";
571 const auto size = x.GetSize();
572 for (std::size_t i = 0; i < size; i++) {
573 os << x({i});
574 if (i != size - 1)
575 os << ", ";
576 }
577 os << " }";
578 } else if (shapeSize == 2) {
579 os << "{";
580 const auto shape = x.GetShape();
581 for (std::size_t i = 0; i < shape[0]; i++) {
582 os << " { ";
583 for (std::size_t j = 0; j < shape[1]; j++) {
584 os << x({i, j});
585 if (j < shape[1] - 1) {
586 os << ", ";
587 } else {
588 os << " ";
589 }
590 }
591 os << "}";
592 }
593 os << " }";
594 } else {
595 os << "{ printing not yet implemented for this rank }";
596 }
597 return os;
598}
599
600} // namespace TMVA::Experimental
601} // namespace TMVA
602
603namespace cling {
604template <typename T>
605std::string printValue(TMVA::Experimental::RTensor<T> *x)
606{
607 std::stringstream ss;
608 ss << *x;
609 return ss.str();
610}
611} // namespace cling
612
613#endif // TMVA_RTENSOR
Py_ssize_t * fShape
size_t fSize
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
TBuffer & operator<<(TBuffer &buf, const Tmpl *obj)
Definition TBuffer.h:399
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
TRObject operator()(const T1 &t1) const
bool operator>=(const Iterator &rhs) const
Definition RTensor.hxx:297
bool operator==(const Iterator &rhs) const
Definition RTensor.hxx:289
std::random_access_iterator_tag iterator_category
Definition RTensor.hxx:268
Iterator(RTensor< Value_t, Container_t > &x, typename Index_t::value_type idx)
Definition RTensor.hxx:274
bool operator!=(const Iterator &rhs) const
Definition RTensor.hxx:294
difference_type operator-(const Iterator &rhs)
Definition RTensor.hxx:281
Iterator operator+(difference_type rhs) const
Definition RTensor.hxx:279
bool operator<(const Iterator &rhs) const
Definition RTensor.hxx:296
Iterator & operator+=(difference_type rhs)
Definition RTensor.hxx:282
Iterator & operator-=(difference_type rhs)
Definition RTensor.hxx:283
RTensor< Value_t, Container_t > & fTensor
Definition RTensor.hxx:265
bool operator>(const Iterator &rhs) const
Definition RTensor.hxx:295
Iterator operator-(difference_type rhs) const
Definition RTensor.hxx:280
Index_t::value_type GetGlobalIndex() const
Definition RTensor.hxx:299
bool operator<=(const Iterator &rhs) const
Definition RTensor.hxx:298
RTensor is a container with contiguous memory and shape information.
Definition RTensor.hxx:162
void ReshapeInplace(const Shape_t &shape)
Reshape tensor in place.
Definition RTensor.hxx:316
RTensor< Value_t, Container_t > Squeeze() const
Squeeze dimensions.
Definition RTensor.hxx:417
const std::shared_ptr< Container_t > GetContainer() const
Definition RTensor.hxx:247
Value_t & operator()(const Index_t &idx)
Access elements.
Definition RTensor.hxx:343
RTensor(Shape_t shape, MemoryLayout layout=MemoryLayout::RowMajor)
Construct a tensor owning data initialized with new container.
Definition RTensor.hxx:223
MemoryLayout GetMemoryLayout() const
Definition RTensor.hxx:248
Iterator end() noexcept
Definition RTensor.hxx:307
std::vector< Shape_t > Slice_t
Definition RTensor.hxx:168
RTensor(Value_t *data, Shape_t shape, Shape_t strides, MemoryLayout layout=MemoryLayout::RowMajor)
Construct a tensor as view on data.
Definition RTensor.hxx:201
RTensor< Value_t, Container_t > ExpandDims(int idx) const
Expand dimensions.
Definition RTensor.hxx:449
RTensor< Value_t, Container_t > Transpose() const
Transpose.
Definition RTensor.hxx:389
std::shared_ptr< Container_t > GetContainer()
Definition RTensor.hxx:246
Value_t & operator()(Idx... idx)
Access elements.
Definition RTensor.hxx:364
std::shared_ptr< Container_t > fContainer
Definition RTensor.hxx:177
RTensor(std::shared_ptr< Container_t > container, Shape_t shape, MemoryLayout layout=MemoryLayout::RowMajor)
Construct a tensor owning externally provided data.
Definition RTensor.hxx:211
RTensor< Value_t, Container_t > Copy(MemoryLayout layout=MemoryLayout::RowMajor) const
Copy RTensor to new object.
Definition RTensor.hxx:547
const Shape_t & GetStrides() const
Definition RTensor.hxx:243
std::size_t GetSize() const
Definition RTensor.hxx:241
RTensor< Value_t, Container_t > Reshape(const Shape_t &shape) const
Reshape tensor.
Definition RTensor.hxx:481
RTensor(Value_t *data, Shape_t shape, MemoryLayout layout=MemoryLayout::RowMajor)
Construct a tensor as view on data.
Definition RTensor.hxx:189
RTensor< Value_t, Container_t > Slice(const Slice_t &slice)
Create a slice of the tensor.
Definition RTensor.hxx:494
const Value_t * GetData() const
Definition RTensor.hxx:245
Iterator begin() noexcept
Definition RTensor.hxx:304
const Shape_t & GetShape() const
Definition RTensor.hxx:242
std::vector< std::size_t > Shape_t
Definition RTensor.hxx:166
Double_t x[n]
Definition legend1.C:17
void RecursiveCopy(const T &here, T &there, const std::vector< std::size_t > &mins, const std::vector< std::size_t > &maxs, std::vector< std::size_t > idx, std::size_t active)
Copy slice of a tensor recursively from here to there.
Definition RTensor.hxx:129
std::vector< std::size_t > ComputeStridesFromShape(const T &shape, MemoryLayout layout)
Compute strides from shape vector.
Definition RTensor.hxx:47
T ComputeIndicesFromGlobalIndex(const T &shape, MemoryLayout layout, const typename T::value_type idx)
Compute indices from global index.
Definition RTensor.hxx:81
std::size_t GetSizeFromShape(const T &shape)
Get size of tensor from shape vector.
Definition RTensor.hxx:28
std::size_t ComputeGlobalIndex(const U &strides, const V &idx)
Compute global index from indices.
Definition RTensor.hxx:99
MemoryLayout
Memory layout type (copy from RTensor.hxx)
Definition CudaTensor.h:47
create variable transformations
Type checking for all types of a parameter pack, e.g., used in combination with std::is_convertible.
Definition RTensor.hxx:111