Skip to content

Slice and Subtensor Examples

amitsingh19975 edited this page Aug 23, 2019 · 3 revisions

This wiki contains examples for both the slice and the subtensor as they both are interlinked with each other. This will describe how to construct and use them.

CAUTION

All the example which has an extent in their comments are the values after transforming them e.g

// original value (start: -6, end: -1, step: 1)
// after transformation ( start: extent - 6, end: extent - 1, step: 1 )
auto s1 = span::slice<-6,-1>{};

Slice

Slice with Single Argument

This kind of slice will get a single element from a given extent. The result will be having an equal start and end value and step of 1 as the default value;

using namespace boost::numeric::ublas;

auto s1 = span::slice<3>{};
auto s2 = span::slice<-3>{};
auto s3 = span::slice<>{3};
auto s4 = span::slice<>{-3};

Slice with Two Arguments

This kind of slice will get elements in a given range and step is set to 1 as default.

using namespace boost::numeric::ublas;

// dynamic slice ( start: 0, end: 10, step: 1 )
auto s1 = span::slice<>{0,10};

// static slice ( start: 0, end: 10, step: 1 )
auto s2 = span::slice<0,10>{};

// dynamic slice ( start: 1, end: ( extent - 2 ), step: 1 )
auto s3 = span::slice<>{1,-2};

// static slice ( start: 1, end: ( extent - 2 ), step: 1 )
auto s4 = span::slice<1,-2>{};

// dynamic slice ( start: ( extent - 2 ), end: ( extent - 1 ), step: 1 )
auto s5 = span::slice<>{-2,-1};

// dynamic slice ( start: ( extent - 2 ), end: ( extent - 1 ), step: 1 )
auto s6 = span::slice<-2,-1>{};

Slice with Three Arguments

This kind of slice will get elements in a given range and step is set to 1 as default.

using namespace boost::numeric::ublas;

// dynamic slice ( start: 0, end: 10, step: 4 )
auto s1 = span::slice<>{0,10, 4};

// static slice ( start: 0, end: 10, step: 4 )
auto s2 = span::slice<0,10,4>{};

// dynamic slice ( start: 1, end: ( extent - 2 ), step: 3 )
auto s3 = span::slice<>{1,-2, 3};

// static slice ( start: 1, end: ( extent - 2 ), step: 3 )
auto s4 = span::slice<1,-2,3>{};

// dynamic slice ( start: ( extent - 2 ), end: ( extent - 1 ), step: 1 )
auto s5 = span::slice<>{-2,-1,1};

// dynamic slice ( start: ( extent - 2 ), end: ( extent - 1 ), step: 1 )
auto s6 = span::slice<-2,-1,1>{};

Slice with Empty Argument

First of all, there was an ambiguity which I needed to make a decision on which was, should basic_slice<ptrdiff_t> or slice<> be static slice or dynamic slice and I chose it to be dynamic as most of the dynamic slice be of this form. So when we create slice<>, it will be treated as the dynamic slice.

using namespace boost::numeric::ublas;

// dynamic slice ( start: 0, end: 0, step: 1 )
auto s1 = span::slice<>{};

Subtensor

Constructor using Subtensor

using namespace boost::numeric::ublas;
auto t1 = tensor{static_extents<10,10>{},1.f};
auto t2 = tensor{static_extents<10,10>{},1.f};

// static subtensor
auto s1 = subtensor<decltype(t1),span::slice<0,5>,span::slice<4>>{ t1 };

// dynamic subtensor
auto s2 = subtensor{ t1, ,span::slice<>{0,5},span::slice<>{4} };

Constructor of Subtensor using tensor

using namespace boost::numeric::ublas;
auto t1 = tensor{static_extents<10,10>{},1.f};
auto t2 = tensor{static_extents<10,10>{},1.f};

// static subtensor
// here span::slice<>{} considered as static slice
auto s1 = t1(span::slice<2,3>{}, span::slice<>{});

// dynamic subtensor
auto s2 = t1(span::slice<>{2,3}, span::slice<>{});

// dynamic subtensor
auto s3 = t2(span::slice<>{2,3}, span::slice<>{});
Clone this wiki locally