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
indexint0 or 1.
Property Value
- NDArray
The output.
Exceptions
- IndexOutOfRangeException
indexis not below Length.
Length
Number of present outputs: 1, or 2 with returned (NumPy's tuple length).
public int Length { get; }
Property Value
Shape
public Shape Shape { get; }
Property Value
dtype
The descriptor of values.
public DType dtype { get; }
Property Value
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
ndim
Rank of values (0 over every axis).
public int ndim { get; }
Property Value
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
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
typecode
The storage lane of values.
public NPTypeCode typecode { get; }
Property Value
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
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
Exceptions
- InvalidOperationException
The result holds the average alone (
returnedwas false): Python'snot 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
indexlongThe 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
indexlongThe flat index (C order); a negative one counts from the end.
Returns
- T
The element as
T.
Type Parameters
TThe type to read the element as.
Exceptions
- IndexError
indexis outside the average.
GetDouble(params long[])
public double GetDouble(params long[] indices)
Parameters
indiceslong[]The coordinates.
Returns
- double
The element read as a double.
GetSingle(params long[])
public float GetSingle(params long[] indices)
Parameters
indiceslong[]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
TThe 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
rhsNDArrayThe array to compare with.
Returns
- bool
Whether the shapes and elements are equal.
astype(DType, char, string, bool)
values cast to dtype
(astype(DType, char, string, bool)).
public NDArray astype(DType dtype, char order = 'K', string casting = "unsafe", bool copy = true)
Parameters
dtypeDTypeThe target dtype.
ordercharThe memory order of a copy ('K' keeps the average's).
castingstringThe casting rule ('unsafe' by default, as NumPy's astype).
copyboolWhether 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
shapelong[]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
resultnp.AverageResultThe 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
resultnp.AverageResultThe 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'sfloat()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
resultnp.AverageResultThe 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
resultnp.AverageResultThe 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
resultnp.AverageResultThe 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
resultnp.AverageResultThe result.
Returns
implicit operator NDArray[](AverageResult)
Tuple-return conversion: the present outputs, NumPy's order.
public static implicit operator NDArray[](np.AverageResult result)
Parameters
resultnp.AverageResultThe result.
Returns
- NDArray[]
A new array of the outputs.