Class np.NDIndex
- Namespace
- NumSharp
- Assembly
- NumSharp.dll
NumPy's numpy.ndindex — a C-order odometer over the index space of a shape,
yielding one index array per step with the LAST dimension varying fastest.
// numpy: list(np.ndindex(3, 2)) -> [(0,0), (0,1), (1,0), (1,1), (2,0), (2,1)]
foreach (var idx in np.ndindex(3, 2)) { /* idx = {0,0}, {0,1}, {1,0}, ... */ }
Like NumPy the object is its OWN iterator (iter(i) is i): it keeps a single
live cursor, so a second enumeration resumes where the first stopped rather than
restarting — the same contract np.Broadcast follows.
public class np.NDIndex : IEnumerable<long[]>, IEnumerable, IEnumerator<long[]>, IEnumerator, IDisposable
- Inheritance
-
np.NDIndex
- Implements
-
IEnumerable<long[]>IEnumerator<long[]>
- Inherited Members
Remarks
Port of NumPy 2.4.2's numpy/lib/_index_tricks_impl.py, whose body is
product(*map(range, shape)) — a plain odometer. NumPy itself moved
ndindex OFF nditer onto itertools.product, so this class
deliberately allocates no iterator state: there are no operands to walk, only a
counter. (nditer(NDArray, string[], string[], DType[], char, string, int[][], long[], long) is the API that drives NumSharp's NDIterRef.)
Indices are long — NumPy's intp, the house type for index-valued
output. Each step yields a FRESH array, never a recycled buffer, so materializing
the enumeration (NumPy's list(np.ndindex(...))) gives distinct indices.
Properties
Current
The index array produced by the most recent step. Its length is the number of
dimensions — 0 for the 0-d index space, whose single step yields an empty array
(NumPy's ()).
public long[] Current { get; }
Property Value
- long[]
Methods
AsSpans()
The allocation-free form of this odometer — the high-performance counterpart.
Where iterating this object yields a FRESH long[] per step,
np.NDIndexSpans yields a ReadOnlySpan<T> over a coordinate
buffer REUSED each step, so a walk of the whole index space allocates nothing:
foreach (var idx in np.ndindex(3, 2).AsSpans()) // idx is ReadOnlySpan<long>
Sum += a.GetValue(idx); // idx valid until the next step
ndindex walks an index SPACE, not an array — there are no element values, so
there is no ref T form (unlike flat<T>(NDArray, bool) or
AsRef(bool), which reference an array's elements). This
span form is its non-boxed, non-allocating equivalent. Copy the span
(.ToArray()) to keep an index past the current iteration.
public np.NDIndexSpans AsSpans()
Returns
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
GetEnumerator()
Returns the odometer itself as its iterator — matching NumPy's iter(i) is i,
so enumeration shares the single live cursor.
public IEnumerator<long[]> GetEnumerator()
Returns
- IEnumerator<long[]>
MoveNext()
Advances the odometer one step, incrementing the LAST axis first and carrying leftward (C-order). Returns false once every index has been produced.
public bool MoveNext()