A slice defines a sub-range or patch of a dimension.
- Introduction
The slice Slice(start,end,step) defines the subvector
long end
Definition slice.h:107
long step
Definition slice.h:108
long start
Definition slice.h:106
with indices as if generated from these loops
else
runtime
error detected when slice is used
void error(const char *msg)
Definition world.cc:147
static const double a
Definition nonlinschro.cc:118
Note that start and end are inclusive, unlike the Python convention of specifying end+1 (note that we cannot do this easily in C/C++ unless we also define a special value to indicate the end of a dimension of unknown size).
- Special meanings and conventions
Negative values for start or end (similar to Python) are relative to the end of the (possibly unknown) dimension. E.g.,
end=-1 is equivalent to end=dim-1
start=-4 is equivalent to start=dim-4
step=0 and start==end implies dimension will be eliminated when the slice is used to index a tensor
- The default constructor specifies the entire dimension
- If the input length is not an exact multiple of step, end is rounded towards start to recover the behaviour of the
<= or >= bounds in the loops specified above.
Special slices have been defined as constants
_ (1 underscore) = Slice(0,-1,1) = full slice in current order
___ (3 underscores) = Array of Slices with value _ so that t(___) will generate an assignable view of the entire tensor t .
- _reverse =
Slice(-1,0,-1) = full dimension reversed
- Examples
Slice() — full slice in current order
Slice(0,-1,1) — full slice in current order
Slice(3,3,0) — eliminate this dimension setting index=3 (step=0)
Slice(3,3,1) — reduce a dimension to length 1 using index=3 (step=1)
Slice(-1,0,-1) — reverse a dimension
Slice(0,-1,2) — use all even numbered elements
Slice(1,-1,2) — use all odd numbered elements