public NDArray Cast(NDArray nd, NPTypeCode dtype, bool copy) { if (dtype == NPTypeCode.Empty) { throw new ArgumentNullException(nameof(dtype)); } NDArray clone() { var copied = new NDArray(nd.dtype, nd.TensorEngine); copied.Storage.Allocate(ArrayConvert.To(nd.Array, dtype), nd.shape); return(copied); } if (nd.GetTypeCode == dtype) { //casting not needed return(copy ? clone() : nd); } else { //casting needed if (copy) { return(clone()); } //just re-set the data, conversion is handled inside. nd.Storage.ReplaceData(nd.Storage.GetData(), dtype); return(nd); } }
public static NDArray array(Array array, Type dtype = null, int ndmin = 1, bool copy = true, char order = 'C') { var arrType = array.ResolveElementType(); //handle dim expansion and extract shape Shape shape; var dims = array.ResolveRank(); var missing = dims - ndmin; if (missing < 0) { shape = Arrays.Concat(Enumerable.Repeat(1, Math.Abs(missing)).ToArray(), Shape.ExtractShape(array)); } else { shape = Shape.ExtractShape(array); } //flatten if (shape.NDim > 1) { array = Arrays.Flatten(array); copy = false; } if (dtype != null && dtype != arrType) { array = ArrayConvert.To(array, dtype); copy = false; } return(new NDArray(copy ? (Array)array.Clone() : array, shape, order)); }