Struct np.IndicesResult
- Namespace
- NumSharp
- Assembly
- NumSharp.dll
The result of <xref href="NumSharp.np.indices(NumSharp.Shape%2cNumSharp.DType%2cSystem.Boolean)" data-throw-if-not-resolved="false"></xref> — NumPy's dense grid OR sparse tuple as one type
(np.unique's <xref href="NumSharp.np.UniqueResult" data-throw-if-not-resolved="false"></xref> pattern). Dense: <xref href="NumSharp.np.IndicesResult.values" data-throw-if-not-resolved="false"></xref> is the grid of shape
<code>(len(dimensions), *dimensions)</code>; sparse: <xref href="NumSharp.np.IndicesResult.values" data-throw-if-not-resolved="false"></xref> is null and the result holds one
broadcast-shaped array per axis.
Both read like NumPy's: it indexes and deconstructs per axis (var (i, j) = np.indices((2, 3)); works for
either form, as Python's tuple unpacking iterates a dense grid's first axis), converts implicitly to
NDArray[] (the per-axis arrays), and a dense result converts implicitly to the grid's
NDArray.
public readonly struct np.IndicesResult : INDArrayCarrier, IDisposable
- Implements
- Inherited Members
Remarks
API-parity batch 11 (the owner's decision 3): it replaces np.indices' NDArray return and the
NumSharp-only np.indices_sparse. The NDArray members callers read on a dense grid forward to
values (shape, size, dtype, the typed getters, ToArray<T>, array_equal,
reshape, astype, flags).
It is IDisposable as the NDArray it replaced was: using var g = np.indices(...);
releases the grid, or every per-axis array of a sparse result.
Footguns: converting a SPARSE result to NDArray raises (NumPy's tuple of differently
shaped arrays has no array form); an extension method of NDArray does not see through the
implicit conversion - call it on values; it is not enumerable (convert it to
NDArray[] for foreach or LINQ over the per-axis arrays).
Properties
this[int]
The index-th axis' coordinates: a dense grid's grid[index] (a view) or the
sparse tuple's index-th array.
public NDArray this[int index] { get; }
Parameters
indexintThe axis, 0-based (a negative index counts from the end in the dense form).
Property Value
- NDArray
The coordinates of that axis.
Exceptions
- IndexOutOfRangeException
The index is out of range (sparse).
- IndexError
The index is out of range (dense, NumPy's indexing text).
Length
The number of per-axis arrays: len(dimensions) (a dense grid's first extent).
public int Length { get; }
Property Value
Shape
The dense grid's Shape.
public Shape Shape { get; }
Property Value
dtype
The dense grid's dtype.
public DType dtype { get; }
Property Value
flags
The dense grid's flags.
public NDArrayFlags flags { get; }
Property Value
ndim
The dense grid's rank: 1 + len(dimensions).
public int ndim { get; }
Property Value
shape
The dense grid's shape (a sparse result has none: see NumSharp.np.IndicesResult.Dense).
public long[] shape { get; }
Property Value
- long[]
size
The dense grid's element count.
public long size { get; }
Property Value
sparse
Whether this is NumPy's sparse=True tuple (no dense grid).
public bool sparse { get; }
Property Value
typecode
The dense grid's storage lane.
public NPTypeCode typecode { get; }
Property Value
values
The dense grid of shape (len(dimensions), *dimensions), or null for a sparse
result.
public NDArray values { get; }
Property Value
Methods
Deconstruct(out NDArray, out NDArray)
Deconstructs a 2-D grid's coordinates: var (i, j) = np.indices((2, 3)).
public void Deconstruct(out NDArray first, out NDArray second)
Parameters
Exceptions
- InvalidOperationException
The grid is not 2-D (Python's unpacking ValueError text).
Deconstruct(out NDArray, out NDArray, out NDArray)
Deconstructs a 3-D grid's coordinates.
public void Deconstruct(out NDArray first, out NDArray second, out NDArray third)
Parameters
firstNDArrayAxis 0's coordinates.
secondNDArrayAxis 1's coordinates.
thirdNDArrayAxis 2's coordinates.
Exceptions
- InvalidOperationException
The grid is not 3-D.
Deconstruct(out NDArray, out NDArray, out NDArray, out NDArray)
Deconstructs a 4-D grid's coordinates.
public void Deconstruct(out NDArray first, out NDArray second, out NDArray third, out NDArray fourth)
Parameters
firstNDArrayAxis 0's coordinates.
secondNDArrayAxis 1's coordinates.
thirdNDArrayAxis 2's coordinates.
fourthNDArrayAxis 3's coordinates.
Exceptions
- InvalidOperationException
The grid is not 4-D.
Dispose()
Releases the dense grid, or every per-axis array of a sparse result (the NDArray it replaced was disposable the same way). Footgun: an array taken from a SPARSE result (the indexer, a deconstruction, ToArray()) is the same object, so it is released too; a dense grid's per-axis arrays are views that hold their own reference on the grid's buffer and stay readable until released themselves.
public void Dispose()
GetDouble(params long[])
The dense grid's element at the given coordinates, as a double (a dtype=float grid's own
type; any other dtype's element is converted).
public double GetDouble(params long[] indices)
Parameters
indiceslong[]The coordinates.
Returns
- double
The element.
GetInt32(params long[])
The dense grid's element at the given coordinates, as an int.
public int GetInt32(params long[] indices)
Parameters
indiceslong[]The coordinates.
Returns
- int
The element.
GetInt64(params long[])
The dense grid's element at the given coordinates, as a long.
public long GetInt64(params long[] indices)
Parameters
indiceslong[]The coordinates.
Returns
- long
The element.
ToArray()
The per-axis arrays as an NDArray[]: the sparse tuple, or a dense grid's
first-axis views (NumPy's tuple(grid)).
public NDArray[] ToArray()
Returns
- NDArray[]
A new array of the per-axis arrays.
ToArray<T>()
The dense grid's elements as a new T[] in C order.
public T[] ToArray<T>() where T : unmanaged
Returns
- T[]
A new managed array.
Type Parameters
TThe element type, as ToArray<T>() reads it.
array_equal(NDArray)
True if the dense grid equals rhs element-wise (same shape).
public bool array_equal(NDArray rhs)
Parameters
rhsNDArrayThe array to compare with.
Returns
- bool
Whether the shapes and elements are equal.
astype(DType, char, string, bool)
The dense grid cast to dtype
(astype(DType, char, string, bool)); a grid built in its final dtype
(np.indices(dims, dtype)) skips the extra copy.
public NDArray astype(DType dtype, char order = 'K', string casting = "unsafe", bool copy = true)
Parameters
dtypeDTypeThe target dtype.
ordercharThe memory order of a copy ('K' keeps the grid's).
castingstringThe casting rule ('unsafe' by default, as NumPy's astype).
copyboolWhether to copy when no cast is needed.
Returns
- NDArray
The cast grid.
Exceptions
- InvalidCastException
The result is sparse, or the cast is refused under
casting.
reshape(params long[])
The dense grid with a new shape (reshape(params long[])).
public NDArray reshape(params long[] shape)
Parameters
shapelong[]The new dimensions.
Returns
- NDArray
The reshaped grid.
Exceptions
- IncorrectShapeException
The size does not match.
Operators
implicit operator NDArray(IndicesResult)
The dense grid (NumPy's single-array return).
public static implicit operator NDArray(np.IndicesResult result)
Parameters
resultnp.IndicesResultThe result.
Returns
- NDArray
The grid.
Exceptions
- InvalidCastException
The result is sparse (a tuple of differently shaped arrays).
implicit operator NDArray[](IndicesResult)
The per-axis arrays (ToArray()).
public static implicit operator NDArray[](np.IndicesResult result)
Parameters
resultnp.IndicesResultThe result.
Returns
- NDArray[]
A new array of the per-axis arrays.