Ejemplo n.º 1
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, Convert.ChangeType(fill_value, (TypeCode)typeCode)), shape)));
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Return a new array of given shape and type, filled with ones.
        /// </summary>
        /// <param name="shape">Shape of the new array.</param>
        /// <param name="typeCode">The desired data-type for the array, e.g., <see cref="uint8"/>. Default is <see cref="float64"/> / <see cref="double"/>.</param>
        /// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.ones.html</remarks>
        public static NDArray ones(Shape shape, NPTypeCode typeCode)
        {
            object one = null;

            switch (typeCode)
            {
            case NPTypeCode.Complex:
                one = new Complex(1d, 0d);
                break;

            case NPTypeCode.NDArray:
                one = NDArray.Scalar(1, np.int32);
                break;

            case NPTypeCode.String:
                one = "1";
                break;

            case NPTypeCode.Char:
                one = '1';
                break;

            default:
                one = Converts.ChangeType((byte)1, typeCode);
                break;
            }

            return(new NDArray(ArraySlice.Allocate(typeCode, shape.size, one), shape));
        }
Ejemplo n.º 3
0
        /// <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, Convert.ChangeType(fill_value, (TypeCode)typeCode)), shape)));
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     Clone internal storage and get reference to it
        /// </summary>
        /// <returns>reference to cloned storage as System.Array</returns>
        public IArraySlice CloneData()
        {
            //Incase shape is not sliced, we can copy the internal buffer.
            if (!_shape.IsSliced && !_shape.IsBroadcasted)
            {
                return(InternalArray.Clone());
            }

            if (_shape.IsScalar)
            {
                return(ArraySlice.Scalar(GetValue(0), _typecode));
            }

            //Linear copy of all the sliced items.

            var ret = ArraySlice.Allocate(InternalArray.TypeCode, _shape.size, false);

            MultiIterator.Assign(new UnmanagedStorage(ret, _shape.Clean()), this);

            return(ret);
        }
Ejemplo n.º 5
0
 /// <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>
 /// <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)
 {
     return(new NDArray(new UnmanagedStorage(ArraySlice.Allocate(fill_value.GetType(), shape.size, fill_value), shape)));
 }