Class np.MGridClass
- Namespace
- NumSharp
- Assembly
- NumSharp.dll
An instance which returns a dense ("fleshed out") multi-dimensional "meshgrid" when
indexed, so that every returned axis-grid has the SAME shape. The number of stacked grids and
their dimensionality equal the number of indexing slices. If the step length is not a complex
number, the stop is NOT inclusive; a complex step (e.g. "…:5j") instead specifies
the number of points, with the stop INCLUSIVE: point i is i * step + start with
step = (stop - start) / (N - 1), as NumPy computes it (so the last point can miss the stop
by a rounding: np.mgrid["0.3:0.9:7j"] ends at 0.9000000000000001, where
linspace puts it on 0.9).
public sealed class np.MGridClass
- Inheritance
-
np.MGridClass
- Inherited Members
Examples
NDArray x = np.mgrid["-1:1:5j"]; // array([-1., -0.5, 0., 0.5, 1.])
var (rows, cols) = np.mgrid["0:5", "0:3"]; // each shape (5, 3)
NDArray stacked = np.mgrid["0:5", "0:3"]; // shape (2, 5, 3)
Remarks
Port of NumPy 2.x numpy.mgrid (the sparse=False instance of
numpy.lib.index_tricks.nd_grid). It is the DENSE twin of ogrid and shares
its whole grammar and dtype/edge-case behaviour (see np.OGridClass; both run
NumPy's nd_grid.getitem statements, mgrid(object[]) and
ogrid(object[], string)): slices are written as strings
(np.mgrid["0:5", "0:3"] ≙ np.mgrid[0:5, 0:3]); one string may carry comma-separated
slices; Slice objects work; there are NO directives. A field written as digits is a
Python int (exact at any size), anything else a Python float. The mesh shares ONE dtype (int64
unless a float or an imaginary step appears, then float64).
Values. A single slice is arange(start, stop, step), or for an imaginary step
arange(0, N) * step + start (a fresh array). In a mesh each axis is i * step + start in
the mesh dtype — for a float step that is NOT arange's start + i * delta:
np.mgrid["0.5:1.5:0.1", "0:2"][0] holds 0.8, where arange(0.5, 1.5, 0.1) holds
0.7999999999999999.
Errors. A zero step is NumPy's ZeroDivisionError, DivideByZeroException with
Python's text (division by zero over ints, float division by zero once a float is
involved). A slice of a multi-slice mesh needs its stop unless its step is imaginary with one point:
NumPy then leaks AttributeError: 'tuple' object has no attribute 'step', NumSharp raises a
ValueError naming the slice, at the same point (a negative size of another axis can win
over it, as in NumPy). A single imaginary slice without its stop is Python's TypeError
(unsupported operand type(s) for -: 'NoneType' and 'int'). A NaN, infinite or past-int64 size
raises NumPy's text (cannot convert float NaN to integer, cannot convert float infinity to
integer, Maximum allowed dimension exceeded).
Shape. A single slice returns a bare 1-D array; N slices return the stacked array of
shape (N, size_0, …, size_{N-1}) — result[k] is the k-th coordinate broadcast
across the whole grid. It equals stack([broadcast_to(o, full) for o in ogrid[…]], axis:0)
(NumPy builds it as indices(sizes) then rescales each layer — same values, same layout).
Return. The np.MGridResult is NumPy's single array: it converts implicitly to
NDArray and Deconstructs / indexes into the per-axis grids.
https://numpy.org/doc/stable/reference/generated/numpy.mgrid.html
Properties
this[object[]]
Expands the slice expression into a dense mesh. See np.MGridClass for how a Python slice literal is spelled in C#.
public np.MGridResult this[params object[] key] { get; }
Parameters
keyobject[]Slice-expression strings (a colon-bearing
"start:stop[:step]", or several such comma-separated), and/or Slice objects.