Table of Contents

Class Vectorized

Namespace
NumSharp
Assembly
NumSharp.dll

A reusable vectorized wrapper around a C# delegate — the object vectorize(Delegate, DType[]) and frompyfunc(Delegate, int, int) return, mirroring NumPy's vectorize class. Build it once and call it many times: an element-wise instance re-uses one compiled fused kernel across calls (the delegate identity is stable, so evaluate(NDExpr, NDArray[], NDArray, NDArray, DType, string, char)'s structural cache reuses it), so repeated invocation is cheap.

public sealed class Vectorized
Inheritance
Vectorized
Inherited Members

Remarks

Two modes, fixed at construction by whether a gufunc signature was supplied: element-wise (no signature) broadcasts the inputs and applies the delegate per element via a single fused Call(Delegate, params NDExpr[]) pass; signature mode applies the delegate to core sub-arrays, iterating the broadcast shape (one call per index, not per element).

Footgun — multi-output element-wise calls the delegate once PER OUTPUT. A delegate returning a ValueTuple of N components is evaluated as N independent fused passes (an expression-built adapter per component), so the user delegate runs N times per element rather than once. This is identical in result to NumPy for a PURE function — which vectorize already assumes (NumPy itself calls the function an extra time merely to infer the output dtype) — but a delegate with side effects will observe the extra calls. Prefer nditer<T>(NDArray, bool, char) for single-call multi-output over a hot, impure, or expensive function.

Ownership (signature mode). The core sub-array views handed to the delegate and every array the delegate returns stay the CALLER's: each result is copied into the output and never disposed, because a delegate may return an array it keeps using (the core view itself, a held constant). A delegate that ALLOCATES a fresh result per core slice therefore leaves those to a GC — run the call inside an NDScope (yielding the outputs) when deterministic release matters. The wrapper's own broadcast views are released before the call returns.

Properties

nin

The number of input arrays this callable consumes (the delegate's parameter count).

public int nin { get; }

Property Value

int

nout

The number of output arrays this callable produces. For an element-wise callable this is the delegate's return component count (1, or the arity of the returned ValueTuple); for a signature callable it is the signature's output-argument count. Use CallMany(params NDArray[]) when this exceeds 1.

public int nout { get; }

Property Value

int

signature

The gufunc signature string this callable was built with, or null for element-wise mode.

public string signature { get; }

Property Value

string

Methods

Call(params NDArray[])

Applies the wrapper and returns the single output array. The common case: element-wise application over broadcast args, or one gufunc pass in signature mode.

public NDArray Call(params NDArray[] args)

Parameters

args NDArray[]

The input arrays, one per nin. C# scalars convert implicitly to 0-d arrays, so vf.Call(a, 2) broadcasts the scalar as NumPy's vfunc(a, 2) does.

Returns

NDArray

The result array. In element-wise mode its dtype is the delegate's return type (or otypes[0] if given) and its shape is the broadcast of the inputs; in signature mode its shape is the broadcast of the non-core dimensions plus the output core dimensions.

Exceptions

ArgumentNullException

args or any element is null.

ArgumentException

args length is not nin.

InvalidOperationException

This callable produces more than one output — use CallMany(params NDArray[]) instead.

IncorrectShapeException

The inputs do not broadcast together (message lists every operand shape, as NumPy does).

CallMany(params NDArray[])

Applies the wrapper and returns ALL outputs, in the delegate's tuple / signature order. Use this for a multi-output callable (nout > 1); it also works for a single-output callable (returns a length-1 array).

public NDArray[] CallMany(params NDArray[] args)

Parameters

args NDArray[]

The input arrays, one per nin (C# scalars convert implicitly to 0-d arrays).

Returns

NDArray[]

One array per output. Element-wise multi-output invokes the delegate once per output (see the class remarks) — identical to NumPy for a pure function.

Exceptions

ArgumentNullException

args or any element is null.

ArgumentException

args length is not nin.

IncorrectShapeException

The inputs do not broadcast together.

ValueError

Signature mode: the delegate returned a different number of outputs than the signature declares.