Class np.MemoryView
- Namespace
- NumSharp
- Assembly
- NumSharp.dll
The NumSharp analog of Python's memoryview — the buffer object returned by
data. It is a lightweight, zero-copy HANDLE onto an array's raw
memory plus the layout metadata needed to interpret it (NumPy's ndarray.data is
literally memoryview(self)).
It owns no memory of its own: every member reads LIVE through the source array's
Storage and Shape, so it stays valid while
the source array (held as obj, keeping it alive exactly as NumPy's
memoryview.obj does) is alive and not structurally mutated. The
Pointer addresses the LOGICAL first element (base + offset·itemsize),
matching NumPy's a.data / PyArray_DATA / a.ctypes.data /
a.__array_interface__['data'][0] — for a sliced or reversed view this is the offset
element, not the buffer base.
Surface (probed against NumPy 2.4.2's memoryview): obj,
nbytes, itemsize, ndim, readonly
(true for broadcast / non-writeable views), shape, strides
(in BYTES like NumPy's — NumSharp's own strides are in elements), format
(the struct-module type code), c_contiguous / f_contiguous /
contiguous, Length (NumPy's len(mv)),
tobytes(string) / hex(), the raw Pointer /
Address, and write-through scalar element access via
this[long[]].
Deliberately NOT modelled (documented boundaries, not gaps to close): partial-index
sub-views (mv[1] on an N-D memoryview yields a sub-memoryview in Python —
use the NDArray indexer for sub-arrays), cast, tolist,
release / the context-manager protocol, and element iteration. NumSharp has no
Python buffer protocol, so these Python-object conveniences have no counterpart; the
buffer ESSENCE (pointer + metadata + write-through + tobytes) is what
ndarray.data is for.
public sealed class np.MemoryView
- Inheritance
-
np.MemoryView
- Inherited Members
Remarks
Properties
Address
Pointer as an nint — equal to NumPy's
a.ctypes.data / a.array_interface['data'][0]. Convenient for P/Invoke
and native interop.
public nint Address { get; }
Property Value
this[long[]]
Write-through scalar element access (NumPy's memoryview full-index element get/set).
Supply exactly one index per dimension (a 0-d array takes a length-0 index array);
negative indices count from the end. Reads/writes go through the array's stride-aware
accessors, so they honour any layout and write THROUGH to the base — a strided /
transposed / reversed / offset view included. Partial indexing (fewer indices than
ndim, which yields a sub-memoryview in Python) is not modelled;
use the NDArray indexer for sub-arrays.
public object this[params long[] indices] { get; set; }
Parameters
Property Value
Exceptions
- ArgumentException
If
indices's length does not equal ndim.- IndexOutOfRangeException
If any (normalized) index is out of bounds.
- InvalidOperationException
On set, if this view is readonly.
Length
The size of the FIRST dimension (NumPy's len(memoryview)). Throws for a 0-d
array, matching NumPy's TypeError: len() of unsized object.
public long Length { get; }
Property Value
Pointer
Raw pointer to the LOGICAL first byte of the array's data
(Storage.Address + offset·itemsize) — the C-level analog of NumPy's
a.data pointer / PyArray_DATA(self). Reads and writes through it hit the
underlying buffer directly (subject to readonly).
public void* Pointer { get; }
Property Value
- void*
c_contiguous
true if the array is C-contiguous (NumPy's memoryview.c_contiguous).
public bool c_contiguous { get; }
Property Value
contiguous
true if the array is contiguous in EITHER C or Fortran order (NumPy's
memoryview.contiguous ≡ PyBuffer_IsContiguous(view, 'A')).
public bool contiguous { get; }
Property Value
f_contiguous
true if the array is Fortran-contiguous (NumPy's memoryview.f_contiguous).
public bool f_contiguous { get; }
Property Value
format
The struct-module type code describing one element (NumPy's memoryview.format),
e.g. "d" for float64, "?" for bool, "Zd" for complex128. The 32-bit
integer codes are platform-dependent exactly as NumPy's are (its C long maps to
'l'/'L' on Windows LLP64 but 'i'/'I' on LP64), because
NumPy's int32 dtype is NPY_LONG on Windows and NPY_INT elsewhere. The two
NumSharp-only dtypes that have NO NumPy analog carry a documented NumSharp-specific
code: Char (2-byte UTF-16) → "u",
Decimal (opaque 16-byte item) → "16s".
public string format { get; }
Property Value
itemsize
Size of one element in bytes (NumPy's memoryview.itemsize).
public int itemsize { get; }
Property Value
nbytes
Total LOGICAL byte count = product(shape)·itemsize (NumPy's
memoryview.nbytes). A broadcast view reports its logical size, a 0-d array one
itemsize, an empty array 0.
public long nbytes { get; }
Property Value
ndim
Number of dimensions (NumPy's memoryview.ndim); 0 for a scalar array.
public int ndim { get; }
Property Value
obj
The underlying array this view exposes (NumPy's memoryview.obj). Holding a
np.MemoryView keeps this array reachable, mirroring how a Python
memoryview keeps its exporter alive.
public NDArray obj { get; }
Property Value
readonly
true when the underlying memory may not be written (NumPy's
memoryview.readonly) — set for broadcast views, which NumSharp (like NumPy)
marks non-writeable. Named to match NumPy; because readonly is a C# keyword,
access it as mv.@readonly.
public bool @readonly { get; }
Property Value
shape
The array's shape (NumPy's memoryview.shape). A fresh array per read.
public long[] shape { get; }
Property Value
- long[]
strides
Strides in BYTES (NumPy's memoryview.strides). NumSharp stores strides in
ELEMENTS, so each is scaled by itemsize here — a broadcast axis keeps its
0 stride, a reversed view its negative stride. A fresh array per read.
public long[] strides { get; }
Property Value
- long[]
Methods
ToString()
An informative debug string (NumPy's memoryview repr is the opaque <memory at 0x…>).
public override string ToString()
Returns
hex()
The array's bytes as a lower-case hex string (NumPy's memoryview.hex()), in
logical C-order. Equivalent to hex-encoding tobytes().
public string hex()
Returns
tobytes(string)
Copies the array's elements into a fresh byte[] in the requested logical order
(NumPy's memoryview.tobytes(order='C')): 'C' row-major (default),
'F' column-major, 'A' = the physical order if the array is already
contiguous (Fortran order when Fortran-contiguous, else C). Follows strides, so a
strided / reversed / transposed view is materialized in logical order — matching
bytes(a.data).
public byte[] tobytes(string order = "C")
Parameters
orderstring"C"(default),"F", or"A".
Returns
- byte[]
Exceptions
- ArgumentException
If
orderis not one of C/F/A.- NotSupportedException
If the byte count exceeds MaxValue (a
byte[]limit, not a format one).