def test_inequalities(self): cases = [ ('Less', (1, 2), True), ('Less', (1, 1), False), ('Less', (2, 1), False), ('LessEqual', (1, 2), True), ('LessEqual', (1, 1), True), ('LessEqual', (2, 1), False), ('Greater', (1, 2), False), ('Greater', (1, 1), False), ('Greater', (2, 1), True), ('GreaterEqual', (1, 2), False), ('GreaterEqual', (1, 1), True), ('GreaterEqual', (2, 1), True), ] for head, args, result in cases: check = getattr(self, 'assert' + str(result)) expr = Expression(head, Symbol('x'), Symbol('y')) int_args = [ CompileArg('System`x', int_type), CompileArg('System`y', int_type) ] cfunc = _compile(expr, int_args) check(cfunc(*args)) real_args = [ CompileArg('System`x', real_type), CompileArg('System`y', real_type) ] cfunc = _compile(expr, real_args) check(cfunc(*(float(arg) for arg in args)))
def test_inequalities(self): cases = [ ("Less", (1, 2), True), ("Less", (1, 1), False), ("Less", (2, 1), False), ("LessEqual", (1, 2), True), ("LessEqual", (1, 1), True), ("LessEqual", (2, 1), False), ("Greater", (1, 2), False), ("Greater", (1, 1), False), ("Greater", (2, 1), True), ("GreaterEqual", (1, 2), False), ("GreaterEqual", (1, 1), True), ("GreaterEqual", (2, 1), True), ] for head, args, result in cases: check = getattr(self, "assert" + str(result)) expr = Expression(head, Symbol("x"), Symbol("y")) int_args = [ CompileArg("System`x", int_type), CompileArg("System`y", int_type), ] cfunc = _compile(expr, int_args) check(cfunc(*args)) real_args = [ CompileArg("System`x", real_type), CompileArg("System`y", real_type), ] cfunc = _compile(expr, real_args) check(cfunc(*(float(arg) for arg in args)))
def test_int_real_equal(self): expr = Expression("Equal", Symbol("x"), Symbol("y")) args = [CompileArg("System`x", real_type), CompileArg("System`y", int_type)] cfunc = _compile(expr, args) self.assertTrue(cfunc(3.0, 3)) self.assertFalse(cfunc(3.0, 2)) self.assertFalse(cfunc(2.0, 3))
def test_c(self): expr = Expression( "Plus", Expression("Plus", Symbol("x"), Symbol("y")), Integer(5) ) args = [CompileArg("System`x", int_type), CompileArg("System`y", real_type)] cfunc = _compile(expr, args) self.assertTypeEqual(cfunc(1, 2.5), 8.5)
def test_b(self): expr = Expression('Plus', Symbol('x'), Symbol('y')) args = [ CompileArg('System`x', int_type), CompileArg('System`y', real_type) ] cfunc = _compile(expr, args) self.assertTypeEqual(cfunc(1, 2.5), 3.5)
def test_a(self): expr = Expression('Plus', Symbol('x'), Integer(2)) args = [ CompileArg('System`x', int_type), CompileArg('System`y', int_type) ] cfunc = _compile(expr, args) self.assertTypeEqual(cfunc(2, 4), 4)
def test_if_return3(self): expr = Expression( "If", Symbol("x"), Symbol("y"), Expression("Return", Integer(3)) ) args = [CompileArg("System`x", bool_type), CompileArg("System`y", int_type)] cfunc = _compile(expr, args) self.assertTypeEqual(cfunc(True, 1), 1) self.assertTypeEqual(cfunc(False, 1), 3)
def test_div0(self): expr = Expression("Power", Symbol("x"), Symbol("y")) args = [CompileArg("System`x", real_type), CompileArg("System`y", real_type)] cfunc = _compile(expr, args) self.assertTypeEqual(cfunc(0.0, -1.5), float("+inf")) self.assertTypeEqual(cfunc(0.0, -1.0), float("+inf")) self.assertTypeEqual(cfunc(-0.0, -1.0), float("-inf")) self.assertTypeEqual(cfunc(0.0, 0.0), float("1.0")) # NaN?
def test_pow_int(self): expr = Expression('Power', Symbol('x'), Symbol('y')) args = [ CompileArg('System`x', int_type), CompileArg('System`y', int_type) ] cfunc = _compile(expr, args) self.assertTypeEqual(cfunc(4, 9), 4**9)
def test_int_unequal(self): expr = Expression("Unequal", Symbol("x"), Symbol("y"), Integer(3)) args = [CompileArg("System`x", int_type), CompileArg("System`y", int_type)] cfunc = _compile(expr, args) self.assertTrue(cfunc(1, 2)) self.assertFalse(cfunc(3, 2)) self.assertFalse(cfunc(2, 3)) self.assertFalse(cfunc(2, 2)) self.assertFalse(cfunc(3, 3))
def test_int_real_equal(self): expr = Expression('Equal', Symbol('x'), Symbol('y')) args = [ CompileArg('System`x', real_type), CompileArg('System`y', int_type) ] cfunc = _compile(expr, args) self.assertTrue(cfunc(3.0, 3)) self.assertFalse(cfunc(3.0, 2)) self.assertFalse(cfunc(2.0, 3))
def test_if_return_error(self): expr = Expression('If', Symbol('x'), Expression('Return', Symbol('y')), Expression('Return', Symbol('z'))) args = [ CompileArg('System`x', bool_type), CompileArg('System`y', int_type), CompileArg('System`z', bool_type) ] with self.assertRaises(CompileError): _compile(expr, args)
def test_if_return3(self): expr = Expression('If', Symbol('x'), Symbol('y'), Expression('Return', Integer(3))) args = [ CompileArg('System`x', bool_type), CompileArg('System`y', int_type) ] cfunc = _compile(expr, args) self.assertTypeEqual(cfunc(True, 1), 1) self.assertTypeEqual(cfunc(False, 1), 3)
def test_if(self): expr = Expression('If', Symbol('x'), Symbol('y'), Symbol('z')) args = [ CompileArg('System`x', int_type), CompileArg('System`y', real_type), CompileArg('System`z', real_type) ] cfunc = _compile(expr, args) self.assertTypeEqual(cfunc(0, 3.0, 4.0), 4.0) self.assertTypeEqual(cfunc(1, 3.0, 4.0), 3.0) self.assertTypeEqual(cfunc(2, 3.0, 4.0), 3.0)
def test_div0(self): expr = Expression('Power', Symbol('x'), Symbol('y')) args = [ CompileArg('System`x', real_type), CompileArg('System`y', real_type) ] cfunc = _compile(expr, args) self.assertTypeEqual(cfunc(0.0, -1.5), float('+inf')) self.assertTypeEqual(cfunc(0.0, -1.0), float('+inf')) self.assertTypeEqual(cfunc(-0.0, -1.0), float('-inf')) self.assertTypeEqual(cfunc(0.0, 0.0), float('1.0')) # NaN?
def _test_binary_math(self, name, fn, check_type=True): expr = Expression(name, Symbol("x"), Symbol("y")) for xtype, ytype in itertools.product([int_type, real_type], repeat=2): args = [CompileArg("System`x", xtype), CompileArg("System`y", ytype)] cfunc = _compile(expr, args) for _ in range(1000): x = self._random(xtype) y = self._random(ytype) py_result = self._py_evaluate(fn, x, y) c_result = cfunc(x, y) self.assertNumEqual(py_result, c_result, check_type)
def test_if(self): expr = Expression("If", Symbol("x"), Symbol("y"), Symbol("z")) args = [ CompileArg("System`x", int_type), CompileArg("System`y", real_type), CompileArg("System`z", real_type), ] cfunc = _compile(expr, args) self.assertTypeEqual(cfunc(0, 3.0, 4.0), 4.0) self.assertTypeEqual(cfunc(1, 3.0, 4.0), 3.0) self.assertTypeEqual(cfunc(2, 3.0, 4.0), 3.0)
def test_if_cont(self): expr = Expression( 'Plus', Integer(1), Expression('If', Symbol('x'), Expression('Sin', Symbol('y')), Expression('Cos', Symbol('y')))) args = [ CompileArg('System`x', int_type), CompileArg('System`y', real_type) ] cfunc = _compile(expr, args) self.assertTypeEqual(cfunc(0, 0.0), 2.0) self.assertTypeEqual(cfunc(1, 0.0), 1.0)
def test_if_bool_bool(self): expr = Expression('If', Symbol('x'), Symbol('y'), Symbol('z')) args = [ CompileArg('System`x', bool_type), CompileArg('System`y', bool_type), CompileArg('System`z', bool_type) ] cfunc = _compile(expr, args) self.assertTrue(cfunc(True, True, False)) self.assertTrue(cfunc(False, False, True)) self.assertFalse(cfunc(True, False, True)) self.assertFalse(cfunc(False, True, False))
def test_int_unequal(self): expr = Expression('Unequal', Symbol('x'), Symbol('y'), Integer(3)) args = [ CompileArg('System`x', int_type), CompileArg('System`y', int_type) ] cfunc = _compile(expr, args) self.assertTrue(cfunc(1, 2)) self.assertFalse(cfunc(3, 2)) self.assertFalse(cfunc(2, 3)) self.assertFalse(cfunc(2, 2)) self.assertFalse(cfunc(3, 3))
def test_if_bool_bool(self): expr = Expression("If", Symbol("x"), Symbol("y"), Symbol("z")) args = [ CompileArg("System`x", bool_type), CompileArg("System`y", bool_type), CompileArg("System`z", bool_type), ] cfunc = _compile(expr, args) self.assertTrue(cfunc(True, True, False)) self.assertTrue(cfunc(False, False, True)) self.assertFalse(cfunc(True, False, True)) self.assertFalse(cfunc(False, True, False))
def test_if_return_error(self): expr = Expression( "If", Symbol("x"), Expression("Return", Symbol("y")), Expression("Return", Symbol("z")), ) args = [ CompileArg("System`x", bool_type), CompileArg("System`y", int_type), CompileArg("System`z", bool_type), ] with self.assertRaises(CompileError): _compile(expr, args)
def test_if_cont(self): expr = Expression( "Plus", Integer(1), Expression( "If", Symbol("x"), Expression("Sin", Symbol("y")), Expression("Cos", Symbol("y")), ), ) args = [CompileArg("System`x", int_type), CompileArg("System`y", real_type)] cfunc = _compile(expr, args) self.assertTypeEqual(cfunc(0, 0.0), 2.0) self.assertTypeEqual(cfunc(1, 0.0), 1.0)
def test_d(self): expr = Expression( "Plus", Symbol("x"), MachineReal(1.5), Integer(2), Symbol("x") ) args = [CompileArg("System`x", real_type)] cfunc = _compile(expr, args) self.assertTypeEqual(cfunc(2.5), 8.5)
def test_if_eq(self): expr = Expression('If', Expression('Equal', Symbol('x'), Integer(1)), Integer(2), Integer(3)) args = [CompileArg('System`x', int_type)] cfunc = _compile(expr, args) self.assertTypeEqual(cfunc(1), 2) self.assertTypeEqual(cfunc(2), 3)
def test_expr_return(self): expr = Expression('Plus', Integer(3), Expression('Return', Symbol('x'))) args = [CompileArg('System`x', int_type)] cfunc = _compile(expr, args) self.assertTypeEqual(cfunc(1), 1) self.assertTypeEqual(cfunc(4), 4)
def _test_bitwise(self, head, args, result): arg_names = ["x%i" % i for i in range(len(args))] expr = Expression(head, *(Symbol(arg_name) for arg_name in arg_names)) int_args = [ CompileArg("System`" + arg_name, int_type) for arg_name in arg_names ] cfunc = _compile(expr, int_args) self.assertTypeEqual(cfunc(*args), result)
def test_if_eq(self): expr = Expression( "If", Expression("Equal", Symbol("x"), Integer(1)), Integer(2), Integer(3) ) args = [CompileArg("System`x", int_type)] cfunc = _compile(expr, args) self.assertTypeEqual(cfunc(1), 2) self.assertTypeEqual(cfunc(2), 3)
def test_pow_E(self): # E ^ x expr = Expression('Power', Symbol('E'), Symbol('x')) args = [CompileArg('System`x', real_type)] cfunc = _compile(expr, args) for _ in range(1000): x = random.random() self.assertAlmostEqual(cfunc(x), mpmath.exp(x))
def test_pow_E(self): # E ^ x expr = Expression("Power", Symbol("E"), Symbol("x")) args = [CompileArg("System`x", real_type)] cfunc = _compile(expr, args) for _ in range(1000): x = random.random() self.assertAlmostEqual(cfunc(x), mpmath.exp(x))