Ejemplo n.º 1
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.º 2
0
        public override NDArray Cast(NDArray nd, NPTypeCode dtype, bool copy)
        {
            if (dtype == NPTypeCode.Empty)
            {
                throw new ArgumentNullException(nameof(dtype));
            }

            //incase its an empty array
            if (nd.Shape.IsEmpty)
            {
                if (copy)
                {
                    return(new NDArray(dtype));
                }

                nd.Storage = new UnmanagedStorage(dtype);
                return(nd);
            }

            //incase its a scalar
            if (nd.Shape.IsScalar)
            {
                var ret = NDArray.Scalar(nd.GetAtIndex(0), dtype);
                if (copy)
                {
                    return(ret);
                }

                nd.Storage = ret.Storage;
                return(nd);
            }

            //incase its a (1,) shaped
            if (nd.Shape.size == 1 && nd.Shape.NDim == 1)
            {
                var ret = new NDArray(ArraySlice.Scalar(nd.GetAtIndex(0), dtype), Shape.Vector(1));
                if (copy)
                {
                    return(ret);
                }

                nd.Storage = ret.Storage;
                return(nd);
            }

            //regular clone
            if (nd.GetTypeCode == dtype)
            {
                //casting not needed
                return(copy ? clone() : nd);
            }
            else
            {
                //casting needed
                if (copy)
                {
                    if (nd.Shape.IsSliced)
                    {
                        nd = clone();
                    }

                    return(new NDArray(new UnmanagedStorage(ArraySlice.FromMemoryBlock(nd.Array.CastTo(dtype), false), nd.Shape)));
                }
                else
                {
                    var storage = nd.Shape.IsSliced ? nd.Storage.Clone() : nd.Storage;
                    nd.Storage = new UnmanagedStorage(ArraySlice.FromMemoryBlock(storage.InternalArray.CastTo(dtype), false), storage.Shape);
                    return(nd);
                }
            }

            NDArray clone() => nd.Clone();
        }