Esempio n. 1
0
 def test_functions_with_float(self):
     v_x = 1.0
     v_y = 1.0
     self.assertEqual(abs(v_x), math.fabs(v_x))
     self.assertEqual(exp(v_x), math.exp(v_x))
     self.assertEqual(sqrt(v_x), math.sqrt(v_x))
     self.assertEqual(pow(v_x, v_y), v_x**v_y)
     self.assertEqual(sin(v_x), math.sin(v_x))
     self.assertEqual(cos(v_x), math.cos(v_x))
     self.assertEqual(tan(v_x), math.tan(v_x))
     self.assertEqual(asin(v_x), math.asin(v_x))
     self.assertEqual(acos(v_x), math.acos(v_x))
     self.assertEqual(atan(v_x), math.atan(v_x))
     self.assertEqual(atan2(v_x, v_y), math.atan2(v_x, v_y))
     self.assertEqual(sinh(v_x), math.sinh(v_x))
     self.assertEqual(cosh(v_x), math.cosh(v_x))
     self.assertEqual(tanh(v_x), math.tanh(v_x))
     self.assertEqual(min(v_x, v_y), min(v_x, v_y))
     self.assertEqual(max(v_x, v_y), max(v_x, v_y))
     self.assertEqual(
         if_then_else(Expression(v_x) > Expression(v_y), v_x, v_y),
         v_x if v_x > v_y else v_y)
Esempio n. 2
0
 def test_functions_with_expression(self):
     self.assertEqual(str(abs(e_x)), "abs(x)")
     self.assertEqual(str(exp(e_x)), "exp(x)")
     self.assertEqual(str(sqrt(e_x)), "sqrt(x)")
     self.assertEqual(str(pow(e_x, e_y)), "pow(x, y)")
     self.assertEqual(str(sin(e_x)), "sin(x)")
     self.assertEqual(str(cos(e_x)), "cos(x)")
     self.assertEqual(str(tan(e_x)), "tan(x)")
     self.assertEqual(str(asin(e_x)), "asin(x)")
     self.assertEqual(str(acos(e_x)), "acos(x)")
     self.assertEqual(str(atan(e_x)), "atan(x)")
     self.assertEqual(str(atan2(e_x, e_y)), "atan2(x, y)")
     self.assertEqual(str(sinh(e_x)), "sinh(x)")
     self.assertEqual(str(cosh(e_x)), "cosh(x)")
     self.assertEqual(str(tanh(e_x)), "tanh(x)")
     self.assertEqual(str(min(e_x, e_y)), "min(x, y)")
     self.assertEqual(str(max(e_x, e_y)), "max(x, y)")
     self.assertEqual(str(if_then_else(e_x > e_y, e_x, e_y)),
                      "(if (x > y) then x else y)")
Esempio n. 3
0
 def test_functions_with_variable(self):
     self.assertEqual(str(abs(x)), "abs(x)")
     self.assertEqual(str(exp(x)), "exp(x)")
     self.assertEqual(str(sqrt(x)), "sqrt(x)")
     self.assertEqual(str(pow(x, y)), "pow(x, y)")
     self.assertEqual(str(sin(x)), "sin(x)")
     self.assertEqual(str(cos(x)), "cos(x)")
     self.assertEqual(str(tan(x)), "tan(x)")
     self.assertEqual(str(asin(x)), "asin(x)")
     self.assertEqual(str(acos(x)), "acos(x)")
     self.assertEqual(str(atan(x)), "atan(x)")
     self.assertEqual(str(atan2(x, y)), "atan2(x, y)")
     self.assertEqual(str(sinh(x)), "sinh(x)")
     self.assertEqual(str(cosh(x)), "cosh(x)")
     self.assertEqual(str(tanh(x)), "tanh(x)")
     self.assertEqual(str(min(x, y)), "min(x, y)")
     self.assertEqual(str(max(x, y)), "max(x, y)")
     self.assertEqual(str(if_then_else(x > y, x, y)),
                      "(if (x > y) then x else y)")
Esempio n. 4
0
def _sympy_converter(var_map, exp, target, expand_pow=False):
    rv = None
    assert isinstance(exp, sp.Expr) and target is not None

    if isinstance(exp, sp.Symbol):
        rv = var_map.get(exp.name, None)
    elif isinstance(exp, sp.Number):
        try:
            rv = RealVal(exp) if isinstance(target, Z3Verifier) else sp.RealNumber(exp)
        except:  # Z3 parser error
            rep = sp.Float(exp, len(str(exp)))
            rv = RealVal(rep)
    elif isinstance(exp, sp.Add):
        # Add(exp_0, ...)
        rv = _sympy_converter(var_map, exp.args[0], target, expand_pow=expand_pow)  # eval this expression
        for e in exp.args[1:]:  # add it to all other remaining expressions
            rv += _sympy_converter(var_map, e, target, expand_pow=expand_pow)
    elif isinstance(exp, sp.Mul):
        rv = _sympy_converter(var_map, exp.args[0], target, expand_pow=expand_pow)
        for e in exp.args[1:]:
            rv *= _sympy_converter(var_map, e, target, expand_pow=expand_pow)
    elif isinstance(exp, sp.Pow):
        x = _sympy_converter(var_map, exp.args[0], target, expand_pow=expand_pow)
        e = _sympy_converter(var_map, exp.args[1], target, expand_pow=expand_pow)
        if expand_pow:
            try:
                i = float(e.sexpr())
                assert i.is_integer()
                i = int(i) - 1
                rv = x
                for _ in range(i):
                    rv *= x
            except:  # fallback
                rv = _sympy_converter(var_map, exp, target, expand_pow=False)
        else:
            rv = x ** e
    elif isinstance(exp, sp.Max):
        x = _sympy_converter(var_map, exp.args[1], target, expand_pow=expand_pow)
        zero = exp.args[0]
        if target == Z3Verifier:
            rv = z3.If(x >= 0.0, x, 0.0)
        else:
            rv = dr.max(x, 0.0)
    elif isinstance(exp, sp.Heaviside):
        x = _sympy_converter(var_map, exp.args[0], target, expand_pow=False)
        if target == Z3Verifier:
            rv = z3.If(x > 0.0, 1.0, 0.0)
        else:
            rv = dr.if_then_else(x>0.0, 1.0, 0.0)
    elif isinstance(exp, sp.Function):
        # check various activation types ONLY FOR DREAL
        if isinstance(exp, sp.tanh):
            rv = dr.tanh(_sympy_converter(var_map, exp.args[0], target, expand_pow=expand_pow))
        elif isinstance(exp, sp.sin):
            rv = dr.sin(_sympy_converter(var_map, exp.args[0], target, expand_pow=expand_pow))
        elif isinstance(exp, sp.cos):
            rv = dr.cos(_sympy_converter(var_map, exp.args[0], target, expand_pow=expand_pow))
        elif isinstance(exp, sp.exp):
            rv = dr.exp(_sympy_converter(var_map, exp.args[0], target, expand_pow=expand_pow))
    else:
        ValueError('Term ' + str(exp) + ' not recognised')

    assert rv is not None
    return rv