Table of Contents

Struct np.AverageResult

Namespace
NumSharp
Assembly
NumSharp.dll
The result of <xref href="NumSharp.np.average(NumSharp.NDArray%2cSystem.Nullable%7bNumSharp.NDAxis%7d%2cNumSharp.NDArray%2cSystem.Boolean%2cSystem.Boolean)" data-throw-if-not-resolved="false"></xref> — NumPy's bare-average-OR-tuple
return expressed as one type (np.unique's <xref href="NumSharp.np.UniqueResult" data-throw-if-not-resolved="false"></xref> pattern): the average
(<xref href="NumSharp.np.AverageResult.values" data-throw-if-not-resolved="false"></xref>), plus <xref href="NumSharp.np.AverageResult.sum_of_weights" data-throw-if-not-resolved="false"></xref> when <code>returned</code> was set (null otherwise).

It stands in for both NumPy return shapes: it converts implicitly to NDArray (the bare form: NDArray avg = np.average(a);) and to NDArray[] (the present outputs), and deconstructs: var (avg, sum_of_weights) = np.average(a, returned: true);.

public readonly struct np.AverageResult : INDArrayCarrier, IDisposable
Implements
Inherited Members

Remarks

API-parity batch 11 (the owner's decision 3, result structs for NumPy's flag-dependent returns): it replaces np.average's NDArray return and the NumSharp-only np.average_returned. The NDArray members callers read on an average forward to values (size, shape, dtype, flags, the typed getters, ToArray<T>, reshape, array_equal, astype), and the explicit scalar casts of every dtype an average can have ((double)np.average(a), NumPy's float(np.average(a))) read a 0-d average; for anything else read values or assign the result to an NDArray.

It is IDisposable as the NDArray it replaced was: using var avg = np.average(a); releases the average and the sum of weights together.

