/// <summary> /// Calculates the mean of the elements along the specified axis /// </summary> /// <param name="axis">The axis to operate along.</param> /// <param name="source">The NdArray containing the source values.</param> /// <returns>A new NdArray containing the result of this operation.</returns> public static NdArray <T> MeanAxis(int axis, NdArray <T> source) { var sp = ScalarPrimitivesRegistry.For <T, int>(); var sum = SumAxis(axis, source); var scalar = NdArray <T> .ScalarLike(source, sp.Convert(source.Shape[axis])); return(sum / scalar); }
/// <summary> /// Calculates the variance of the elements along the specified axis. /// </summary> /// <param name="axis">The axis to operate along.</param> /// <param name="source">The NdArray containing the source values.</param> /// <param name="ddof">The delta degrees of freedom.</param> /// <returns>A new NdArray containing the result of this operation.</returns> public static NdArray <T> VarAxis(int axis, NdArray <T> source, T deltaDegreeOfFreedom) { var sp = ScalarPrimitivesRegistry.For <T, int>(); var spc = ScalarPrimitivesRegistry.For <int, T>(); var mean = NdArray <T> .InsertAxis(axis, NdArray <T> .MeanAxis(axis, source)); var v = source - mean; var n = sp.Convert(source.Shape[axis] - spc.Convert(deltaDegreeOfFreedom)); return(SumAxis(axis, (v * v) / NdArray <T> .ScalarLike(source, n))); }
private static NdArray <bool> IsCloseWithTolerence <P>(NdArray <P> lhs, NdArray <P> rhs, P absoluteTolerence, P relativeTolerence) { var absoluteTolerenceScalar = NdArray <P> .ScalarLike(lhs, absoluteTolerence); var relativeTolerenceScalar = NdArray <P> .ScalarLike(lhs, relativeTolerence); /// NOTE This is not symmetric. /// https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.isclose.html var absDiff = NdArray <P> .Abs(lhs - rhs); var absRhs = NdArray <P> .Abs(rhs); return(absDiff <= absoluteTolerenceScalar + (relativeTolerenceScalar * absRhs)); }