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()); }
/// <summary> /// Performs a generic reduce or accumulate operation on an input array. /// A reduce operation reduces the number of dimensions of the input array /// by one where accumulate does not. Accumulate stores in incremental /// accumulated values in the extra dimension. /// </summary> /// <param name="arr">Input array</param> /// <param name="indices">Used only for reduceat</param> /// <param name="axis">Axis to reduce</param> /// <param name="otype">Output type of the array</param> /// <param name="outArr">Optional output array</param> /// <param name="operation">Reduce/accumulate operation to perform</param> /// <returns>Resulting array, either outArr or a new array</returns> private Object GenericReduce(ndarray arr, ndarray indices, int axis, dtype otype, ndarray outArr, GenericReductionOp operation) { if (signature() != null) { throw new RuntimeException("Reduction is not defined on ufunc's with signatures"); } if (nin != 2) { throw new ArgumentException("Reduce/accumulate only supported for binary functions"); } if (nout != 1) { throw new ArgumentException("Reduce/accumulate only supported for functions returning a single value"); } if (arr.ndim == 0) { throw new ArgumentTypeException("Cannot reduce/accumulate a scalar"); } if (arr.IsFlexible || (otype != null && NpyDefs.IsFlexible(otype.TypeNum))) { throw new ArgumentTypeException("Cannot perform reduce/accumulate with flexible type"); } return(NpyCoreApi.GenericReduction(this, arr, indices, outArr, axis, otype, operation)); }
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 override string ToString() { string ret; if (this.HasNames) { Object lst = this.descr; ret = (lst != null) ? lst.ToString() : "<err>"; ret = String.Format("('{0}', {1})", this.str, this.descr); } else if (this.HasSubarray) { dtype b = @base; if (!b.HasNames && !b.HasSubarray) { ret = String.Format("('{0}',{1})", b.ToString(), shape); } else { ret = String.Format("({0},{1})", b.ToString(), shape); } } else if (NpyDefs.IsFlexible(this.TypeNum) || !this.IsNativeByteOrder) { ret = this.str; } else { ret = this.name; } return(ret); }
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)); } } }
private static void ConvertSingleIndex(ndarray arr, Object arg, NpyIndexes indexes, int index) { if (arg == null) { indexes.AddNewAxis(); } else if (arg is string && (string)arg == "...") { indexes.AddEllipsis(); } else if (arg is Ellipsis) { indexes.AddEllipsis(); } else if (arg is CSharpTuple) { indexes.AddCSharpTuple((CSharpTuple)arg); } else if (arg is bool) { indexes.AddIndex((bool)arg); } else if (arg is int) { indexes.AddIndex((int)arg); } else if (arg is BigInteger) { BigInteger bi = (BigInteger)arg; npy_intp lval = (npy_intp)bi; indexes.AddIndex(lval); } else if (arg is ISlice) { indexes.AddIndex((ISlice)arg); } else if (arg is string) { indexes.AddIndex(arr, (string)arg, index); } else { ndarray array_arg = null; if (arg.GetType().IsArray) { dynamic arr1 = arg; if (arr1[0] is ndarray) { } else { array_arg = np.array(new VoidPtr(arr1), null); } } else { array_arg = arg as ndarray; } if (array_arg == null && arg is IEnumerable <object> ) { array_arg = np.FromIEnumerable((IEnumerable <object>)arg, null, false, 0, 0); } if (array_arg == null && arg is IEnumerable <npy_intp> ) { var arr1 = arg as IEnumerable <npy_intp>; array_arg = np.array(arr1.ToArray(), null); } // Boolean scalars if (array_arg != null && array_arg.ndim == 0 && NpyDefs.IsBool(array_arg.TypeNum)) { indexes.AddIndex(Convert.ToBoolean(array_arg[0])); } // Integer scalars else if (array_arg != null && array_arg.ndim == 0 && NpyDefs.IsInteger(array_arg.TypeNum)) { try { indexes.AddIndex((npy_intp)Convert.ToInt64(array_arg[0])); } catch (Exception e) { throw new IndexOutOfRangeException(e.Message); } } else if (array_arg != null) { // Arrays must be either boolean or integer. if (NpyDefs.IsInteger(array_arg.TypeNum)) { indexes.AddIntpArray(array_arg); } else if (NpyDefs.IsBool(array_arg.TypeNum)) { indexes.AddBoolArray(array_arg); } else { throw new IndexOutOfRangeException("arrays used as indices must be of integer (or boolean) type."); } } else if (arg is IEnumerable <Object> ) { // Other sequences we convert to an intp array indexes.AddIntpArray(arg); } else if (arg is IConvertible) { #if NPY_INTP_64 indexes.AddIndex((npy_intp)Convert.ToInt64(arg)); #else indexes.AddIndex((npy_intp)Convert.ToInt32(arg)); #endif } else { throw new ArgumentException(String.Format("Argument '{0}' is not a valid index.", arg)); } } }
/// <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); }
private void FindScalarInfo() { ScalarInfo info = null; NPY_TYPES type = TypeNum; if (NpyDefs.IsSigned(type)) { switch (ElementSize) { case 1: info = ScalarInfo.Make <ScalarInt8>(); break; case 2: info = ScalarInfo.Make <ScalarInt16>(); break; case 4: info = (type == NpyCoreApi.TypeOf_Int32) ? ScalarInfo.Make <ScalarInt32>() : ScalarInfo.Make <ScalarIntC>(); break; case 8: info = (type == NpyCoreApi.TypeOf_Int64) ? ScalarInfo.Make <ScalarInt64>() : ScalarInfo.Make <ScalarLongLong>(); break; } } else if (NpyDefs.IsUnsigned(type)) { switch (ElementSize) { case 1: info = ScalarInfo.Make <ScalarUInt8>(); break; case 2: info = ScalarInfo.Make <ScalarUInt16>(); break; case 4: info = (type == NpyCoreApi.TypeOf_UInt32) ? ScalarInfo.Make <ScalarUInt32>() : ScalarInfo.Make <ScalarUIntC>(); break; case 8: info = (type == NpyCoreApi.TypeOf_UInt64) ? ScalarInfo.Make <ScalarUInt64>() : ScalarInfo.Make <ScalarULongLong>(); break; } } else if (NpyDefs.IsFloat(type)) { switch (ElementSize) { case 4: info = ScalarInfo.Make <ScalarFloat32>(); break; case 8: info = ScalarInfo.Make <ScalarFloat64>(); break; } } else if (NpyDefs.IsComplex(type)) { switch (ElementSize) { case 8: info = ScalarInfo.Make <ScalarComplex64>(); break; case 16: info = ScalarInfo.Make <ScalarComplex128>(); break; } } else if (type == NPY_TYPES.NPY_BOOL) { info = ScalarInfo.Make <ScalarBool>(); } else if (type == NPY_TYPES.NPY_VOID) { info = ScalarInfo.Make <ScalarVoid>(); } else if (type == NPY_TYPES.NPY_OBJECT) { info = ScalarInfo.Make <ScalarObject>(); } if (info == null) { info = new ScalarInfo(); } scalarInfo = info; }