Ejemplo n.º 1
0
        public NDArray matrix_power(int power)
        {
            if (power < 0)
            {
                throw new Exception("matrix_power just work with int >= 0");
            }

            NDArray product = this.copy();

            for (int idx = 2; idx <= power; idx++)
            {
                product = TensorEngine.Dot(product, this);
            }

            product = (power == 0) ? np.eye(product.shape[0]) : product;

            return(product);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Negates all positive values.
        /// </summary>
        public NDArray negative()
        {
            if (this.size == 0)
            {
                return(this.Clone());
            }

            var @out = TensorEngine.Cast(this, dtype ?? this.dtype, copy: true);

            var len = @out.size;

            unsafe
            {
                switch (@out.GetTypeCode)
                {
                case NPTypeCode.Boolean:
                {
                    var out_addr = (bool *)@out.Address;
                    var addr     = (bool *)@out.Address;
                    for (int i = 0; i < len; i++)
                    {
                        out_addr[i] = !addr[i];
                    }
                    return(@out);
                }

#if _REGEN
                    % foreach supported_numericals_signed, supported_numericals_signed_lowercase %
                case NPTypeCode.#1:
                {
                    var out_addr = (#2 *)@out.Address;

                    for (int i = 0; i < len; i++)
                    {
                        var val = out_addr[i];
                        if (val > 0)
                        {
                            out_addr[i] = -val;
                        }
                    }
                    return(@out);
                }
Ejemplo n.º 3
0
 /// <summary>
 /// Constructor for init data type
 /// internal storage is 1D with 1 element
 /// </summary>
 /// <param name="dtype">Data type of elements</param>
 /// <param name="engine">The engine of this <see cref="NDArray"/></param>
 /// <remarks>This constructor does not call allocation/></remarks>
 protected internal NDArray(Type dtype, TensorEngine engine)
 {
     tensorEngine = engine;
     Storage = TensorEngine.GetStorage(dtype);
 }
Ejemplo n.º 4
0
 /// <summary>
 ///     Creates a new <see cref="NDArray"/> with this storage.
 /// </summary>
 /// <param name="shape">The shape to set for this NDArray, does not perform checks.</param>
 /// <remarks>Doesn't copy. Does not perform checks for <paramref name="shape"/>.</remarks>
 protected internal NDArray(UnmanagedStorage storage, ref Shape shape)
 {
     Storage = storage.Alias(ref shape);
     tensorEngine = storage.Engine;
 }
Ejemplo n.º 5
0
 /// <summary>
 ///     Creates a new <see cref="NDArray"/> with this storage.
 /// </summary>
 /// <param name="storage"></param>
 public NDArray(UnmanagedStorage storage)
 {
     Storage = storage;
     tensorEngine = storage.Engine;
 }
Ejemplo n.º 6
0
 public NDArray amin(Type dtype = null)
 {
     return(TensorEngine.AMin(this, null, dtype?.GetTypeCode(), false));
 }
Ejemplo n.º 7
0
 public NDArray amin(int axis, bool keepdims = false, Type dtype = null)
 {
     return(TensorEngine.AMin(this, axis, dtype, keepdims));
 }
Ejemplo n.º 8
0
 /// <summary>
 ///     Sum of array elements over a given axis.
 /// </summary>
 /// <param name="axis">Axis or axes along which a sum is performed. The default, axis=None, will sum all of the elements of the input array. If axis is negative it counts from the last to the first axis.</param>
 /// <param name="typeCode">The type of the returned array and of the accumulator in which the elements are summed. The dtype of a is used by default unless a has an integer dtype of less precision than the default platform integer. In that case, if a is signed then the platform integer is used while if a is unsigned then an unsigned integer of the same precision as the platform integer is used.</param>
 /// <param name="keepdims">If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array. If the default value is passed, then keepdims will not be passed through to the sum method of sub-classes of ndarray, however any non-default value will be.If the sub-class’ method does not implement keepdims any exceptions will be raised.</param>
 /// <returns>An array with the same shape as a, with the specified axis removed. If a is a 0-d array, or if axis is None, a scalar is returned. If an output array is specified, a reference to out is returned.</returns>
 /// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.sum.html</remarks>
 public NDArray sum(int axis, bool keepdims, NPTypeCode?typeCode = null)
 {
     return(TensorEngine.Sum(this, axis, typeCode, keepdims));
 }
Ejemplo n.º 9
0
 /// <summary>
 ///     Compute the arithmetic mean along the specified axis.
 ///     Returns the average of the array elements.
 ///     The average is taken over the flattened array by default, otherwise over the specified axis.
 ///     float64 intermediate and return values are used for integer inputs.
 /// </summary>
 /// <param name="nd">Array containing numbers whose mean is desired. If a is not an array, a conversion is attempted.</param>
 /// <param name="axis">Axis or axes along which the means are computed. The default is to compute the mean of the flattened array.</param>
 /// <param name="type">Type to use in computing the mean. For integer inputs, the default is float64; for floating point inputs, it is the same as the input dtype.</param>
 /// <param name="keepdims">
 ///     If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
 ///     If the default value is passed, then keepdims will not be passed through to the mean method of sub-classes of ndarray, however any non-default value will be.If the sub-class’ method does not implement keepdims any exceptions will be raised.
 /// </param>
 /// <returns> returns a new array containing the mean values, otherwise a reference to the output array is returned.</returns>
 public NDArray mean(int axis, bool keepdims)
 => TensorEngine.Mean(this, axis, null, keepdims);
Ejemplo n.º 10
0
 /// <summary>
 ///     Compute the arithmetic mean along the specified axis.
 ///     Returns the average of the array elements.
 ///     The average is taken over the flattened array by default, otherwise over the specified axis.
 ///     float64 intermediate and return values are used for integer inputs.
 /// </summary>
 /// <param name="nd">Array containing numbers whose mean is desired. If a is not an array, a conversion is attempted.</param>
 /// <param name="axis">Axis or axes along which the means are computed. The default is to compute the mean of the flattened array.</param>
 /// <param name="type">Type to use in computing the mean. For integer inputs, the default is float64; for floating point inputs, it is the same as the input dtype.</param>
 /// <param name="keepdims">
 ///     If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
 ///     If the default value is passed, then keepdims will not be passed through to the mean method of sub-classes of ndarray, however any non-default value will be.If the sub-class’ method does not implement keepdims any exceptions will be raised.
 /// </param>
 /// <returns> returns a new array containing the mean values, otherwise a reference to the output array is returned.</returns>
 /// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.mean.html</remarks>
 public NDArray mean(int axis, NPTypeCode type, bool keepdims = false)
 => TensorEngine.Mean(this, axis, type, keepdims);
Ejemplo n.º 11
0
 /// <summary>
 ///     Compute the arithmetic mean along the specified axis.
 ///     Returns the average of the array elements.
 ///     The average is taken over the flattened array by default, otherwise over the specified axis.
 ///     float64 intermediate and return values are used for integer inputs.
 /// </summary>
 /// <param name="nd">Array containing numbers whose mean is desired. If a is not an array, a conversion is attempted.</param>
 /// <param name="axis">Axis or axes along which the means are computed. The default is to compute the mean of the flattened array.</param>
 /// <param name="type">Type to use in computing the mean. For integer inputs, the default is float64; for floating point inputs, it is the same as the input dtype.</param>
 /// <param name="keepdims">
 ///     If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
 ///     If the default value is passed, then keepdims will not be passed through to the mean method of sub-classes of ndarray, however any non-default value will be.If the sub-class’ method does not implement keepdims any exceptions will be raised.
 /// </param>
 /// <returns> returns a new array containing the mean values, otherwise a reference to the output array is returned.</returns>
 /// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.mean.html</remarks>
 public NDArray mean(int axis)
 => TensorEngine.Mean(this, axis);
Ejemplo n.º 12
0
 /// <summary>
 ///     Compute the arithmetic mean along the specified axis.
 ///     Returns the average of the array elements.
 ///     The average is taken over the flattened array by default, otherwise over the specified axis.
 ///     float64 intermediate and return values are used for integer inputs.
 /// </summary>
 /// <param name="nd">Array containing numbers whose mean is desired. If a is not an array, a conversion is attempted.</param>
 /// <param name="axis">Axis or axes along which the means are computed. The default is to compute the mean of the flattened array.</param>
 /// <param name="type">Type to use in computing the mean. For integer inputs, the default is float64; for floating point inputs, it is the same as the input dtype.</param>
 /// <param name="keepdims">
 ///     If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
 ///     If the default value is passed, then keepdims will not be passed through to the mean method of sub-classes of ndarray, however any non-default value will be.If the sub-class’ method does not implement keepdims any exceptions will be raised.
 /// </param>
 /// <returns> returns a new array containing the mean values, otherwise a reference to the output array is returned.</returns>
 /// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.mean.html</remarks>
 public NDArray mean()
 => TensorEngine.Mean(this);
Ejemplo n.º 13
0
 /// <summary>
 ///     Creates a new <see cref="NDArray"/> with this storage.
 /// </summary>
 /// <param name="storage"></param>
 protected internal NDArray(UnmanagedStorage storage)
 {
     Storage = storage;
     tensorEngine = storage.Engine;
 }
Ejemplo n.º 14
0
 /// <summary>
 ///     Sum of array elements into a scalar.
 /// </summary>
 /// <param name="axis">Axis or axes along which a sum is performed. The default, axis=None, will sum all of the elements of the input array. If axis is negative it counts from the last to the first axis.</param>
 /// <param name="typeCode">The type of the returned array and of the accumulator in which the elements are summed. The dtype of a is used by default unless a has an integer dtype of less precision than the default platform integer. In that case, if a is signed then the platform integer is used while if a is unsigned then an unsigned integer of the same precision as the platform integer is used.</param>
 /// <param name="keepdims">If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array. If the default value is passed, then keepdims will not be passed through to the sum method of sub-classes of ndarray, however any non-default value will be.If the sub-class’ method does not implement keepdims any exceptions will be raised.</param>
 /// <returns>An array with the same shape as a, with the specified axis removed. If a is a 0-d array, or if axis is None, a scalar is returned. If an output array is specified, a reference to out is returned.</returns>
 /// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.sum.html</remarks>
 public NDArray sum()
 {
     return(TensorEngine.Sum(this, null, null, false));
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Constructor for init data type
 /// internal storage is 1D with 1 element
 /// </summary>
 /// <param name="typeCode">Data type of elements</param>
 /// <param name="engine">The engine of this <see cref="NDArray"/></param>
 /// <remarks>This constructor does not call allocation/></remarks>
 protected internal NDArray(NPTypeCode typeCode, TensorEngine engine)
 {
     tensorEngine = engine;
     Storage = TensorEngine.GetStorage(typeCode);
 }
Ejemplo n.º 16
0
 /// <summary>
 ///     Return the minimum of an array or minimum along an axis.
 /// </summary>
 /// <typeparam name="T">The expected return type, cast will be performed if necessary.</typeparam>
 /// <returns>Minimum of a. If axis is None, the result is a scalar value. If axis is given, the result is an array of dimension a.ndim - 1.</returns>
 /// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.amin.html</remarks>
 public T amin <T>() where T : unmanaged
 {
     return(np.asscalar <T>(TensorEngine.AMin(this, null, typeof(T).GetTypeCode(), false)));
 }
Ejemplo n.º 17
0
 /// <summary>
 ///     Sum of array elements over a given axis.
 /// </summary>
 /// <param name="axis">Axis or axes along which a sum is performed. The default, axis=None, will sum all of the elements of the input array. If axis is negative it counts from the last to the first axis.</param>
 /// <param name="dtype">The type of the returned array and of the accumulator in which the elements are summed. The dtype of a is used by default unless a has an integer dtype of less precision than the default platform integer. In that case, if a is signed then the platform integer is used while if a is unsigned then an unsigned integer of the same precision as the platform integer is used.</param>
 /// <param name="keepdims">If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array. If the default value is passed, then keepdims will not be passed through to the sum method of sub-classes of ndarray, however any non-default value will be.If the sub-class’ method does not implement keepdims any exceptions will be raised.</param>
 /// <returns>An array with the same shape as a, with the specified axis removed. If a is a 0-d array, or if axis is None, a scalar is returned. If an output array is specified, a reference to out is returned.</returns>
 /// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.sum.html</remarks>
 public NDArray sum(int axis, bool keepdims, Type dtype)
 {
     return(TensorEngine.Sum(this, axis, dtype != null ? dtype.GetTypeCode() : default(NPTypeCode?), keepdims));
 }