Python operator.truediv() Examples

The following are 30 code examples of operator.truediv(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module operator , or try the search function .
Example #1
Source File: test_series.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_binary_operators(self):

        # skipping for now #####
        import pytest
        pytest.skip("skipping sparse binary operators test")

        def _check_inplace_op(iop, op):
            tmp = self.bseries.copy()

            expected = op(tmp, self.bseries)
            iop(tmp, self.bseries)
            tm.assert_sp_series_equal(tmp, expected)

        inplace_ops = ['add', 'sub', 'mul', 'truediv', 'floordiv', 'pow']
        for op in inplace_ops:
            _check_inplace_op(getattr(operator, "i%s" % op),
                              getattr(operator, op)) 
Example #2
Source File: test_series.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_binary_operators(self):

        # skipping for now #####
        import pytest
        pytest.skip("skipping sparse binary operators test")

        def _check_inplace_op(iop, op):
            tmp = self.bseries.copy()

            expected = op(tmp, self.bseries)
            iop(tmp, self.bseries)
            tm.assert_sp_series_equal(tmp, expected)

        inplace_ops = ['add', 'sub', 'mul', 'truediv', 'floordiv', 'pow']
        for op in inplace_ops:
            _check_inplace_op(getattr(operator, "i%s" % op),
                              getattr(operator, op)) 
Example #3
Source File: test_panel.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_arith(self):
        self._test_op(self.panel, operator.add)
        self._test_op(self.panel, operator.sub)
        self._test_op(self.panel, operator.mul)
        self._test_op(self.panel, operator.truediv)
        self._test_op(self.panel, operator.floordiv)
        self._test_op(self.panel, operator.pow)

        self._test_op(self.panel, lambda x, y: y + x)
        self._test_op(self.panel, lambda x, y: y - x)
        self._test_op(self.panel, lambda x, y: y * x)
        self._test_op(self.panel, lambda x, y: y / x)
        self._test_op(self.panel, lambda x, y: y ** x)

        self._test_op(self.panel, lambda x, y: x + y)  # panel + 1
        self._test_op(self.panel, lambda x, y: x - y)  # panel - 1
        self._test_op(self.panel, lambda x, y: x * y)  # panel * 1
        self._test_op(self.panel, lambda x, y: x / y)  # panel / 1
        self._test_op(self.panel, lambda x, y: x ** y)  # panel ** 1

        pytest.raises(Exception, self.panel.__add__,
                      self.panel['ItemA']) 
Example #4
Source File: test_classes.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_truediv(Poly):
    # true division is valid only if the denominator is a Number and
    # not a python bool.
    p1 = Poly([1,2,3])
    p2 = p1 * 5

    for stype in np.ScalarType:
        if not issubclass(stype, Number) or issubclass(stype, bool):
            continue
        s = stype(5)
        assert_poly_almost_equal(op.truediv(p2, s), p1)
        assert_raises(TypeError, op.truediv, s, p2)
    for stype in (int, long, float):
        s = stype(5)
        assert_poly_almost_equal(op.truediv(p2, s), p1)
        assert_raises(TypeError, op.truediv, s, p2)
    for stype in [complex]:
        s = stype(5, 0)
        assert_poly_almost_equal(op.truediv(p2, s), p1)
        assert_raises(TypeError, op.truediv, s, p2)
    for s in [tuple(), list(), dict(), bool(), np.array([1])]:
        assert_raises(TypeError, op.truediv, p2, s)
        assert_raises(TypeError, op.truediv, s, p2)
    for ptype in classes:
        assert_raises(TypeError, op.truediv, p2, ptype(1)) 
Example #5
Source File: base.py    From recruit with Apache License 2.0 6 votes vote down vote up
def _add_numeric_methods_binary(cls):
        """
        Add in numeric methods.
        """
        cls.__add__ = _make_arithmetic_op(operator.add, cls)
        cls.__radd__ = _make_arithmetic_op(ops.radd, cls)
        cls.__sub__ = _make_arithmetic_op(operator.sub, cls)
        cls.__rsub__ = _make_arithmetic_op(ops.rsub, cls)
        cls.__rpow__ = _make_arithmetic_op(ops.rpow, cls)
        cls.__pow__ = _make_arithmetic_op(operator.pow, cls)

        cls.__truediv__ = _make_arithmetic_op(operator.truediv, cls)
        cls.__rtruediv__ = _make_arithmetic_op(ops.rtruediv, cls)
        if not compat.PY3:
            cls.__div__ = _make_arithmetic_op(operator.div, cls)
            cls.__rdiv__ = _make_arithmetic_op(ops.rdiv, cls)

        # TODO: rmod? rdivmod?
        cls.__mod__ = _make_arithmetic_op(operator.mod, cls)
        cls.__floordiv__ = _make_arithmetic_op(operator.floordiv, cls)
        cls.__rfloordiv__ = _make_arithmetic_op(ops.rfloordiv, cls)
        cls.__divmod__ = _make_arithmetic_op(divmod, cls)
        cls.__mul__ = _make_arithmetic_op(operator.mul, cls)
        cls.__rmul__ = _make_arithmetic_op(ops.rmul, cls) 
Example #6
Source File: ops.py    From recruit with Apache License 2.0 6 votes vote down vote up
def __call__(self, env):
        """Recursively evaluate an expression in Python space.

        Parameters
        ----------
        env : Scope

        Returns
        -------
        object
            The result of an evaluated expression.
        """
        # handle truediv
        if self.op == '/' and env.scope['truediv']:
            self.func = op.truediv

        # recurse over the left/right nodes
        left = self.lhs(env)
        right = self.rhs(env)

        return self.func(left, right) 
Example #7
Source File: ops.py    From recruit with Apache License 2.0 6 votes vote down vote up
def _gen_fill_zeros(name):
    """
    Find the appropriate fill value to use when filling in undefined values
    in the results of the given operation caused by operating on
    (generally dividing by) zero.

    Parameters
    ----------
    name : str

    Returns
    -------
    fill_value : {None, np.nan, np.inf}
    """
    name = name.strip('__')
    if 'div' in name:
        # truediv, floordiv, div, and reversed variants
        fill_value = np.inf
    elif 'mod' in name:
        # mod, rmod
        fill_value = np.nan
    else:
        fill_value = None
    return fill_value 
Example #8
Source File: test_classes.py    From auto-alt-text-lambda-api with MIT License 6 votes vote down vote up
def check_truediv(Poly):
    # true division is valid only if the denominator is a Number and
    # not a python bool.
    p1 = Poly([1,2,3])
    p2 = p1 * 5

    for stype in np.ScalarType:
        if not issubclass(stype, Number) or issubclass(stype, bool):
            continue
        s = stype(5)
        assert_poly_almost_equal(op.truediv(p2, s), p1)
        assert_raises(TypeError, op.truediv, s, p2)
    for stype in (int, long, float):
        s = stype(5)
        assert_poly_almost_equal(op.truediv(p2, s), p1)
        assert_raises(TypeError, op.truediv, s, p2)
    for stype in [complex]:
        s = stype(5, 0)
        assert_poly_almost_equal(op.truediv(p2, s), p1)
        assert_raises(TypeError, op.truediv, s, p2)
    for s in [tuple(), list(), dict(), bool(), np.array([1])]:
        assert_raises(TypeError, op.truediv, p2, s)
        assert_raises(TypeError, op.truediv, s, p2)
    for ptype in classes:
        assert_raises(TypeError, op.truediv, p2, ptype(1)) 
Example #9
Source File: test_classes.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def check_truediv(Poly):
    # true division is valid only if the denominator is a Number and
    # not a python bool.
    p1 = Poly([1,2,3])
    p2 = p1 * 5

    for stype in np.ScalarType:
        if not issubclass(stype, Number) or issubclass(stype, bool):
            continue
        s = stype(5)
        assert_poly_almost_equal(op.truediv(p2, s), p1)
        assert_raises(TypeError, op.truediv, s, p2)
    for stype in (int, long, float):
        s = stype(5)
        assert_poly_almost_equal(op.truediv(p2, s), p1)
        assert_raises(TypeError, op.truediv, s, p2)
    for stype in [complex]:
        s = stype(5, 0)
        assert_poly_almost_equal(op.truediv(p2, s), p1)
        assert_raises(TypeError, op.truediv, s, p2)
    for s in [tuple(), list(), dict(), bool(), np.array([1])]:
        assert_raises(TypeError, op.truediv, p2, s)
        assert_raises(TypeError, op.truediv, s, p2)
    for ptype in classes:
        assert_raises(TypeError, op.truediv, p2, ptype(1)) 
Example #10
Source File: test_panel.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_arith(self):
        with catch_warnings(record=True):
            self._test_op(self.panel, operator.add)
            self._test_op(self.panel, operator.sub)
            self._test_op(self.panel, operator.mul)
            self._test_op(self.panel, operator.truediv)
            self._test_op(self.panel, operator.floordiv)
            self._test_op(self.panel, operator.pow)

            self._test_op(self.panel, lambda x, y: y + x)
            self._test_op(self.panel, lambda x, y: y - x)
            self._test_op(self.panel, lambda x, y: y * x)
            self._test_op(self.panel, lambda x, y: y / x)
            self._test_op(self.panel, lambda x, y: y ** x)

            self._test_op(self.panel, lambda x, y: x + y)  # panel + 1
            self._test_op(self.panel, lambda x, y: x - y)  # panel - 1
            self._test_op(self.panel, lambda x, y: x * y)  # panel * 1
            self._test_op(self.panel, lambda x, y: x / y)  # panel / 1
            self._test_op(self.panel, lambda x, y: x ** y)  # panel ** 1

            pytest.raises(Exception, self.panel.__add__,
                          self.panel['ItemA']) 
Example #11
Source File: fft.py    From scarlet with MIT License 6 votes vote down vote up
def match_psfs(psf1, psf2, padding=3, axes=(-2, -1)):
    """Calculate the difference kernel between two psfs

    Parameters
    ----------
    psf1: `Fourier`
        `Fourier` object representing the psf and it's FFT.
    psf2: `Fourier`
        `Fourier` object representing the psf and it's FFT.
    padding: int
        Additional padding to use when generating the FFT
        to supress artifacts.
    axes: tuple or None
        Axes that contain the spatial information for the PSFs.
    """
    if psf1.shape[0] < psf2.shape[0]:
        shape = psf2.shape
    else:
        shape = psf1.shape
    return _kspace_operation(psf1, psf2, padding, operator.truediv, shape, axes=axes) 
Example #12
Source File: fft.py    From scarlet with MIT License 6 votes vote down vote up
def _kspace_operation(image1, image2, padding, op, shape, axes):
    """Combine two images in k-space using a given `operator`

    `image1` and `image2` are required to be `Fourier` objects and
    `op` should be an operator (either `operator.mul` for a convolution
    or `operator.truediv` for deconvolution). `shape` is the shape of the
    output image (`Fourier` instance).
    """
    if len(image1.shape) != len(image2.shape):
        msg = "Both images must have the same number of axes, got {0} and {1}"
        raise Exception(msg.format(len(image1.shape), len(image2.shape)))
    fft_shape = _get_fft_shape(image1.image, image2.image, padding, axes)
    convolved_fft = op(image1.fft(fft_shape, axes), image2.fft(fft_shape, axes))
    # why is shape not image1.shape? images are never padded
    convolved = Fourier.from_fft(convolved_fft, fft_shape, shape, axes)
    return convolved 
Example #13
Source File: elegant_lattice_converter.py    From ocelot with GNU General Public License v3.0 5 votes vote down vote up
def calc_rpn(self, expression, constants={}, info=''):
        """
        Calculation of the expression written in reversed polish notation
        """
        
        operators = {'+': operator.add, '-': operator.sub, '*': operator.mul, '/': operator.truediv}
        functions = {'SIN': np.sin, 'COS': np.cos, 'TAN': np.tan, 'ASIN': np.arcsin, 'ACOS': np.arccos, 'ATAN': np.arctan, 'SQRT': np.sqrt}
        
        stack = [0]
        for token in expression.split(" "):
            
            if token in operators:
                op2, op1 = stack.pop(), stack.pop()
                stack.append(operators[token](op1, op2))
                
            elif token in constants:
                stack.append(constants[token])
            
            elif token in functions:
                op = stack.pop()
                stack.append(functions[token](op))
                
            elif token:
                try:
                    stack.append(float(token))
                except ValueError:
                    print('********* ERROR! NO ' + token + ' in function conversion or constants list in calc_rpn function. ' + info)
                    sys.exit()
        
        return stack.pop() 
Example #14
Source File: bytevector.py    From pycoind with MIT License 5 votes vote down vote up
def __itruediv__(self, other):
        self.set_value(operators.truediv(self.value, other.value)) 
Example #15
Source File: euclid.py    From renpy-shader with MIT License 5 votes vote down vote up
def __rtruediv__(self, other):
        assert type(other) in (int, long, float)
        return Vector3(operator.truediv(other, self.x),
                       operator.truediv(other, self.y),
                       operator.truediv(other, self.z)) 
Example #16
Source File: euclid.py    From renpy-shader with MIT License 5 votes vote down vote up
def __truediv__(self, other):
        assert type(other) in (int, long, float)
        return Vector3(operator.truediv(self.x, other),
                       operator.truediv(self.y, other),
                       operator.truediv(self.z, other)) 
Example #17
Source File: euclid.py    From renpy-shader with MIT License 5 votes vote down vote up
def __rtruediv__(self, other):
        assert type(other) in (int, long, float)
        return Vector2(operator.truediv(other, self.x),
                       operator.truediv(other, self.y)) 
Example #18
Source File: test_operator.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_truediv(self):
        self.assertRaises(TypeError, operator.truediv, 5)
        self.assertRaises(TypeError, operator.truediv, None, None)
        self.assertTrue(operator.truediv(5, 2) == 2.5) 
Example #19
Source File: wrappers.py    From python-netsurv with MIT License 5 votes vote down vote up
def __truediv__(self, other):
        return operator.truediv(self.__wrapped__, other) 
Example #20
Source File: bytevector.py    From pycoind with MIT License 5 votes vote down vote up
def __truediv__(self, other):
        return ByteVector.from_value(operator.truediv(self.value, other.value))


    # In-place operators 
Example #21
Source File: simple.py    From python-netsurv with MIT License 5 votes vote down vote up
def __rtruediv__(self, other):
        return operator.truediv(other, self.__wrapped__) 
Example #22
Source File: slots.py    From python-netsurv with MIT License 5 votes vote down vote up
def __rtruediv__(self, other):
        return operator.truediv(other, self.__wrapped__) 
Example #23
Source File: slots.py    From python-netsurv with MIT License 5 votes vote down vote up
def __truediv__(self, other):
        return operator.truediv(self.__wrapped__, other) 
Example #24
Source File: algebraic.py    From fungrim with MIT License 5 votes vote down vote up
def __truediv__(a, b):
        return alg._binop(a, b, operator.truediv) 
Example #25
Source File: algebraic.py    From fungrim with MIT License 5 votes vote down vote up
def __rdiv__(a, b):
        return alg._binop(b, a, operator.truediv) 
Example #26
Source File: bytevector.py    From pycoind with MIT License 5 votes vote down vote up
def __div__(self, other):
        return ByteVector.from_value(operator.truediv(self.value, other.value)) 
Example #27
Source File: algebraic.py    From fungrim with MIT License 5 votes vote down vote up
def __div__(a, b):
        return alg._binop(a, b, operator.truediv) 
Example #28
Source File: algebraic.py    From fungrim with MIT License 5 votes vote down vote up
def test_composed_op():
    from random import randrange
    for N in range(100):
        while 1:
            D1 = randrange(10)
            D2 = randrange(10)
            F = fmpz_poly([randrange(-2,3) for n in range(D1)])
            G = fmpz_poly([randrange(-2,3) for n in range(D2)])
            if F.degree() >= 1 and G.degree() >= 1:
                break
        H = composed_op(F, G, operator.add)
        for x, r in F.roots():
            for y, r in G.roots():
                v = H(x+y)
                assert v.contains(0)
        H = composed_op(F, G, operator.sub)
        for x, r in F.roots():
            for y, r in G.roots():
                v = H(x-y)
                assert v.contains(0)
        H = composed_op(F, G, operator.mul)
        for x, r in F.roots():
            for y, r in G.roots():
                v = H(x*y)
                assert v.contains(0)
        if G[0] != 0:
            H = composed_op(F, G, operator.truediv)
            for x, r in F.roots():
                for y, r in G.roots():
                    v = H(x/y)
                    assert v.contains(0) 
Example #29
Source File: truediv.py    From mars with Apache License 2.0 5 votes vote down vote up
def truediv(df, other, axis='columns', level=None, fill_value=None):
    op = DataFrameTrueDiv(axis=axis, level=level, fill_value=fill_value, lhs=df, rhs=other)
    return op(df, other) 
Example #30
Source File: bytevector.py    From pycoind with MIT License 5 votes vote down vote up
def __idiv__(self, other):
        self.set_value(operators.truediv(self.value, other.value))