Table of Contents

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

nint

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

indices long[]

One index per dimension (length must equal ndim).

Property Value

object

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

long

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

bool

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

bool

f_contiguous

true if the array is Fortran-contiguous (NumPy's memoryview.f_contiguous).

public bool f_contiguous { get; }

Property Value

bool

format

The struct-module type code describing one element (NumPy's memoryview.format), e.g. "d" for float64, "?" for bool, "Zd" for complex128. The integer codes are platform-dependent exactly as NumPy's are, because they are the C type each dtype is on the host: int32 is 'l' (the C long) on Windows and 'i' on LP64, int64 is 'q' on Windows and 'l' on LP64; NumPy's C-type classes keep their own codes (np.intc 'i', np.longlong 'q', np.longdouble 'g', np.clongdouble "Zg"). 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". A fixed-width string array reports NumPy's character-count codes: "3w" for <U3 (three UCS-4 code points) and "2s" for |S2 (two bytes) — the struct codes NumPy's buffer.c emits.

public string format { get; }

Property Value

string

itemsize

Size of one element in bytes (NumPy's memoryview.itemsize).

public int itemsize { get; }

Property Value

int

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

long

ndim

Number of dimensions (NumPy's memoryview.ndim); 0 for a scalar array.

public int ndim { get; }

Property Value

int

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

NDArray

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

bool

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), as NumPy's buffer export writes them (_buffer_info_new in buffer.c): a C-contiguous array reports the C strides of its shape, an F-contiguous one (not also C) the F strides of its shape, any other array its own strides scaled from NumSharp's elements to bytes. A fresh array per read.

public long[] strides { get; }

Property Value

long[]

Remarks

The recomputation is what callers see on two kinds of array, where the memoryview's strides are NOT the array's strides: a zero-size array NumPy allocated (all-zero strides, while np.empty((0, 3)).data.strides is (24, 8) and np.zeros((2, 0, 3)).data.strides (0, 24, 8): the zero extents are multiplied in) and a contiguous array whose extent-1 axis carries an arbitrary stride (contiguity ignores it; the export reports the canonical one). An empty F array is also C-contiguous, so it reports C strides, as NumPy's memoryview(a) request (PyBUF_FULL_RO, no PyBUF_F_CONTIGUOUS) does. A broadcast axis keeps its 0 stride and a reversed view its negative stride (neither array is contiguous).

Methods

ToString()

An informative debug string (NumPy's memoryview repr is the opaque <memory at 0x…>).

public override string ToString()

Returns

string

hex()

The array's bytes as a lower-case hex string (NumPy's memoryview.hex()), in logical C-order. Equivalent to hex-encoding tobytes(string).

public string hex()

Returns

string

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

order string

"C" (default), "F", or "A", upper case only; null is Python's None, which means "C".

Returns

byte[]

Exceptions

ValueError

If order is anything else (CPython's memory_tobytes text: order must be 'C', 'F' or 'A').

NotSupportedException

If the byte count exceeds MaxValue (a byte[] limit, not a format one).