コード例 #1
0
        public T ToScalar<T>()
        {
            unsafe
            {
                if (typeof(T).as_dtype() == this.dtype && this.dtype != TF_DataType.TF_STRING)
                    return Unsafe.Read<T>(this.buffer.ToPointer());

                switch (this.dtype)
                {
#if _REGEN
                    %foreach supported_numericals_TF_DataType,supported_numericals,supported_numericals_lowercase%
                    case TF_DataType.#1:
                        return Converts.ChangeType<T>(*(#3*) this.buffer);
                    %
#else

                    case TF_DataType.TF_UINT8:
                        return Converts.ChangeType<T>(*(byte*) this.buffer);
コード例 #2
0
        /// <summary>
        ///     Return a 2-D array with ones on the diagonal and zeros elsewhere.
        /// </summary>
        /// <param name="N">Number of rows in the output.</param>
        /// <param name="M">Number of columns in the output. If None, defaults to N.</param>
        /// <param name="k">Index of the diagonal: 0 (the default) refers to the main diagonal, a positive value refers to an upper diagonal, and a negative value to a lower diagonal.</param>
        /// <param name="dtype">Data-type of the returned array.</param>
        /// <returns>An array where all elements are equal to zero, except for the k-th diagonal, whose values are equal to one.</returns>
        /// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.eye.html</remarks>
        public static NDArray eye(int N, int?M = null, int k = 0, Type dtype = null)
        {
            if (!M.HasValue)
            {
                M = N;
            }
            var m = np.zeros(Shape.Matrix(N, M.Value), dtype ?? typeof(double));

            if (k >= M)
            {
                return(m);
            }
            int i;

            if (k >= 0)
            {
                i = k;
            }
            else
            {
                i = (-k) * M.Value;
            }

            var flat = m.flat;
            var one  = dtype != null?Converts.ChangeType(1d, dtype.GetTypeCode()) : 1d;

            int skips = k < 0 ? Math.Abs(k) - 1 : 0;

            for (int j = k; j < flat.size; j += N + 1)
            {
                if (j < 0 || skips-- > 0)
                {
                    continue;
                }
                flat.SetAtIndex(one, j);
            }

            return(m);
        }
コード例 #3
0
ファイル: NdArray.Scalar.cs プロジェクト: jangocheng/NumSharp
 public static NDArray Scalar <T>(object value) where T : unmanaged
 {
     return(new NDArray(UnmanagedStorage.Scalar(value as T? ?? Converts.ChangeType <T>(value, InfoOf <T> .NPTypeCode))));
 }
コード例 #4
0
ファイル: np.full.cs プロジェクト: SciSharp/NumSharp.Lite
        /// <summary>
        ///     Return a new array of given shape and type, filled with fill_value.
        /// </summary>
        /// <param name="fill_value">Fill value.</param>
        /// <param name="shape">Shape of the empty array, e.g., (2, 3) or 2.</param>
        /// <param name="typeCode">The desired data-type for the array The default, null, means np.array(fill_value).dtype.</param>
        /// <returns>Array of fill_value with the given shape, dtype, and order.</returns>
        /// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.full.html</remarks>
        public static NDArray full(ValueType fill_value, Shape shape, NPTypeCode typeCode)
        {
            if (typeCode == NPTypeCode.Empty)
            {
                throw new ArgumentNullException(nameof(typeCode));
            }

            return(new NDArray(new UnmanagedStorage(ArraySlice.Allocate(typeCode, shape.size, Converts.ChangeType(fill_value, (TypeCode)typeCode)), shape)));
        }
コード例 #5
0
 public static NDArray Scalar(object value, Type dtype)
 {
     return(new NDArray(UnmanagedStorage.Scalar(Converts.ChangeType(value, dtype.GetTypeCode()))));
 }
コード例 #6
0
 /// <summary>
 /// Scalar value
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public T randn <T>()
 {
     return((T)Converts.ChangeType(randomizer.NextDouble(), InfoOf <T> .NPTypeCode));
 }
コード例 #7
0
        public override NDArray ReduceProduct(NDArray arr, int? axis_, bool keepdims = false, NPTypeCode? typeCode = null)
        {
            //in order to iterate an axis:
            //consider arange shaped (1,2,3,4) when we want to summarize axis 1 (2nd dimension which its value is 2)
            //the size of the array is [1, 2, n, m] all shapes after 2nd multiplied gives size
            //the size of what we need to reduce is the size of the shape of the given axis (shape[axis])
            var shape = arr.Shape;
            if (shape.IsEmpty || shape.size==0)
                return NDArray.Scalar(1, (typeCode ?? arr.typecode));

            if (shape.IsScalar || (shape.size == 1 && shape.NDim == 1))
            {
                var r = NDArray.Scalar(typeCode.HasValue ? Converts.ChangeType(arr.GetAtIndex(0), typeCode.Value) : arr.GetAtIndex(0));
                if (keepdims)
                    r.Storage.ExpandDimension(0);
                
                return r;
            }

            if (axis_ == null)
            {
                var r = NDArray.Scalar(product_elementwise(arr, typeCode));
                if (keepdims)
                    r.Storage.ExpandDimension(0);
                else if (!r.Shape.IsScalar && r.Shape.size == 1 && r.ndim == 1)
                    r.Storage.Reshape(Shape.Scalar);
                return r;
            }

            var axis = axis_.Value;
            while (axis < 0)
                axis = arr.ndim + axis; //handle negative axis

            if (axis >= arr.ndim)
                throw new ArgumentOutOfRangeException(nameof(axis));

            if (shape[axis] == 1)
            {
                //if the given div axis is 1 and can be squeezed out.
                if (keepdims)
                    return new NDArray(arr.Storage.Alias());
                return np.squeeze_fast(arr, axis);
            }

            //handle keepdims
            Shape axisedShape = Shape.GetAxis(shape, axis);
            var retType = typeCode ?? (arr.GetTypeCode.GetAccumulatingType());

            //prepare ret
            var ret = new NDArray(retType, axisedShape, false);
            var iterAxis = new NDCoordinatesAxisIncrementor(ref shape, axis);
            var iterRet = new NDCoordinatesIncrementor(ref axisedShape);
            var iterIndex = iterRet.Index;
            var slices = iterAxis.Slices;

            //resolve the accumulator type

#if _REGEN1
            #region Compute
            switch (arr.GetTypeCode)
		    {
			    %foreach supported_numericals,supported_numericals_lowercase%
			    case NPTypeCode.#1: 
                {
                    switch (retType)
		            {
			            %foreach supported_numericals,supported_numericals_lowercase,supported_numericals_onevales%
			            case NPTypeCode.#101: 
                        {
                            do
                            {
                                var slice = arr[slices];
                                var iter = slice.AsIterator<#2>();
                                var moveNext = iter.MoveNext;
                                var hasNext = iter.HasNext;
                                |#102 sum = #103;
                                while (hasNext())
                                    sum *= (#102) moveNext();

                                ret.Set#101(Converts.To#101(sum), iterIndex);
                            } while (iterAxis.Next() != null && iterRet.Next() != null);
                            break;
                        }
コード例 #8
0
        /// <summary>
        ///     Return a full array with the same shape and type as a given array.
        /// </summary>
        /// <param name="a">The shape and data-type of a define these same attributes of the returned array.</param>
        /// <param name="fill_value">Fill value.</param>
        /// <param name="dtype">Overrides the data type of the result.</param>
        /// <returns>Array of fill_value with the same shape and type as a.</returns>
        /// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.full_like.html</remarks>
        public static NDArray full_like(NDArray a, object fill_value, Type dtype = null)
        {
            var typeCode = (dtype ?? fill_value?.GetType() ?? a.dtype).GetTypeCode();
            var shape    = new Shape((int[])a.shape.Clone());

            return(new NDArray(new UnmanagedStorage(ArraySlice.Allocate(typeCode, shape.size, Converts.ChangeType(fill_value, (TypeCode)typeCode)), shape)));
        }