public static NumericOperations GetOperations(UFuncOperation operationType, NumericOperation operation, NpyArray srcArray, NpyArray destArray, NpyArray operandArray)
        {
            NumericOperations operations = new NumericOperations();

            operations.operationType = operationType;
            operations.operation     = operation;

            if (srcArray != null)
            {
                operations.srcHelper       = GetNumericOperationsHelper(srcArray);
                operations._ConvertOperand = DefaultArrayHandlers.GetArrayHandler(srcArray.ItemType).MathOpConvertOperand;
            }

            if (destArray != null)
            {
                operations.destHelper      = GetNumericOperationsHelper(destArray);
                operations.destItemType    = destArray.ItemType;
                operations.destTypeIsFloat = numpyinternal.NpyTypeNum_ISFLOAT(destArray.ItemType);
            }

            if (operandArray != null)
            {
                operations.operHelper = GetNumericOperationsHelper(operandArray);
                operations.IsSrcAndOperandSameType = srcArray.ItemType == operandArray.ItemType;
            }


            return(operations);
        }
Exemple #2
0
        public static NumericOperations GetOperations(NumericOperation operation, NpyArray srcArray, NpyArray destArray, NpyArray operandArray)
        {
            NumericOperations operations = new NumericOperations();

            operations.operation = operation;

            if (srcArray != null)
            {
                operations.srcGetItem     = srcArray.descr.f.getitem;
                operations.srcSetItem     = srcArray.descr.f.setitem;
                operations.ConvertOperand = DefaultArrayHandlers.GetArrayHandler(srcArray.ItemType).MathOpConvertOperand;
            }

            if (destArray != null)
            {
                operations.destGetItem = destArray.descr.f.getitem;
                operations.destSetItem = destArray.descr.f.setitem;
            }

            if (operandArray != null)
            {
                operations.operandGetItem = operandArray.descr.f.getitem;
                operations.operandSetItem = operandArray.descr.f.setitem;
            }


            return(operations);
        }
        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;
        }
Exemple #4
0
        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;
        }
Exemple #5
0
        private static NpyArray NpyUFunc_PerformOuterOpArrayIter(NpyArray a, NpyArray b, NpyArray destArray, NumericOperations operations, UFuncOperation ops)
        {
            var destSize = NpyArray_Size(destArray);
            var aSize    = NpyArray_Size(a);
            var bSize    = NpyArray_Size(b);

            if (bSize == 0 || aSize == 0)
            {
                NpyArray_Resize(destArray, new NpyArray_Dims()
                {
                    len = 0, ptr = new npy_intp[] { }
                }, false, NPY_ORDER.NPY_ANYORDER);
                return(destArray);
            }


            IUFUNC_Operations UFunc = GetUFuncHandler(destArray.ItemType);

            if (UFunc != null && destArray.ItemType != NPY_TYPES.NPY_BOOL)
            {
                UFunc.PerformOuterOpArrayIter(a, b, destArray, operations, ops);
            }
            else
            {
                var      aIter   = NpyArray_IterNew(a);
                object[] aValues = new object[aSize];
                for (long i = 0; i < aSize; i++)
                {
                    aValues[i] = operations.srcGetItem(aIter.dataptr.data_offset - a.data.data_offset, a);
                    NpyArray_ITER_NEXT(aIter);
                }

                var      bIter   = NpyArray_IterNew(b);
                object[] bValues = new object[bSize];
                for (long i = 0; i < bSize; i++)
                {
                    bValues[i] = operations.operandGetItem(bIter.dataptr.data_offset - b.data.data_offset, b);
                    NpyArray_ITER_NEXT(bIter);
                }

                var DestIter = NpyArray_IterNew(destArray);

                for (long i = 0; i < aSize; i++)
                {
                    var aValue = aValues[i];

                    for (long j = 0; j < bSize; j++)
                    {
                        var bValue = bValues[j];

                        object destValue = operations.operation(aValue, operations.ConvertOperand(aValue, bValue));

                        try
                        {
                            operations.destSetItem(DestIter.dataptr.data_offset - destArray.data.data_offset, destValue, destArray);
                        }
                        catch
                        {
                            operations.destSetItem(DestIter.dataptr.data_offset - destArray.data.data_offset, 0, destArray);
                        }
                        NpyArray_ITER_NEXT(DestIter);
                    }
                }
            }

            if (HasBoolReturn(ops))
            {
                destArray = NpyArray_CastToType(destArray, NpyArray_DescrFromType(NPY_TYPES.NPY_BOOL), false);
            }
            return(destArray);
        }
Exemple #6
0
        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;
        }