internal static NpyArray NpyArray_IterSubscriptIntp(NpyArrayIterObject self, npy_intp index) { NpyArray result; bool swap; if (index < 0) { index += self.size; } if (index < 0 || index >= self.size) { string msg = string.Format("index {0} out of bounds 0<=index<{1}", index, self.size); NpyErr_SetString(npyexc_type.NpyExc_IndexError, msg); return(null); } Npy_INCREF(self.ao.descr); result = NpyArray_Alloc(self.ao.descr, 0, null, false, Npy_INTERFACE(self.ao)); if (result == null) { return(null); } swap = (NpyArray_ISNOTSWAPPED(self.ao) != NpyArray_ISNOTSWAPPED(result)); NpyArray_ITER_RESET(self); NpyArray_ITER_GOTO1D(self, index); result.descr.f.copyswap(result.data, self.dataptr, swap, self.ao); NpyArray_ITER_RESET(self); return(result); }
internal static void NpyArray_ITER_RESET(NpyArrayIterObject it) { Debug.Assert(Validate(it)); it.index = 0; it.dataptr = new VoidPtr(it.ao); memset(new VoidPtr(it.coordinates), 0, (it.nd_m1 + 1) * sizeof(npy_intp)); }
internal static npy_intp[] NpyArrayAccess_IterCoords(NpyArrayIterObject self) { if (self.contiguous) { /* * coordinates not kept track of --- * need to generate from index */ npy_intp val; int nd = self.ao.nd; int i; val = self.index; for (i = 0; i < nd; i++) { if (self.factors[i] != 0) { self.coordinates[i] = val / self.factors[i]; val = val % self.factors[i]; } else { self.coordinates[i] = 0; } } } return(self.coordinates); }
internal static void _NpyArray_ITER_NEXT1(NpyArrayIterObject it) { Debug.Assert(Validate(it)); it.dataptr.data_offset += it.strides[0]; it.coordinates[0]++; }
internal static int NpyArray_IndexFancyAssign(NpyArray self, NpyIndex [] indexes, int n, NpyArray value) { int result; if (self.nd == 1 && n == 1) { /* Special case for 1-d arrays. */ NpyArrayIterObject iter = NpyArray_IterNew(self); if (iter == null) { return(-1); } result = NpyArray_IterSubscriptAssign(iter, indexes, n, value); Npy_DECREF(iter); return(result); } else { NpyArrayMapIterObject mit = NpyArray_MapIterNew(indexes, n); if (mit == null) { return(-1); } if (NpyArray_MapIterBind(mit, self, null) < 0) { Npy_DECREF(mit); return(-1); } result = NpyArray_SetMap(mit, value); Npy_DECREF(mit); return(result); } }
internal static void _NpyArray_ITER_NEXT3(NpyArrayIterObject it) { Debug.Assert(Validate(it)); if (it.coordinates[2] < it.dims_m1[2]) { it.coordinates[2]++; it.dataptr.data_offset += it.strides[2]; } else { it.coordinates[2] = 0; it.dataptr -= it.backstrides[2]; if (it.coordinates[1] < it.dims_m1[1]) { it.coordinates[1]++; it.dataptr.data_offset += it.strides[1]; } else { it.coordinates[1] = 0; it.coordinates[0]++; it.dataptr.data_offset += it.strides[0] - it.backstrides[1]; } } }
internal static int NpyArray_IterSubscriptAssignIntp(NpyArrayIterObject self, npy_intp index, NpyArray value) { NpyArray converted_value; bool swap; if (NpyArray_SIZE(value) == 0) { NpyErr_SetString(npyexc_type.NpyExc_ValueError, "Error setting single item of array"); return(-1); } Npy_INCREF(self.ao.descr); converted_value = NpyArray_FromArray(value, self.ao.descr, 0); if (converted_value == null) { return(-1); } swap = (NpyArray_ISNOTSWAPPED(self.ao) != NpyArray_ISNOTSWAPPED(converted_value)); NpyArray_ITER_RESET(self); NpyArray_ITER_GOTO1D(self, index); self.ao.descr.f.copyswap(self.dataptr, converted_value.data, swap, self.ao); NpyArray_ITER_RESET(self); Npy_DECREF(converted_value); return(0); }
static NpyArray NpyArray_IterSubscriptBool(NpyArrayIterObject self, bool index) { NpyArray result; bool swap; NpyArray_ITER_RESET(self); if (index) { /* Returns a 0-d array with the value. */ Npy_INCREF(self.ao.descr); result = NpyArray_Alloc(self.ao.descr, 0, null, false, Npy_INTERFACE(self.ao)); if (result == null) { return(null); } swap = (NpyArray_ISNOTSWAPPED(self.ao) != NpyArray_ISNOTSWAPPED(result)); result.descr.f.copyswap(result.data, self.dataptr, swap, self.ao); return(result); } else { /* Make an empty array. */ npy_intp [] ii = new npy_intp[1] { 0 }; Npy_INCREF(self.ao.descr); result = NpyArray_Alloc(self.ao.descr, 7, ii, false, Npy_INTERFACE(self.ao)); return(result); } }
static NpyArray NpyArray_IndexFancy(NpyArray self, NpyIndex [] indexes, int n) { NpyArray result; if (self.nd == 1 && n == 1) { /* Special case for 1-d arrays. */ NpyArrayIterObject iter = NpyArray_IterNew(self); if (iter == null) { return(null); } result = NpyArray_IterSubscript(iter, indexes, n); Npy_DECREF(iter); return(result); } else { NpyArrayMapIterObject mit = NpyArray_MapIterNew(indexes, n); if (mit == null) { return(null); } if (NpyArray_MapIterBind(mit, self, null) < 0) { Npy_DECREF(mit); return(null); } result = NpyArray_GetMap(mit); Npy_DECREF(mit); return(result); } }
internal static int NpyArray_ToTextStream(NpyArray self, Stream fs, string sep, string format) { if (NpyDataType_FLAGCHK(self.descr, NpyArray_Descr_Flags.NPY_LIST_PICKLE)) { NpyErr_SetString(npyexc_type.NpyExc_ValueError, "cannot write object arrays to a file in text mode"); return(-1); } if (NpyArray_ISCONTIGUOUS(self)) { if (NpyArray_WriteTextStream(fs, self.data, NpyArray_SIZE(self), sep, format) == false) { string msg = string.Format("error writing array contents to file"); NpyErr_SetString(npyexc_type.NpyExc_ValueError, msg); return(-1); } } else { NpyArrayIterObject it = NpyArray_IterNew(self); while (it.index < it.size) { if (NpyArray_WriteTextStream(fs, it.dataptr, NpyArray_ITEMSIZE(self), sep, format) == false) { string msg = string.Format("problem writing element {0} to file", it.index); NpyErr_SetString(npyexc_type.NpyExc_IOError, msg); Npy_DECREF(it); return(-1); } NpyArray_ITER_NEXT(it); } Npy_DECREF(it); } return(0); }
internal static void NpyArray_ITER_GOTO1D(NpyArrayIterObject it, npy_intp indices) { Debug.Assert(Validate(it)); if (indices < 0) { indices += it.size; } it.index = indices; if (it.nd_m1 == 0) { it.dataptr = new VoidPtr(it.ao); it.dataptr.data_offset += indices * it.strides[0]; } else if (it.contiguous) { it.dataptr = new VoidPtr(it.ao); it.dataptr.data_offset += (npy_intp)(indices * it.ao.descr.elsize); } else { it.dataptr = new VoidPtr(it.ao); for (int index = 0; index <= it.nd_m1; index++) { it.dataptr.data_offset += (indices / it.factors[index]) * it.strides[index]; indices %= it.factors[index]; } } }
internal static int NpyArray_ToBinaryStream(NpyArray self, Stream fs) { if (NpyArray_ISCONTIGUOUS(self)) { if (NpyArray_WriteBinaryStream(fs, self.data, NpyArray_SIZE(self)) == false) { string msg = string.Format("error writing array contents to file"); NpyErr_SetString(npyexc_type.NpyExc_ValueError, msg); return(-1); } } else { NpyArrayIterObject it = NpyArray_IterNew(self); while (it.index < it.size) { if (NpyArray_WriteBinaryStream(fs, it.dataptr, NpyArray_ITEMSIZE(self)) == false) { string msg = string.Format("problem writing element {0} to file", it.index); NpyErr_SetString(npyexc_type.NpyExc_IOError, msg); Npy_DECREF(it); return(-1); } NpyArray_ITER_NEXT(it); } Npy_DECREF(it); } return(0); }
private static void Npy_XDECREF(NpyArrayIterObject iterobject) { if (iterobject == null) { return; } Npy_DECREF(iterobject); }
static int NpyArray_IterSubscriptAssignSlice(NpyArrayIterObject self, NpyIndexSlice slice, NpyArray value) { NpyArray converted_value; npy_intp steps, start, step_size; bool swap; NpyArray_CopySwapFunc copyswap; NpyArrayIterObject value_iter = null; Npy_INCREF(self.ao.descr); converted_value = NpyArray_FromArray(value, self.ao.descr, 0); if (converted_value == null) { return(-1); } /* Copy in the data. */ value_iter = NpyArray_IterNew(converted_value); if (value_iter == null) { Npy_DECREF(converted_value); return(-1); } if (value_iter.size > 0) { steps = NpyArray_SliceSteps(slice); copyswap = self.ao.descr.f.copyswap; start = slice.start; step_size = slice.step; swap = (NpyArray_ISNOTSWAPPED(self.ao) != NpyArray_ISNOTSWAPPED(converted_value)); NpyArray_ITER_RESET(self); while (steps-- > 0) { NpyArray_ITER_GOTO1D(self, start); copyswap(self.dataptr, value_iter.dataptr, swap, self.ao); NpyArray_ITER_NEXT(value_iter); if (!NpyArray_ITER_NOTDONE(value_iter)) { NpyArray_ITER_RESET(value_iter); } start += step_size; } NpyArray_ITER_RESET(self); } Npy_DECREF(value_iter); Npy_DECREF(converted_value); return(0); }
internal static VoidPtr NpyArray_IterNext(NpyArrayIterObject it) { VoidPtr result = null; if (it.index < it.size - 1) { result = it.dataptr; NpyArray_ITER_NEXT(it); } return(result); }
//internal static NpyTypeObject NpyArrayNeighborhoodIter_Type = new NpyTypeObject() //{ // ntp_dealloc = null, // ntp_interface_alloc = null, //}; /* get the dataptr from its current coordinates for simple iterator */ static VoidPtr get_ptr_simple(NpyArrayIterObject iter, npy_intp[] coordinates) { VoidPtr ret = new VoidPtr(iter.ao); for (int i = 0; i < iter.ao.nd; ++i) { ret.data_offset += coordinates[i] * iter.strides[i]; } return(ret); }
/* * Get Iterator. */ internal static NpyArrayIterObject NpyArray_IterNew(NpyArray ao) { NpyArrayIterObject it = new NpyArrayIterObject(); NpyObject_Init(it, new NpyTypeObject()); if (it == null) { return(null); } array_iter_base_init(it, ao); /* Defer creation of the wrapper - will be handled by Npy_INTERFACE. */ return(it); }
//private static void GetViewOffsets(NpyArray arr, int dimIdx, npy_intp offset, npy_intp[] offsets, ref long offset_index) //{ // if (dimIdx == arr.nd) // { // offsets[offset_index++] = offset >> arr.ItemDiv; // } // else // { // for (int i = 0; i < arr.dimensions[dimIdx]; i++) // { // GetViewOffsets(arr, dimIdx + 1, offset + arr.strides[dimIdx] * i, offsets, ref offset_index); // } // } //} internal static npy_intp[] GetViewOffsets(NpyArrayIterObject iter, long count) { var itemDiv = iter.ao.ItemDiv; npy_intp[] offsets = new npy_intp[count]; for (int i = 0; i < count; i++) { offsets[i] = iter.dataptr.data_offset >> itemDiv; NpyArray_ITER_NEXT(iter); } return(offsets); }
internal static VoidPtr NpyArrayAccess_IterGoto1D(NpyArrayIterObject it, npy_intp index) { if (index < 0) { index += it.size; } if (index < 0 || index >= it.size) { string buf = string.Format("index out of bounds 0<=index<{0}", (long)it.size); NpyErr_SetString(npyexc_type.NpyExc_IndexError, buf); return(null); } NpyArray_ITER_RESET(it); NpyArray_ITER_GOTO1D(it, index); return(it.dataptr); }
internal static NpyArray NpyArray_IterSubscriptSlice(NpyArrayIterObject self, NpyIndexSlice slice) { NpyArray result; npy_intp[] steps = new npy_intp[1] { 0 }; npy_intp start, step_size; int elsize; bool swap; VoidPtr dptr; NpyArray_CopySwapFunc copyswap; /* Build the result. */ steps[0] = NpyArray_SliceSteps(slice); Npy_INCREF(self.ao.descr); result = NpyArray_Alloc(self.ao.descr, 1, steps, false, Npy_INTERFACE(self.ao)); if (result == null) { return(result); } /* Copy in the data. */ copyswap = result.descr.f.copyswap; start = slice.start; step_size = slice.step; elsize = result.descr.elsize; swap = (NpyArray_ISNOTSWAPPED(self.ao) != NpyArray_ISNOTSWAPPED(result)); dptr = new VoidPtr(result); NpyArray_ITER_RESET(self); while (steps[0]-- > 0) { NpyArray_ITER_GOTO1D(self, start); copyswap(dptr, self.dataptr, swap, result); dptr.data_offset += elsize; start += step_size; } NpyArray_ITER_RESET(self); return(result); }
static int array_iter_base_init(NpyArrayIterObject it, NpyArray ao) { int nd, i; it.nob_interface = null; it.nob_magic_number = npy_defs.NPY_VALID_MAGIC; nd = ao.nd; NpyArray_UpdateFlags(ao, NPYARRAYFLAGS.NPY_CONTIGUOUS); if (NpyArray_ISCONTIGUOUS(ao)) { it.contiguous = true; } else { it.contiguous = false; } Npy_INCREF(ao); it.ao = ao; it.size = NpyArray_SIZE(ao); it.nd_m1 = nd - 1; if (nd != 0) { it.factors[nd - 1] = 1; } for (i = 0; i < nd; i++) { it.dims_m1[i] = ao.dimensions[i] - 1; it.strides[i] = ao.strides[i]; it.backstrides[i] = it.strides[i] * it.dims_m1[i]; if (i > 0) { it.factors[nd - i - 1] = it.factors[nd - i] * ao.dimensions[nd - i]; } it.bounds[i, 0] = 0; it.bounds[i, 1] = ao.dimensions[i] - 1; it.limits[i, 0] = 0; it.limits[i, 1] = ao.dimensions[i] - 1; it.limits_sizes[i] = it.limits[i, 1] - it.limits[i, 0] + 1; } it.translate = get_ptr_simple; NpyArray_ITER_RESET(it); return(0); }
internal static void NpyArray_ITER_GOTO(NpyArrayIterObject it, npy_intp[] destination) { Debug.Assert(Validate(it)); int i; it.index = 0; it.dataptr = new VoidPtr(it.ao); for (i = it.nd_m1; i >= 0; i--) { if (destination[i] < 0) { destination[i] += it.dims_m1[i] + 1; } it.dataptr.data_offset += destination[i] * it.strides[i]; it.coordinates[i] = destination[i]; it.index += destination[i] * (i == it.nd_m1 ? 1 : it.dims_m1[i + 1] + 1); } }
internal static void NpyArray_CopyTo(NpyArray destArray, NpyArray srcArray, NPY_CASTING casting, NpyArray whereArray) { var destSize = NpyArray_Size(destArray); NumericOperations operations = NumericOperations.GetOperations(null, srcArray, destArray, whereArray); NpyArrayIterObject SrcIter = NpyArray_BroadcastToShape(srcArray, destArray.dimensions, destArray.nd); NpyArrayIterObject DestIter = NpyArray_BroadcastToShape(destArray, destArray.dimensions, destArray.nd); NpyArrayIterObject WhereIter = null; if (whereArray != null) { WhereIter = NpyArray_BroadcastToShape(whereArray, destArray.dimensions, destArray.nd); } bool whereValue = true; for (long i = 0; i < destSize; i++) { var srcValue = operations.srcGetItem(SrcIter.dataptr.data_offset - srcArray.data.data_offset, srcArray); if (WhereIter != null) { whereValue = (bool)operations.operandGetItem(WhereIter.dataptr.data_offset - whereArray.data.data_offset, whereArray); } if (whereValue) { operations.destSetItem(DestIter.dataptr.data_offset - destArray.data.data_offset, srcValue, destArray); } NpyArray_ITER_NEXT(SrcIter); NpyArray_ITER_NEXT(DestIter); if (WhereIter != null) { NpyArray_ITER_NEXT(WhereIter); } } return; }
public NpyArrayIterObject copy() { NpyArrayIterObject _copy = new NpyArrayIterObject(); _copy.nd_m1 = this.nd_m1; _copy.index = this.index; _copy.size = this.size; Array.Copy(this.coordinates, _copy.coordinates, this.coordinates.Length); Array.Copy(this.dims_m1, _copy.dims_m1, this.dims_m1.Length); Array.Copy(this.strides, _copy.strides, this.strides.Length); Array.Copy(this.backstrides, _copy.backstrides, this.backstrides.Length); Array.Copy(this.factors, _copy.factors, this.factors.Length); _copy.ao = this.ao; _copy.elsize = this.elsize; _copy.dataptr = new VoidPtr(this.dataptr); _copy.contiguous = this.contiguous; //Array.Copy(this.bounds, _copy.bounds, this.bounds.Length); //Array.Copy(this.limits, _copy.limits, this.limits.Length); //Array.Copy(this.limits_sizes, _copy.limits_sizes, this.limits_sizes.Length); return(_copy); }
internal static void NpyArray_Place(NpyArray arr, NpyArray mask, NpyArray vals) { var arrSize = NpyArray_Size(arr); var maskSize = NpyArray_Size(mask); if (arrSize != maskSize) { NpyErr_SetString(npyexc_type.NpyExc_ValueError, "size of mask must be same size as src arr"); return; } NumericOperations operations = NumericOperations.GetOperations(null, mask, arr, vals); NpyArrayIterObject valsIter = NpyArray_IterNew(vals); NpyArrayIterObject arrIter = NpyArray_IterNew(arr); NpyArrayIterObject maskIter = NpyArray_IterNew(mask); for (long i = 0; i < arrSize; i++) { bool whereValue = (bool)operations.srcGetItem(maskIter.dataptr.data_offset - mask.data.data_offset, mask); if (whereValue) { var valValue = operations.operandGetItem(valsIter.dataptr.data_offset - vals.data.data_offset, vals); operations.destSetItem(arrIter.dataptr.data_offset - arr.data.data_offset, valValue, arr); NpyArray_ITER_NEXT(valsIter); } if (!NpyArray_ITER_NOTDONE(valsIter)) { NpyArray_ITER_RESET(valsIter); } NpyArray_ITER_NEXT(arrIter); NpyArray_ITER_NEXT(maskIter); } return; }
internal static void NpyArray_ITER_NEXT(NpyArrayIterObject it) { Debug.Assert(Validate(it)); it.index++; if (it.nd_m1 == 0) { _NpyArray_ITER_NEXT1(it); } else if (it.contiguous) { it.dataptr.data_offset += (npy_intp)it.ao.descr.elsize; } else if (it.nd_m1 == 1) { _NpyArray_ITER_NEXT2(it); } else { int i; for (i = it.nd_m1; i >= 0; i--) { if (it.coordinates[i] < it.dims_m1[i]) { it.coordinates[i]++; it.dataptr.data_offset += it.strides[i]; break; } else { it.coordinates[i] = 0; it.dataptr.data_offset -= it.backstrides[i]; } } } }
internal static bool Validate(NpyArrayIterObject it) { return(null != it && npy_defs.NPY_VALID_MAGIC == it.nob_magic_number); }
internal static int NpyArray_ITEMSIZE(NpyArrayIterObject arr) { return(arr.ao.descr.elsize); }
internal static void NpyArray_CopyTo(NpyArray destArray, NpyArray srcArray, NPY_CASTING casting, NpyArray whereArray) { var destSize = NpyArray_Size(destArray); NumericOperations operations = NumericOperations.GetOperations(null, srcArray, destArray, whereArray); NpyArrayIterObject SrcIter = NpyArray_BroadcastToShape(srcArray, destArray.dimensions, destArray.nd); NpyArrayIterObject DestIter = NpyArray_BroadcastToShape(destArray, destArray.dimensions, destArray.nd); NpyArrayIterObject WhereIter = null; if (whereArray != null) { WhereIter = NpyArray_BroadcastToShape(whereArray, destArray.dimensions, destArray.nd); } IEnumerable <NpyArrayIterObject> srcParallelIters = NpyArray_ITER_ParallelSplit(SrcIter); IEnumerable <NpyArrayIterObject> destParallelIters = NpyArray_ITER_ParallelSplit(DestIter); IEnumerable <NpyArrayIterObject> whereParalleIters = null; if (WhereIter != null) { whereParalleIters = NpyArray_ITER_ParallelSplit(WhereIter); } Parallel.For(0, destParallelIters.Count(), index => //for (int index = 0; index < destParallelIters.Count(); index++) // { NpyArrayIterObject ldestIter = destParallelIters.ElementAt(index); NpyArrayIterObject lsrcIter = srcParallelIters.ElementAt(index); NpyArrayIterObject lwhereIter = null; bool whereValue = true; if (whereParalleIters != null) { lwhereIter = whereParalleIters.ElementAt(index); } while (ldestIter.index < ldestIter.size) { var srcValue = operations.srcGetItem(lsrcIter.dataptr.data_offset - srcArray.data.data_offset, srcArray); if (WhereIter != null) { whereValue = (bool)operations.operandGetItem(lwhereIter.dataptr.data_offset - whereArray.data.data_offset, whereArray); } if (whereValue) { operations.destSetItem(ldestIter.dataptr.data_offset - destArray.data.data_offset, srcValue, destArray); } NpyArray_ITER_PARALLEL_NEXT(ldestIter); NpyArray_ITER_PARALLEL_NEXT(lsrcIter); if (lwhereIter != null) { NpyArray_ITER_PARALLEL_NEXT(lwhereIter); } } }); return; }
internal static NpyArray NpyArray_InnerProduct(NpyArray ap1, NpyArray ap2, NPY_TYPES typenum) { npy_intp [] dimensions = new npy_intp[npy_defs.NPY_MAXDIMS]; npy_intp l = ap1.dimensions[ap1.nd - 1]; if (ap2.dimensions[ap2.nd - 1] != l) { NpyErr_SetString(npyexc_type.NpyExc_ValueError, "matrices are not aligned"); return(null); } int nd = ap1.nd + ap2.nd - 2; int j = 0; for (int i = 0; i < ap1.nd - 1; i++) { dimensions[j++] = ap1.dimensions[i]; } for (int i = 0; i < ap2.nd - 1; i++) { dimensions[j++] = ap2.dimensions[i]; } /* * Need to choose an output array that can hold a sum * -- use priority to determine which subtype. */ NpyArray ret = new_array_for_sum(ap1, ap2, nd, dimensions, typenum); if (ret == null) { return(null); } npy_intp is1 = ap1.strides[ap1.nd - 1]; npy_intp is2 = ap2.strides[ap2.nd - 1]; VoidPtr op = new VoidPtr(ret); npy_intp os = ret.descr.elsize; int axis = ap1.nd - 1; NpyArrayIterObject it1 = NpyArray_IterAllButAxis(ap1, ref axis); axis = ap2.nd - 1; NpyArrayIterObject it2 = NpyArray_IterAllButAxis(ap2, ref axis); var helper = MemCopy.GetMemcopyHelper(it1.dataptr); helper.InnerProduct(it1, it2, op, is1, is2, os, l); Npy_DECREF(it1); Npy_DECREF(it2); if (NpyErr_Occurred()) { goto fail; } return(ret); fail: Npy_DECREF(ret); return(null); }