Table of Contents

Class Slice

Namespace
NumSharp
Assembly
NumSharp.dll
                                                                                                                                 <br />

NDArray can be indexed using slicing
A slice is constructed by start:stop:step notation

Examples:

a[start:stop] # items start through stop-1
a[start:] # items start through the rest of the array
a[:stop] # items from the beginning through stop-1

The key point to remember is that the :stop value represents the first value that is not
in the selected slice. So, the difference between stop and start is the number of elements
selected (if step is 1, the default).

There is also the step value, which can be used with any of the above:
a[:] # a copy of the whole array
a[start:stop:step] # start through not past stop, by step

The other feature is that start or stop may be a negative number, which means it counts
from the end of the array instead of the beginning. So:
a[-1] # last item in the array
a[-2:] # last two items in the array
a[:-2] # everything except the last two items
Similarly, step may be a negative number:

a[::- 1] # all items in the array, reversed
a[1::- 1] # the first two items, reversed
a[:-3:-1] # the last two items, reversed
a[-3::- 1] # everything except the last two items, reversed

NumSharp is kind to the programmer if there are fewer items than
you ask for. For example, if you ask for a[:-2] and a only contains one element, you get an
empty list instead of an error.Sometimes you would prefer the error, so you have to be aware
that this may happen.

Adapted from Greg Hewgill's answer on Stackoverflow: https://stackoverflow.com/questions/509211/understanding-slice-notation

Note: special IsIndex == true
It will pick only a single value at Start in this dimension effectively reducing the Shape of the sliced matrix by 1 dimension.
It can be used to reduce an N-dimensional array/matrix to a (N-1)-dimensional array/matrix

Example:
a=[[1, 2], [3, 4]]
a[:, 1] returns the second column of that 2x2 matrix as a 1-D vector

public class Slice : IIndex
Inheritance
Slice
Implements
Inherited Members
Extension Methods

Constructors

Slice(int?, int?, int)

ndarray can be indexed using slicing slice is constructed by start:stop:step notation

public Slice(int? start = null, int? stop = null, int step = 1)

Parameters

start int?

Start index of the slice, null means from the start of the array

stop int?

Stop index (first index after end of slice), null means to the end of the array

step int

Optional step to select every n-th element, defaults to 1

Slice(string)

public Slice(string slice_notation)

Parameters

slice_notation string

Fields

All

return : for this dimension

public static readonly Slice All

Field Value

Slice

Ellipsis

fill up the missing dimensions with : at this point, corresponds to ...

public static readonly Slice Ellipsis

Field Value

Slice

IsEllipsis

public bool IsEllipsis

Field Value

bool

IsIndex

public bool IsIndex

Field Value

bool

IsNewAxis

public bool IsNewAxis

Field Value

bool

NewAxis

insert a new dimension at this point

public static readonly Slice NewAxis

Field Value

Slice

None

return 0:0 for this dimension

public static readonly Slice None

Field Value

Slice

Start

public int? Start

Field Value

int?

Step

public int Step

Field Value

int

Stop

public int? Stop

Field Value

int?

Properties

Length

Length of the slice. The length is not guaranteed to be known for i.e. a slice like ":". Make sure to check Start and Stop for null before using it

public int? Length { get; }

Property Value

int?

Methods

Equals(object)

Determines whether the specified object is equal to the current object.

public override bool Equals(object obj)

Parameters

obj object

The object to compare with the current object.

Returns

bool

true if the specified object is equal to the current object; otherwise, false.

FormatSlices(params Slice[])

Creates Python array slice notation out of an array of Slice objects (mainly used for tests)

public static string FormatSlices(params Slice[] slices)

Parameters

slices Slice[]

Returns

string

GetHashCode()

Serves as the default hash function.

public override int GetHashCode()

Returns

int

A hash code for the current object.

GetSize()

public int GetSize()

Returns

int

Index(int)

return exactly one element at this dimension and reduce the shape from n-dim to (n-1)-dim

public static Slice Index(int index)

Parameters

index int

Returns

Slice

ParseSlices(string)

Parses Python array slice notation and returns an array of Slice objects

public static Slice[] ParseSlices(string multi_slice_notation)

Parameters

multi_slice_notation string

Returns

Slice[]

ToSliceDef(int)

Converts the user Slice into an internal SliceDef which is easier to calculate with

public SliceDef ToSliceDef(int dim)

Parameters

dim int

Returns

SliceDef

ToString()

Returns a string that represents the current object.

public override string ToString()

Returns

string

A string that represents the current object.

Operators

operator --(Slice)

public static Slice operator --(Slice a)

Parameters

a Slice

Returns

Slice

operator ==(Slice, Slice)

public static bool operator ==(Slice a, Slice b)

Parameters

a Slice
b Slice

Returns

bool

implicit operator Slice(int)

public static implicit operator Slice(int index)

Parameters

index int

Returns

Slice

implicit operator Slice(string)

public static implicit operator Slice(string slice)

Parameters

slice string

Returns

Slice

operator ++(Slice)

public static Slice operator ++(Slice a)

Parameters

a Slice

Returns

Slice

operator !=(Slice, Slice)

public static bool operator !=(Slice a, Slice b)

Parameters

a Slice
b Slice

Returns

bool