14namespace Experimental {
30 if (shape.size() == 0)
49 const auto size = shape.size();
51 if (layout == MemoryLayout::RowMajor) {
52 for (std::size_t i = 0; i <
size; i++) {
54 strides[
size - 1 - i] = 1;
56 strides[
size - 1 - i] = strides[
size - 1 - i + 1] * shape[
size - 1 - i + 1];
59 }
else if (layout == MemoryLayout::ColumnMajor) {
60 for (std::size_t i = 0; i <
size; i++) {
64 strides[i] = strides[i - 1] * shape[i - 1];
69 ss <<
"Memory layout type is not valid for calculating strides.";
70 throw std::runtime_error(ss.str());
83 const auto size = shape.size();
87 for (std::size_t i = 0; i <
size; i++) {
88 indices[i] =
int(
r / strides[i]);
98template <
typename U,
typename V>
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];
110template <
class... Ts>
114template <
class T0,
class... Ts>
115struct and_types<T0, Ts...> : std::integral_constant<bool, T0() && and_types<Ts...>()> {
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)
133 const auto size = idx.size();
134 for (std::size_t i = mins[active]; i < maxs[active]; i++) {
136 if (active ==
size - 1) {
138 for (std::size_t j = 0; j <
size; j++) {
139 idxThere[j] -= mins[j];
141 there(idxThere) = here(idx);
161template <
typename V,
typename C = std::vector<V>>
316template <
typename Value_t,
typename Container_t>
321 std::stringstream ss;
322 ss <<
"Cannot reshape tensor with size " <<
fSize <<
" into shape { ";
323 for (std::size_t i = 0; i < shape.size(); i++) {
324 if (i != shape.size() - 1) {
325 ss << shape[i] <<
", ";
327 ss << shape[i] <<
" }.";
330 throw std::runtime_error(ss.str());
343template <
typename Value_t,
typename Container_t>
347 return fData[globalIndex];
353template <
typename Value_t,
typename Container_t>
357 return fData[globalIndex];
363template <
typename Value_t,
typename Container_t>
364template <
typename... Idx>
368 "Indices are not convertible to std::size_t.");
369 return operator()({
static_cast<std::size_t
>(idx)...});
375template <
typename Value_t,
typename Container_t>
376template <
typename... Idx>
380 "Indices are not convertible to std::size_t.");
381 return operator()({
static_cast<std::size_t
>(idx)...});
389template <
typename Value_t,
typename Container_t>
394 if (fLayout == MemoryLayout::RowMajor) {
395 layout = MemoryLayout::ColumnMajor;
396 }
else if (fLayout == MemoryLayout::ColumnMajor) {
397 layout = MemoryLayout::RowMajor;
399 throw std::runtime_error(
"Memory layout is not known.");
406 std::reverse(
x.fShape.begin(),
x.fShape.end());
409 std::reverse(
x.fStrides.begin(),
x.fStrides.end());
417template <
typename Value_t,
typename Container_t>
423 for (std::size_t i = 0; i <
fShape.size(); i++) {
425 shape.emplace_back(
fShape[i]);
426 strides.emplace_back(fStrides[i]);
433 if (shape.size() == 0 &&
fShape.size() != 0) {
434 shape.emplace_back(1);
435 strides.emplace_back(1);
441 x.fStrides = strides;
449template <
typename Value_t,
typename Container_t>
455 auto strides = fStrides;
460 throw std::runtime_error(
"Given negative index is invalid.");
462 else if (idx >
len) {
463 throw std::runtime_error(
"Given index is invalid.");
465 shape.insert(shape.begin() + idx, 1);
471 x.fStrides = strides;
479template <
typename Value_t,
typename Container_t>
484 x.ReshapeInplace(shape);
492template <
typename Value_t,
typename Container_t>
496 RTensor <Value_t, Container_t>
x(shape, fLayout);
500 std::copy(this->GetData(), this->GetData() +
n,
x.GetData() );
509template <
typename Value_t,
typename Container_t>
513 const auto sliceSize = slice.size();
514 const auto shapeSize =
fShape.size();
515 if (sliceSize != shapeSize) {
516 std::stringstream ss;
517 ss <<
"Size of slice (" << sliceSize <<
") is unequal number of dimensions (" << shapeSize <<
").";
518 throw std::runtime_error(ss.str());
533 for (std::size_t i = 0; i < sliceSize; i++) {
534 shape[i] = slice[i][1] - slice[i][0];
541 for (std::size_t i = 0; i < sliceSize; i++) {
542 idx[i] = slice[i][0];
562template <
typename Value_t,
typename Container_t>
584 const auto shapeSize =
x.GetShape().size();
585 if (shapeSize == 1) {
587 const auto size =
x.GetSize();
588 for (std::size_t i = 0; i <
size; i++) {
594 }
else if (shapeSize == 2) {
596 const auto shape =
x.GetShape();
597 for (std::size_t i = 0; i < shape[0]; i++) {
599 for (std::size_t j = 0; j < shape[1]; j++) {
601 if (j < shape[1] - 1) {
611 os <<
"{ printing not yet implemented for this rank }";
623 std::stringstream ss;
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
TBuffer & operator<<(TBuffer &buf, const Tmpl *obj)
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
std::ptrdiff_t difference_type
bool operator>=(const Iterator &rhs) const
bool operator==(const Iterator &rhs) const
std::random_access_iterator_tag iterator_category
Iterator(RTensor< Value_t, Container_t > &x, typename Index_t::value_type idx)
bool operator!=(const Iterator &rhs) const
difference_type operator-(const Iterator &rhs)
Iterator operator+(difference_type rhs) const
bool operator<(const Iterator &rhs) const
Index_t::value_type fGlobalIndex
Iterator & operator+=(difference_type rhs)
Iterator & operator-=(difference_type rhs)
RTensor< Value_t, Container_t > & fTensor
bool operator>(const Iterator &rhs) const
Iterator operator-(difference_type rhs) const
Index_t::value_type GetGlobalIndex() const
bool operator<=(const Iterator &rhs) const
RTensor is a container with contiguous memory and shape information.
void ReshapeInplace(const Shape_t &shape)
Reshape tensor in place.
RTensor< Value_t, Container_t > Squeeze() const
Squeeze dimensions.
const std::shared_ptr< Container_t > GetContainer() const
Value_t & operator()(const Index_t &idx)
Access elements.
RTensor(Shape_t shape, MemoryLayout layout=MemoryLayout::RowMajor)
Construct a tensor owning data initialized with new container.
MemoryLayout GetMemoryLayout() const
std::vector< Shape_t > Slice_t
RTensor(Value_t *data, Shape_t shape, Shape_t strides, MemoryLayout layout=MemoryLayout::RowMajor)
Construct a tensor as view on data.
RTensor< Value_t, Container_t > ExpandDims(int idx) const
Expand dimensions.
RTensor< Value_t, Container_t > Transpose() const
Transpose.
std::shared_ptr< Container_t > GetContainer()
Value_t & operator()(Idx... idx)
Access elements.
std::shared_ptr< Container_t > fContainer
RTensor(std::shared_ptr< Container_t > container, Shape_t shape, MemoryLayout layout=MemoryLayout::RowMajor)
Construct a tensor owning externally provided data.
RTensor< Value_t, Container_t > Resize(const Shape_t &shape)
Resize tensor.
RTensor< Value_t, Container_t > Copy(MemoryLayout layout=MemoryLayout::RowMajor) const
Copy RTensor to new object.
const Shape_t & GetStrides() const
std::size_t GetSize() const
RTensor< Value_t, Container_t > Reshape(const Shape_t &shape) const
Reshape tensor.
RTensor(Value_t *data, Shape_t shape, MemoryLayout layout=MemoryLayout::RowMajor)
Construct a tensor as view on data.
RTensor< Value_t, Container_t > Slice(const Slice_t &slice)
Create a slice of the tensor.
const Value_t * GetData() const
Iterator begin() noexcept
const Shape_t & GetShape() const
std::vector< std::size_t > Shape_t
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.
std::vector< std::size_t > ComputeStridesFromShape(const T &shape, MemoryLayout layout)
Compute strides from shape vector.
T ComputeIndicesFromGlobalIndex(const T &shape, MemoryLayout layout, const typename T::value_type idx)
Compute indices from global index.
std::size_t GetSizeFromShape(const T &shape)
Get size of tensor from shape vector.
std::size_t ComputeGlobalIndex(const U &strides, const V &idx)
Compute global index from indices.
MemoryLayout
Memory layout type (copy from RTensor.hxx)
create variable transformations
Type checking for all types of a parameter pack, e.g., used in combination with std::is_convertible.