Example #1
0
        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);
        }
Example #2
0
        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);
        }