public static string DumpArray(StringBuilder sb, NpyArray arr, bool repr) { // Equivalent to array_repr_builtin (arrayobject.c) if (repr) { sb.Append("array("); } npy_intp totalElements = numpyAPI.NpyArray_Size(arr); DumpArray(arr, sb, arr.dimensions, arr.strides, 0, 0, totalElements); if (repr) { if (NpyDefs.IsExtended(arr.descr.type_num)) { sb.AppendFormat(", '{0}{1}')", arr.ItemType, arr.ItemSize); } else { sb.AppendFormat(", '{0}')", arr.ItemType); } } return(sb.ToString()); }
internal static ndarray FromPythonScalar(object src, dtype descr) { int itemsize = descr.ElementSize; NPY_TYPES type = descr.TypeNum; if (itemsize == 0 && NpyDefs.IsExtended(type)) { int n = PythonOps.Length(src); if (type == NPY_TYPES.NPY_UNICODE) { n *= 4; } descr = new dtype(descr); descr.ElementSize = n; } ndarray result = NpyCoreApi.AllocArray(descr, 0, null, false); if (result.ndim > 0) { throw new ArgumentException("shape-mismatch on array construction"); } result.Dtype.f.setitem(0, src, result.Array); return(result); }
public static void DumpArray(List <string> sb, NpyArray arr, bool repr) { // Equivalent to array_repr_builtin (arrayobject.c) if (repr) { sb.Add("array("); } else { string ItemType = arr.ItemType.ToString().Substring("NPY_".Length); if (string.IsNullOrEmpty(arr.Name)) { sb.Add(string.Format("{0} \n", ItemType)); } else { sb.Add(string.Format("{0}:Name({1}) \n", ItemType, arr.Name)); } } npy_intp totalElements = numpyAPI.NpyArray_Size(arr); DumpArray(arr, sb, arr.dimensions, arr.strides, 0, 0, totalElements, 0, !repr); if (repr) { if (NpyDefs.IsExtended(arr.descr.type_num)) { sb.Add(string.Format(", '{0}{1}')", arr.ItemType, arr.ItemSize)); } else { sb.Add(String.Format(", '{0}')", arr.ItemType)); } } }
/// <summary> /// Builds an array from a sequence of objects. The elements of the sequence /// can also be sequences in which case this function recursively walks the /// nested sequences and builds an n dimentional array. /// /// IronPython tuples and lists work as sequences. /// </summary> /// <param name="src">Input sequence</param> /// <param name="descr">Desired array element type or null to determine automatically</param> /// <param name="fortran">True if array should be Fortran layout, false for C</param> /// <param name="minDepth"></param> /// <param name="maxDepth"></param> /// <returns>New array instance</returns> internal static ndarray FromIEnumerable(IEnumerable <Object> src, dtype descr, bool fortran, int minDepth, int maxDepth) { ndarray result = null; if (descr == null) { descr = FindArrayType(src, null, NpyDefs.NPY_MAXDIMS); } int itemsize = descr.ElementSize; NPY_TYPES type = descr.TypeNum; bool checkIt = (descr.Type != NPY_TYPECHAR.NPY_CHARLTR); bool stopAtString = type != NPY_TYPES.NPY_STRING || descr.Type == NPY_TYPECHAR.NPY_STRINGLTR; bool stopAtTuple = type == NPY_TYPES.NPY_VOID && (descr.HasNames || descr.HasSubarray); int numDim = DiscoverDepth(src, NpyDefs.NPY_MAXDIMS + 1, stopAtString, stopAtTuple); if (numDim == 0) { return(FromPythonScalar(src, descr)); } else { if (maxDepth > 0 && type == NPY_TYPES.NPY_OBJECT && numDim > maxDepth) { numDim = maxDepth; } if (maxDepth > 0 && numDim > maxDepth || minDepth > 0 && numDim < minDepth) { throw new ArgumentException("Invalid number of dimensions."); } npy_intp[] dims = new npy_intp[numDim]; DiscoverDimensions(src, numDim, dims, 0, checkIt); if (descr.Type == NPY_TYPECHAR.NPY_CHARLTR && numDim > 0 && dims[numDim - 1] == 1) { numDim--; } if (itemsize == 0 && NpyDefs.IsExtended(descr.TypeNum)) { itemsize = DiscoverItemsize(src, numDim, 0); if (descr.TypeNum == NPY_TYPES.NPY_UNICODE) { itemsize *= 4; } descr = new dtype(descr); descr.ElementSize = itemsize; } result = NpyCoreApi.AllocArray(descr, numDim, dims, fortran); AssignToArray(src, result); } return(result); }