Footgun: an extension method of NDArray (FluentAssertions' Should()) does not see through the implicit conversion - call it on values. C# applies one user-defined conversion at a time, so a cast the result does not declare ((int)np.average(a)) needs values: (int)np.average(a).values.

Properties

this[int]

The index-th present output: 0 the average, 1 the sum of weights.

public NDArray this[int index] { get; }

Parameters

index int

0 or 1.

Property Value

NDArray

The output.

Exceptions

IndexOutOfRangeException

index is not below Length.

Length

Number of present outputs: 1, or 2 with returned (NumPy's tuple length).

public int Length { get; }

Property Value

int

Shape

The Shape of values.

public Shape Shape { get; }

Property Value

Shape

dtype

The descriptor of values.

public DType dtype { get; }

Property Value

DType

flags

The flags of values (flags): an average is a fresh array, so owndata holds even for the average over no axis.

public NDArrayFlags flags { get; }

Property Value

NDArrayFlags

ndim

Rank of values (0 over every axis).

public int ndim { get; }

Property Value

int

shape

Shape of values as a long[].

public long[] shape { get; }

Property Value

long[]

size

Element count of values.

public long size { get; }

Property Value

long

sum_of_weights

The sum of the weights in the average's dtype and shape (the element count per average without weights), or null when returned was false.

public NDArray sum_of_weights { get; }

Property Value

NDArray

typecode

The storage lane of values.

public NPTypeCode typecode { get; }

Property Value

NPTypeCode

values

The average: float64 for an integer or bool operand, else the common dtype of the operand and the weights; 0-d over every axis; a matrix operand's class as its rule gives it. Always present.

public NDArray values { get; }

Property Value

NDArray

Methods

Deconstruct(out NDArray, out NDArray)

Deconstructs a returned=True result: var (avg, sum_of_weights) = ....

public void Deconstruct(out NDArray values, out NDArray sum_of_weights)

Parameters

values NDArray

The average.

sum_of_weights NDArray

The sum of weights.

Exceptions

InvalidOperationException

The result holds the average alone (returned was false): Python's not enough values to unpack (expected 2, got 1).

Dispose()

Releases the average and the sum of weights (the NDArray it replaced was disposable the same way). Footgun: an NDArray taken from the result (the implicit conversion, an indexer, a deconstruction) is the same object, so it is released too.

public void Dispose()

GetAtIndex(long)

The element of values at a flat index, boxed in its own type.

public object GetAtIndex(long index)

Parameters

index long

The flat index.

Returns

object

The element.

GetAtIndex<T>(long)

The element of values at a flat index, read as T (the stored value for the average's own type, NumPy's cast of it otherwise).

public T GetAtIndex<T>(long index) where T : unmanaged

Parameters

index long

The flat index (C order); a negative one counts from the end.

Returns

T

The element as T.

Type Parameters

T

The type to read the element as.

Exceptions

IndexError

index is outside the average.

GetDouble(params long[])

The double value of values at the given coordinates (none for a 0-d average).

public double GetDouble(params long[] indices)

Parameters

indices long[]

The coordinates.

Returns

double

The element read as a double.

GetSingle(params long[])

The float value of values at the given coordinates.

public float GetSingle(params long[] indices)

Parameters

indices long[]

The coordinates.

Returns

float

The element read as a float.

ToArray()

The present outputs as an NDArray[], NumPy's order.

public NDArray[] ToArray()

Returns

NDArray[]

A new array of one or two outputs.

ToArray<T>()

The elements of values as a new T[] in C order.

public T[] ToArray<T>() where T : unmanaged

Returns

T[]

A new managed array.

Type Parameters

T

The element type, as ToArray<T>() reads it.

array_equal(NDArray)

True if values equals rhs element-wise (same shape).

public bool array_equal(NDArray rhs)

Parameters

rhs NDArray

The array to compare with.

Returns

bool

Whether the shapes and elements are equal.

astype(DType, char, string, bool)

public NDArray astype(DType dtype, char order = 'K', string casting = "unsafe", bool copy = true)

Parameters

dtype DType

The target dtype.

order char

The memory order of a copy ('K' keeps the average's).

casting string

The casting rule ('unsafe' by default, as NumPy's astype).

copy bool

Whether to copy when no cast is needed.

Returns

NDArray

The cast average.

Exceptions

InvalidCastException

The cast is refused under casting.

reshape(params long[])

values with a new shape (reshape(params long[]): a view where one exists).

public NDArray reshape(params long[] shape)

Parameters

shape long[]

The new dimensions.

Returns

NDArray

The reshaped average.

Exceptions

IncorrectShapeException

The size does not match.

Operators

explicit operator decimal(AverageResult)

The 0-d average as a decimal (NumSharp's lane). Footgun: a NaN or infinite float average converts to 0, NumSharp's decimal convention, not an error.

public static explicit operator decimal(np.AverageResult result)

Parameters

result np.AverageResult

The result; only values is read.

Returns

decimal

The average.

Exceptions

TypeError

The average is not 0-d, or it is complex.

explicit operator double(AverageResult)

NumPy's float(np.average(a)): the 0-d average as a double.

public static explicit operator double(np.AverageResult result)

Parameters

result np.AverageResult

The result; only values is read.

Returns

double

The average.

Exceptions

TypeError

The average is not 0-d (only 0-dimensional arrays can be converted to Python scalars), or it is complex (CPython's float() text).

explicit operator Half(AverageResult)

The 0-d average as a Half (NumPy's float() rounded to float16).

public static explicit operator Half(np.AverageResult result)

Parameters

result np.AverageResult

The result; only values is read.

Returns

Half

The average.

Exceptions

TypeError

The average is not 0-d, or it is complex.

explicit operator Complex(AverageResult)

NumPy's complex(np.average(a)): the 0-d average as a Complex (a real average gets a zero imaginary part).

public static explicit operator Complex(np.AverageResult result)

Parameters

result np.AverageResult

The result; only values is read.

Returns

Complex

The average.

Exceptions

TypeError

The average is not 0-d.

explicit operator float(AverageResult)

The 0-d average as a float (NumPy's float() rounded to float32).

public static explicit operator float(np.AverageResult result)

Parameters

result np.AverageResult

The result; only values is read.

Returns

float

The average.

Exceptions

TypeError

The average is not 0-d, or it is complex.

implicit operator NDArray(AverageResult)

Bare-return conversion: the average (NumPy's single-array return).

public static implicit operator NDArray(np.AverageResult result)

Parameters

result np.AverageResult

The result.

Returns

NDArray

values.

implicit operator NDArray[](AverageResult)

Tuple-return conversion: the present outputs, NumPy's order.

public static implicit operator NDArray[](np.AverageResult result)

Parameters

result np.AverageResult

The result.

Returns

NDArray[]

A new array of the outputs.