Exemplo n.º 1
0
        public void For_IntInt_ReturnScalarPrimitivesIntInt()
        {
            // arrange & action
            var op = ScalarPrimitivesRegistry.For <int, int>();

            // assert
            Assert.IsInstanceOfType(op, typeof(ScalarPrimitives <int, int>));
        }
        /// <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)));
        }
Exemplo n.º 4
0
        private static NdArray <bool> IsCloseWithoutTolerence <P>(NdArray <P> lhs, NdArray <P> rhs)
        {
            if (typeof(P) == typeof(double) || typeof(P) == typeof(float))
            {
                var op = ScalarPrimitivesRegistry.For <P, double>();
                P   absoluteTolerenceT = op.Convert(1e-8);
                P   relativeTolerenceT = op.Convert(1e-5);

                return(IsCloseWithTolerence(lhs, rhs, absoluteTolerenceT, relativeTolerenceT));
            }

            return(lhs == rhs);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Creates a new NdArray filled with equaly spaced values using a specifed increment.
        /// </summary>
        /// <param name="device">The device to create the NdArray on.</param>
        /// <param name="start">The starting value.</param>
        /// <param name="stop">The end value, which is not included.</param>
        /// <param name="step">The increment between successive element.</param>
        /// <typeparam name="T">The new NdArray.</typeparam>
        public static NdArray <T> Arange(IDevice device, T start, T stop, T step)
        {
            var op  = ScalarPrimitivesRegistry.For <T, T>();
            var opc = ScalarPrimitivesRegistry.For <int, T>();

            var numberOfElementT   = op.Divide(op.Subtract(stop, start), step);
            var numberOfElementInt = opc.Convert(numberOfElementT);
            var numberOfElement    = Math.Max(0, numberOfElementInt);

            var shape = new[] { numberOfElement };

            var newArray = new NdArray <T>(shape, device);

            newArray.FillIncrementing(start, step);

            return(newArray);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Creates a new NdArray of given size filled with equaly spaced values.
        /// </summary>
        /// <param name="device">The device to create the NdArray on.</param>
        /// <param name="start">The starting value.</param>
        /// <param name="stop">The end value, which is not included.</param>
        /// <param name="numElement">The size of the vector.</param>
        /// <returns>The new NdArray.</returns>
        public static NdArray <T> Linspace(IDevice device, T start, T stop, int numElement)
        {
            if (numElement < 2)
            {
                throw new ArgumentException("Linspace requires at least two elements.", "numElement");
            }

            var op = ScalarPrimitivesRegistry.For <T, int>();

            var numElementT = op.Convert(numElement - 1);
            var increment   = op.Divide(op.Subtract(stop, start), numElementT);

            var newArray = new NdArray <T>(new[] { numElement }, device);

            newArray.FillIncrementing(start, increment);

            return(newArray);
        }