private static ndarray ndArrayFromMD(Array ssrc, NPY_TYPES type_num, int ndim) { npy_intp [] newshape = new npy_intp[ndim]; for (int i = 0; i < ndim; i++) { newshape[i] = ssrc.GetLength(i); } dtype WantedDType = NpyCoreApi.DescrFromType(type_num); return(np.array(new VoidPtr(ArrayFromMD(ssrc, type_num), type_num), dtype: WantedDType).reshape(newshape)); }
internal void AddIntpArray(Object arg) { // Convert to an intp array ndarray arr = np.FromAny(arg, NpyCoreApi.DescrFromType(NPY_TYPES.NPY_INTP), 0, 0, NPYARRAYFLAGS.NPY_FORCECAST, null); // Write the type indexes[num_indexes].type = NpyIndexType.NPY_INDEX_INTP_ARRAY; // Write the array indexes[num_indexes].intp_array = arr.Array; ++num_indexes; }
internal void AddBoolArray(Object arg) { // Convert to an intp array ndarray arr = np.FromAny(arg, NpyCoreApi.DescrFromType(NPY_TYPES.NPY_BOOL), 0, 0, 0, null); // Write the type indexes[num_indexes].type = NpyIndexType.NPY_INDEX_BOOL_ARRAY; // Write the array indexes[num_indexes].bool_array = arr.Array; ++num_indexes; }
/// <summary> /// Given some object and an optional minimum type, returns the appropriate type descriptor. /// Equivalent to _array_find_type in common.c of CPython interface. /// </summary> /// <param name="src">Source object</param> /// <param name="minitype">Minimum type, or null if any</param> /// <param name="max">Maximum dimensions</param> /// <returns>Type descriptor fitting requirements</returns> internal static dtype FindArrayType(Object src, dtype minitype, int max = NpyDefs.NPY_MAXDIMS) { dtype chktype = null; if (src.GetType().IsArray) { if (src is ndarray[]) { ndarray[] arr1 = src as ndarray[]; src = arr1[0]; } } if (src is ndarray) { chktype = ((ndarray)src).Dtype; if (minitype == null) { return(chktype); } else { return(FindArrayReturn(chktype, minitype)); } } if (minitype == null) { minitype = NpyCoreApi.DescrFromType(NPY_TYPES.NPY_BOOL); } if (max < 0) { chktype = UseDefaultType(src); return(FindArrayReturn(chktype, minitype)); } chktype = FindScalarType(src); if (chktype != null) { return(FindArrayReturn(chktype, minitype)); } chktype = UseDefaultType(src); return(FindArrayReturn(chktype, minitype)); }
internal object Round(int decimals, ndarray ret = null) { // KM: this is done in the rint handler function for COMPLEX variables // For complex just round both parts. //if (IsComplex) //{ // if (ret == null) // { // ret = Copy(); // } // Real.Round(decimals, ret.Real); // Imag.Round(decimals, ret.Imag); // return ret; //} if (decimals >= 0 && IsInteger) { // There is nothing to do for integers. if (ret != null) { NpyCoreApi.CopyAnyInto(ret, this); return(ret); } else { return(this); } } if (decimals == 0) { // This is just a ufunc return(UnaryOpInPlace(this, UFuncOperation.rint, ret)); } // Set up to do a multiply, round, divide, or the other way around. NpyUFuncObject pre; NpyUFuncObject post; if (decimals >= 0) { pre = NpyCoreApi.GetNumericOp(UFuncOperation.multiply); post = NpyCoreApi.GetNumericOp(UFuncOperation.divide); } else { pre = NpyCoreApi.GetNumericOp(UFuncOperation.divide); post = NpyCoreApi.GetNumericOp(UFuncOperation.multiply); decimals = -decimals; } var factor = PowerOfTen(decimals); // Make a temporary array, if we need it. NPY_TYPES tmpType = NPY_TYPES.NPY_DOUBLE; if (!IsInteger) { tmpType = TypeNum; } ndarray tmp; if (ret != null && ret.TypeNum == tmpType) { tmp = ret; } else { tmp = NpyCoreApi.NewFromDescr(NpyCoreApi.DescrFromType(tmpType), dims, null, 0, null); } // Do the work tmp = BinaryOp(this, factor, pre); UnaryOpInPlace(tmp, UFuncOperation.rint, tmp); BinaryOpInPlace(tmp, factor, post, tmp); if (ret != null && tmp != ret) { NpyCoreApi.CopyAnyInto(ret, tmp); return(ret); } if (this.TypeNum != tmp.TypeNum) { tmp = tmp.astype(this.Dtype); } return(tmp); }
private static dtype UseDefaultType(Object src) { // TODO: User-defined types are not implemented yet. return(NpyCoreApi.DescrFromType(NPY_TYPES.NPY_OBJECT)); }
/// <summary> /// Constructs a new array from multiple input types, like lists, arrays, etc. /// </summary> /// <param name="src"></param> /// <param name="descr"></param> /// <param name="minDepth"></param> /// <param name="maxDepth"></param> /// <param name="requires"></param> /// <param name="context"></param> /// <returns></returns> internal static ndarray FromAny(Object src, dtype descr = null, int minDepth = 0, int maxDepth = 0, NPYARRAYFLAGS flags = 0, Object context = null) { ndarray result = null; if (src == null) { return(np.empty(new shape(0), NpyCoreApi.DescrFromType(NPY_TYPES.NPY_OBJECT))); } Type t = src.GetType(); if (t != typeof(PythonTuple)) { if (src is ndarray) { result = NpyCoreApi.FromArray((ndarray)src, descr, flags); return(FromAnyReturn(result, minDepth, maxDepth)); } if (t.IsArray) { result = asanyarray(src); } dtype newtype = (descr ?? FindScalarType(src)); if (descr == null && newtype != null) { if ((flags & NPYARRAYFLAGS.NPY_UPDATEIFCOPY) != 0) { throw UpdateIfCopyError(); } result = FromPythonScalar(src, newtype); return(FromAnyReturn(result, minDepth, maxDepth)); } //result = FromScalar(src, descr, context); if (result != null) { if (descr != null && !NpyCoreApi.EquivTypes(descr, result.Dtype) || flags != 0) { result = NpyCoreApi.FromArray(result, descr, flags); return(FromAnyReturn(result, minDepth, maxDepth)); } } } bool is_object = false; if ((flags & NPYARRAYFLAGS.NPY_UPDATEIFCOPY) != 0) { throw UpdateIfCopyError(); } if (descr == null) { descr = FindArrayType(src, null); } else if (descr.TypeNum == NPY_TYPES.NPY_OBJECT) { is_object = true; } if (result == null) { bool seq = false; if (src is IEnumerable <object> ) { try { result = FromIEnumerable((IEnumerable <object>)src, descr, (flags & NPYARRAYFLAGS.NPY_FORTRAN) != 0, minDepth, maxDepth); seq = true; } catch (InsufficientMemoryException) { throw; } catch { if (is_object) { result = FromNestedList(src, descr, (flags & NPYARRAYFLAGS.NPY_FORTRAN) != 0); seq = true; } } } if (!seq) { result = FromScalar(src, descr, null); } } return(FromAnyReturn(result, minDepth, maxDepth)); }
internal object Round(int decimals, ndarray ret = null) { // For complex just round both parts. if (IsComplex) { if (ret == null) { ret = Copy(); } Real.Round(decimals, ret.Real); Imag.Round(decimals, ret.Imag); return(ret); } if (decimals >= 0 && IsInteger) { // There is nothing to do for integers. if (ret != null) { NpyCoreApi.CopyAnyInto(ret, this); return(ret); } else { return(this); } } if (decimals == 0) { // This is just a ufunc return(UnaryOpInPlace(this, NpyArray_Ops.npy_op_rint, ret)); } // Set up to do a multiply, round, divide, or the other way around. ufunc pre; ufunc post; if (decimals >= 0) { pre = NpyCoreApi.GetNumericOp(NpyArray_Ops.npy_op_multiply); post = NpyCoreApi.GetNumericOp(NpyArray_Ops.npy_op_divide); } else { pre = NpyCoreApi.GetNumericOp(NpyArray_Ops.npy_op_divide); post = NpyCoreApi.GetNumericOp(NpyArray_Ops.npy_op_multiply); decimals = -decimals; } var factor = PowerOfTen(decimals); // Make a temporary array, if we need it. NPY_TYPES tmpType = NPY_TYPES.NPY_DOUBLE; if (!IsInteger) { tmpType = Dtype.TypeNum; } ndarray tmp; if (ret != null && ret.Dtype.TypeNum == tmpType) { tmp = ret; } else { tmp = NpyCoreApi.NewFromDescr(NpyCoreApi.DescrFromType(tmpType), dims, null, 0, null); } // Do the work tmp = BinaryOp(this, factor, pre); UnaryOpInPlace(tmp, NpyArray_Ops.npy_op_rint, tmp); BinaryOpInPlace(tmp, factor, post, tmp); if (ret != null && tmp != ret) { NpyCoreApi.CopyAnyInto(ret, tmp); return(ret); } return(tmp); }
/// <summary> /// Returns the descriptor for a given native type or null if src is /// not a scalar type /// </summary> /// <param name="src">Object to type</param> /// <returns>Descriptor for type of 'src' or null if not scalar</returns> internal static dtype FindScalarType(Object src) { NPY_TYPES type; if (src is Double) { type = NPY_TYPES.NPY_DOUBLE; } else if (src is Single) { type = NPY_TYPES.NPY_FLOAT; } else if (src is Boolean) { type = NPY_TYPES.NPY_BOOL; } else if (src is Byte) { type = NPY_TYPES.NPY_UBYTE; } else if (src is SByte) { type = NPY_TYPES.NPY_BYTE; } else if (src is Int16) { type = NPY_TYPES.NPY_SHORT; } else if (src is Int32) { type = NpyCoreApi.TypeOf_Int32; } else if (src is Int64) { type = NpyCoreApi.TypeOf_Int64; } else if (src is UInt16) { type = NPY_TYPES.NPY_USHORT; } else if (src is UInt32) { type = NpyCoreApi.TypeOf_UInt32; } else if (src is UInt64) { type = NpyCoreApi.TypeOf_UInt64; } else if (src is Decimal) { type = NpyCoreApi.TypeOf_Decimal; } else if (src is BigInteger) { BigInteger bi = (BigInteger)src; if (System.Int64.MinValue <= bi && bi <= System.Int64.MaxValue) { type = NpyCoreApi.TypeOf_Int64; } else { type = NPY_TYPES.NPY_OBJECT; } } else if (src is Complex) { type = NPY_TYPES.NPY_CDOUBLE; } else { type = NPY_TYPES.NPY_NOTYPE; } return((type != NPY_TYPES.NPY_NOTYPE) ? NpyCoreApi.DescrFromType(type) : null); }
/// <summary> /// Given some object and an optional minimum type, returns the appropriate type descriptor. /// Equivalent to _array_find_type in common.c of CPython interface. /// </summary> /// <param name="src">Source object</param> /// <param name="minitype">Minimum type, or null if any</param> /// <param name="max">Maximum dimensions</param> /// <returns>Type descriptor fitting requirements</returns> public static dtype FindArrayType(Object src, dtype minitype, int max = NpyDefs.NPY_MAXDIMS) { dtype chktype = null; if (src.GetType().IsArray) { dynamic arr1 = src; if (arr1[0] is ndarray) { src = arr1[0]; } } if (src is ndarray) { chktype = ((ndarray)src).Dtype; if (minitype == null) { return(chktype); } else { return(FindArrayReturn(chktype, minitype)); } } if (src is ScalarGeneric) { chktype = (dtype)((ScalarGeneric)src).dtype; if (minitype == null) { return(chktype); } else { return(FindArrayReturn(chktype, minitype)); } } if (minitype == null) { minitype = NpyCoreApi.DescrFromType(NPY_TYPES.NPY_BOOL); } if (max < 0) { chktype = UseDefaultType(src); return(FindArrayReturn(chktype, minitype)); } chktype = FindScalarType(src); if (chktype != null) { return(FindArrayReturn(chktype, minitype)); } if (src is String) { String s = (String)src; chktype = new dtype(NpyCoreApi.DescrFromType(NPY_TYPES.NPY_UNICODE)); chktype.ElementSize = s.Length * 4; return(FindArrayReturn(chktype, minitype)); } chktype = UseDefaultType(src); return(FindArrayReturn(chktype, minitype)); }
/// <summary> /// Constructs a new array from multiple input types, like lists, arrays, etc. /// </summary> /// <param name="src"></param> /// <param name="descr"></param> /// <param name="minDepth"></param> /// <param name="maxDepth"></param> /// <param name="requires"></param> /// <param name="context"></param> /// <returns></returns> public static ndarray FromAny(Object src, dtype descr = null, int minDepth = 0, int maxDepth = 0, NPYARRAYFLAGS flags = 0, Object context = null) { ndarray result = null; if (src == null) { return(np.empty(new shape(0), NpyCoreApi.DescrFromType(NPY_TYPES.NPY_OBJECT))); } Type t = src.GetType(); if (t != typeof(PythonTuple)) { if (src is ndarray) { result = NpyCoreApi.FromArray((ndarray)src, descr, flags); return(FromAnyReturn(result, minDepth, maxDepth)); } if (t.IsArray) { result = asanyarray(src); } if (src is ScalarGeneric) { if ((flags & NPYARRAYFLAGS.NPY_UPDATEIFCOPY) != 0) { throw UpdateIfCopyError(); } result = FromScalar((ScalarGeneric)src, descr); return(FromAnyReturn(result, minDepth, maxDepth)); } dtype newtype = (descr ?? FindScalarType(src)); if (descr == null && newtype != null) { if ((flags & NPYARRAYFLAGS.NPY_UPDATEIFCOPY) != 0) { throw UpdateIfCopyError(); } result = FromPythonScalar(src, newtype); return(FromAnyReturn(result, minDepth, maxDepth)); } //result = FromScalar(src, descr, context); if (result != null) { if (descr != null && !NpyCoreApi.EquivTypes(descr, result.Dtype) || flags != 0) { result = NpyCoreApi.FromArray(result, descr, flags); return(FromAnyReturn(result, minDepth, maxDepth)); } } } bool is_object = false; if ((flags & NPYARRAYFLAGS.NPY_UPDATEIFCOPY) != 0) { throw UpdateIfCopyError(); } if (descr == null) { descr = FindArrayType(src, null); } else if (descr.TypeNum == NPY_TYPES.NPY_OBJECT) { is_object = true; } if (result == null) { // Hack required because in C# strings are enumerations of chars, not objects. // However, we want to keep src as a string if we are building a string or object array. if (!is_object && (descr.TypeNum != NPY_TYPES.NPY_STRING || descr.Type == NPY_TYPECHAR.NPY_CHARLTR) && descr.TypeNum != NPY_TYPES.NPY_UNICODE && src is string && ((string)src).Length > 1) { src = ((string)src).Cast <object>(); } bool seq = false; if (src is IEnumerable <object> ) { try { result = FromIEnumerable((IEnumerable <object>)src, descr, (flags & NPYARRAYFLAGS.NPY_FORTRAN) != 0, minDepth, maxDepth); seq = true; } catch (InsufficientMemoryException) { throw; } catch { if (is_object) { result = FromNestedList(src, descr, (flags & NPYARRAYFLAGS.NPY_FORTRAN) != 0); seq = true; } } } if (!seq) { result = FromScalar(src, descr, null); } } return(FromAnyReturn(result, minDepth, maxDepth)); }