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>>
217 fData = &(*container->begin());
315template <
typename Value_t,
typename Container_t>
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] <<
", ";
326 ss << shape[i] <<
" }.";
329 throw std::runtime_error(ss.str());
342template <
typename Value_t,
typename Container_t>
346 return fData[globalIndex];
352template <
typename Value_t,
typename Container_t>
356 return fData[globalIndex];
362template <
typename Value_t,
typename Container_t>
363template <
typename... Idx>
367 "Indices are not convertible to std::size_t.");
368 return operator()({
static_cast<std::size_t
>(idx)...});
374template <
typename Value_t,
typename Container_t>
375template <
typename... Idx>
379 "Indices are not convertible to std::size_t.");
380 return operator()({
static_cast<std::size_t
>(idx)...});
388template <
typename Value_t,
typename Container_t>
393 if (fLayout == MemoryLayout::RowMajor) {
394 layout = MemoryLayout::ColumnMajor;
395 }
else if (fLayout == MemoryLayout::ColumnMajor) {
396 layout = MemoryLayout::RowMajor;
398 throw std::runtime_error(
"Memory layout is not known.");
405 std::reverse(
x.fShape.begin(),
x.fShape.end());
408 std::reverse(
x.fStrides.begin(),
x.fStrides.end());
416template <
typename Value_t,
typename Container_t>
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]);
432 if (shape.size() == 0 && fShape.size() != 0) {
433 shape.emplace_back(1);
434 strides.emplace_back(1);
440 x.fStrides = strides;
448template <
typename Value_t,
typename Container_t>
452 const int len = fShape.size();
454 auto strides = fStrides;
456 if (len + idx + 1 < 0) {
457 throw std::runtime_error(
"Given negative index is invalid.");
459 shape.insert(shape.end() + 1 + idx, 1);
460 strides.insert(strides.begin() + 1 + idx, 1);
463 throw std::runtime_error(
"Given index is invalid.");
465 shape.insert(shape.begin() + idx, 1);
466 strides.insert(strides.begin() + idx, 1);
472 x.fStrides = strides;
480template <
typename Value_t,
typename Container_t>
485 x.ReshapeInplace(shape);
493template <
typename Value_t,
typename Container_t>
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());
517 for (std::size_t i = 0; i < sliceSize; i++) {
518 shape[i] = slice[i][1] - slice[i][0];
525 for (std::size_t i = 0; i < sliceSize; i++) {
526 idx[i] = slice[i][0];
546template <
typename Value_t,
typename Container_t>
553 const auto mins =
Shape_t(fShape.size());
554 const auto maxs = fShape;
568 const auto shapeSize =
x.GetShape().size();
569 if (shapeSize == 1) {
571 const auto size =
x.GetSize();
572 for (std::size_t i = 0; i <
size; i++) {
578 }
else if (shapeSize == 2) {
580 const auto shape =
x.GetShape();
581 for (std::size_t i = 0; i < shape[0]; i++) {
583 for (std::size_t j = 0; j < shape[1]; j++) {
585 if (j < shape[1] - 1) {
595 os <<
"{ printing not yet implemented for this rank }";
607 std::stringstream ss;
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
TBuffer & operator<<(TBuffer &buf, const Tmpl *obj)
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 > 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.