Class np.OGridClass
- Namespace
- NumSharp
- Assembly
- NumSharp.dll
An instance which returns an open multi-dimensional "meshgrid" when indexed, so that only
one dimension of each returned array is greater than 1. The number and dimensionality of the
outputs 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 (not linspace, which forces the
last point onto the stop).
public sealed class np.OGridClass
- Inheritance
-
np.OGridClass
- Inherited Members
Examples
NDArray x = np.ogrid["-1:1:5j"]; // array([-1., -0.5, 0., 0.5, 1.])
var (y, xx) = np.ogrid["0:5", "0:5"]; // y.shape (5,1), xx.shape (1,5)
NDArray[] g = np.ogrid["0:3", "0:4", "0:5"];
NDArray sum = np.ogrid["0:5", "0:5"][0] + np.ogrid["0:5", "0:5"][1]; // broadcast add
Remarks
Port of NumPy 2.x numpy.ogrid (the sparse=True instance of
numpy.lib.index_tricks.nd_grid; its getitem's statements are
ogrid(object[], string)). It shares the
slice-expression grammar of r_: C# has no a[1:5:2] literal, so a slice is written
as a string — np.ogrid["0:5"] ≙ np.ogrid[0:5] — and one string may carry several
comma-separated slices (np.ogrid["0:3, 0:5"]). Slice objects work as
entries too. Unlike r_ there are NO leading directives: every entry is a slice. A field
written as digits is a Python int (exact at any size), anything else a Python float.
Dtype. All the slices of a multi-slice mesh share ONE dtype — int64 when every field of
every slice is a Python int, else float64 (an imaginary step counts as a float). This mirrors
NumPy's single result_type(*num_list) over all the slice bounds: e.g.
np.ogrid["0:2", "0.0:2"] gives two float64 arrays, not one int64 and one float64.
A single-slice result keeps the literal-driven dtype of r_'s slice branch.
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 line is
arange(size) * step + start in the mesh dtype, with the size ceil((stop - start) / step)
in Python's arithmetic: a negative size is an empty axis, an underflowed quotient a size of 0, and a
float step's points are NOT arange's start + i * delta.
Missing stop. A single slice may omit its stop (np.ogrid["5:"] is
arange(0, 5), exactly like r_), except with an imaginary step of more than one
point: Python's TypeError for None - start, as in NumPy. A multi-slice mesh needs
every stop but that of an imaginary step of one point: NumPy leaks an AttributeError when it is
absent, so NumSharp raises a ValueError naming the slice, at the same point.
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 NaN, infinite or past-int64 size raises NumPy's text (cannot convert float NaN to
integer, cannot convert float infinity to integer, Maximum allowed size exceeded).
Return. The np.OGridResult stands in for NumPy's array-or-tuple: a
single-slice result converts implicitly to a bare NDArray; a multi-slice result
converts to NDArray[], deconstructs, or indexes.
https://numpy.org/doc/stable/reference/generated/numpy.ogrid.html
Properties
this[object[]]
Expands the slice expression into an open mesh. See np.OGridClass for how a Python slice literal is spelled in C#.
public np.OGridResult 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.