Exemplo n.º 1
0
 def test_pow(self):
     self.failUnlessRaises(TypeError, operator.pow)
     self.failUnlessRaises(TypeError, operator.pow, None, None)
     self.failUnless(operator.pow(3,5) == 3**5)
     self.failUnless(operator.__pow__(3,5) == 3**5)
     self.assertRaises(TypeError, operator.pow, 1)
     self.assertRaises(TypeError, operator.pow, 1, 2, 3)
Exemplo n.º 2
0
 def test_pow(self):
     self.assertRaises(TypeError, operator.pow)
     self.assertRaises(TypeError, operator.pow, None, None)
     self.assertTrue(operator.pow(3, 5) == 3**5)
     self.assertTrue(operator.__pow__(3, 5) == 3**5)
     self.assertRaises(TypeError, operator.pow, 1)
     self.assertRaises(TypeError, operator.pow, 1, 2, 3)
 def test_pow(self):
     self.assertRaises(TypeError, operator.pow)
     self.assertRaises(TypeError, operator.pow, None, None)
     self.assertEqual(operator.pow(3, 5), 3 ** 5)
     self.assertEqual(operator.__pow__(3, 5), 3 ** 5)
     self.assertRaises(TypeError, operator.pow, 1)
     self.assertRaises(TypeError, operator.pow, 1, 2, 3)
 def test_pow(self):
     self.failUnlessRaises(TypeError, operator.pow)
     self.failUnlessRaises(TypeError, operator.pow, None, None)
     self.assertEqual(operator.pow(3, 5), 3**5)
     self.assertEqual(operator.__pow__(3, 5), 3**5)
     self.assertRaises(TypeError, operator.pow, 1)
     self.assertRaises(TypeError, operator.pow, 1, 2, 3)
Exemplo n.º 5
0
    def __pow__(self, other, modulo=_not_given):
        """
        self ** other, pow(self, other)

         pow(self, other, modulo)
        """
        if modulo is _not_given:
            return __pow__(get_wrapped_object(self), get_wrapped_object(other))
        return pow(get_wrapped_object(self), get_wrapped_object(other),
                   get_wrapped_object(modulo))
Exemplo n.º 6
0
    operator.__le__,
    operator.__eq__,
    operator.__ne__,
    operator.__ge__,
    operator.__gt__,
]

decomposeunary = [
    operator.__abs__,
]

decomposebinary = [
    operator.__add__,
    operator.__mul__,
    operator.__pow__,
    lambda x,y: operator.__pow__(y,x),
]

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

def lcm(a, b):
    return a * b // gcd(a, b)

logging = 1
def getmessage(string):
    if logging:
        return string
    return ''
Exemplo n.º 7
0
 def __pow__(self, value):
     return operator.__pow__(self._tensor, value)
Exemplo n.º 8
0
 def __rpow__(self, other):
     with self._lock:
         return operator.__pow__(other, self.__wrapped__)
Exemplo n.º 9
0
rate = 0.9
dollar = {'under_my_bed': 1000, 'jeans': 45, 'bank': 5000}
print(sum(map(partial(mul, rate), dollar.values())))

# Series and Parallel Mapping
insects = ['fly', 'ant', 'beetle', 'cankerworm']
f = lambda x: x + 'is an insect'
print(list(map(f, insects)))

# Exponentiation
# Exponentiation Using builtins: ** and pow()
import operator

operator.pow(4, 2)
operator.__pow__(4, 3)
# Square root:
import math

n = math.sqrt(9)
p = math.sqrt(11.11)
print(n, p)


def modular_inverse(x, p):
    """Find a such as ax == 1 (mod p), assuming p is prime."""
    return pow(x, p - 2, p)


print([modular_inverse(x, 13) for x in range(1, 13)])
Exemplo n.º 10
0
 def test_pow(self):
     self.failUnless(operator.pow(3,5) == 3**5)
     self.failUnless(operator.__pow__(3,5) == 3**5)
     self.assertRaises(TypeError, operator.pow, 1)
     self.assertRaises(TypeError, operator.pow, 1, 2, 3)
with higher precision than math.exp(x) or cmath.exp(x) would allow:

Section 70.8: Magic methods and exponentiation: builtin, math and cmath

Section 70.9: Roots: nth-root with fractional exponents
While the math.sqrt function is provided for the specific case of square roots, it's often convenient to use the
exponentiation operator (**) with fractional exponents to perform nth-root operations, like cube roots


"""

#Section 70.1: Exponentiation using builtins: ** and pow()
print("------Section 70.1: Exponentiation using builtins: ** and pow()-------")
import operator
print(operator.pow(4, 2))  # 16
print(operator.__pow__(4, 3))  # 64
val1, val2 = 4, 2
print(val1.__pow__(val2))  # 16
print(val2.__rpow__(val1))  # 16

#Section 70.2: Square root: math.sqrt() and cmath.sqrt
print("---Section 70.2: Square root: math.sqrt() and cmath.sqrt---------")

import math
import cmath
math.sqrt(9)  # 3.0
math.sqrt(11.11)  # 3.3331666624997918
#math.sqrt(Decimal('6.25')) # 2.5

#Section 70.3: Modular exponentiation: pow() with 3 arguments
print(
Exemplo n.º 12
0
 def __rpow__(self, other):
     """other ** self, pow(other, self)"""
     return __pow__(other, get_wrapped_object(self))
Exemplo n.º 13
0
 def update_event(self, inp=-1):
     self.set_output_val(0, operator.__pow__(self.input(0), self.input(1)))
Exemplo n.º 14
0
               doc=operator.ne.__doc__)

or_ = spice(lambda x, y: operator.or_(x, y),
            name='or_',
            doc=operator.or_.__doc__)
__or__ = spice(lambda x, y: operator.__or__(x, y),
               name='__or__',
               doc=operator.or_.__doc__)

pos = spice(lambda x, y: operator.pos(x, y),
            name='pos',
            doc=operator.pos.__doc__)

#reversed
pow = spice(lambda x, y: operator.pow(y, x), name='pow')
__pow__ = spice(lambda x, y: operator.__pow__(y, x), name='__pow__')

# reversed
rshift = spice(lambda x, y: operator.rshift(y, x), name='rshift')
__rshift__ = spice(lambda x, y: operator.__rshift__(y, x), name='__rshift__')

# reversed
sub = spice(lambda x, y: operator.sub(y, x), name='sub')
__sub__ = spice(lambda x, y: operator.__sub__(y, x), name='__sub__')

# reversed
truediv = spice(lambda x, y: operator.truediv(y, x), name='truediv')
__truediv__ = spice(lambda x, y: operator.__truediv__(y, x),
                    name='__truediv__')

xor = spice(lambda x, y: operator.xor(x, y),
Exemplo n.º 15
0
def safePow( left, right ):
	if abs( right ) > POW_RIGHT_MAX:
		raise UnsafeOperatorError( "'** X' cannot exceed " + str( POW_RIGHT_MAX ) )
	elif abs( left ) > POW_LEFT_MAX:
		raise UnsafeOperatorError( "'X **' cannot exceed " + str( POW_LEFT_MAX ) )
	return operator.__pow__( left, right )