Table of Contents

Class np.IndexExpression

Namespace
NumSharp
Assembly
NumSharp.dll

A nicer way to build up index tuples for arrays — the type behind s_ and index_exp. Use those two instances rather than constructing this directly.

public sealed class np.IndexExpression
Inheritance
np.IndexExpression
Inherited Members

Remarks

Port of NumPy 2.x numpy.lib.index_tricks.IndexExpression. For any index combination, a[indices] equals a[np.index_exp[indices]], but np.s_[…] can be stored in a variable, passed around and reused.

The indexer surface mirrors NDArray's. NumPy's s_ passes ANY index object straight through — np.s_[[1, 2]] is the list, np.s_[..., mask] the tuple — so anything writable as arr[…] must be writable as np.s_[…] too. The three overloads below cover the same vocabulary NDArray's three public indexers do, and each returns the array type that indexer consumes: basic expressions (slices, slice strings, integers) come back as Slice[], while anything advanced — a fancy index array, a boolean mask, or a basic/advanced mix — comes back as object[]. Either can be handed straight to arr[…].

C# divergence — s_ and index_exp return the same thing. NumPy's two instances differ only in maketuple: np.s_[2::2] yields the bare slice(2, None, 2) while np.index_exp[2::2] wraps it as (slice(2, None, 2),). C# has no such distinction at the index site — NDArray's indexers are this[params Slice[]] / this[params object[]], so a lone Slice and a one-element Slice[] are literally the same call. The pair is kept so NumPy code ports across verbatim.

https://numpy.org/doc/stable/reference/generated/numpy.s_.html

Properties

this[Slice[]]

Builds an index tuple out of already-constructed Slice objects — the programmatic spelling of the string form. Integer and string literals convert implicitly, so np.s_[0] and np.s_["1:3", "::2"] land here too.

public Slice[] this[params Slice[] item] { get; }

Parameters

item Slice[]

Property Value

Slice[]

this[object[]]

The ADVANCED-indexing form: captures any index expression NDArray's this[params object[]] accepts, so everything writable as arr[…] is also writable as np.s_[…] and stored for later.

public object[] this[params object[] item] { get; }

Parameters

item object[]

Any mix of Slice, slice-notation string, integer, NDArray (integer fancy index or boolean mask), int[] / long[], bool[] (and rectangular bool[,]), IList sequences, and ITuples.

Property Value

object[]

Remarks

Overload resolution keeps the basic forms on the Slice[] overload — a lone Slice, an int (which converts implicitly to Slice) and a slice string all still return Slice[]. This overload takes over the moment an entry cannot be a Slice: a fancy index array, a mask, or a basic/advanced mix such as np.s_[Slice.All, np.array(new[] {0, 2})]. Both return types feed arr[…] directly, including a NDArray<TDType> (its own indexers do not hide the inherited this[params object[]]).

this[string]

Parses Python index notation — comma-separated slices, indices, ... and newaxis — into the index tuple NumSharp's array indexer consumes.

public Slice[] this[string item] { get; }

Parameters

item string

e.g. "2::2", ":, 0", "..., ::-1", "np.newaxis, 3:".

Property Value

Slice[]

Exceptions

ArgumentException

The notation is malformed.

maketuple

True for index_exp (NumPy always returns a tuple), false for s_. Kept to mirror NumPy's constructor; it has no observable effect in C# because a Slice[] is already how NumSharp spells an index tuple.

public bool maketuple { get; }

Property Value

bool