def visit_LiteralEvaluation(self, stmt: 'LiteralEvaluation', environment=None, usub=False) -> PyTexpr1: if usub: cst = PyMPQScalarCoeff(-float(stmt.literal.val)) else: cst = PyMPQScalarCoeff(float(stmt.literal.val)) return PyTexpr1.cst(environment, cst)
def relu(self, stmt: PyVar, active: bool = False, inactive: bool = False) -> 'BoxState': self.flag = None if active: # we only do the active case (h >= 0) expr1 = PyTexpr1.var(self.environment, stmt) cond = PyTcons1.make(expr1, ConsTyp.AP_CONS_SUPEQ) abstract1 = self.domain(self.state.manager, self.environment, array=PyTcons1Array([cond])) self.state = self.state.meet(abstract1) self.flag = 1 return self if inactive: # we only do the inactive case (h < 0) expr1 = PyTexpr1.var(self.environment, stmt) neg = PyTexpr1.unop(TexprOp.AP_TEXPR_NEG, expr1, rtype, rdir) cond = PyTcons1.make(neg, ConsTyp.AP_CONS_SUP) abstract1 = self.domain(self.state.manager, self.environment, array=PyTcons1Array([cond])) zero = PyTexpr1.cst(self.environment, PyMPQScalarCoeff(0.0)) self.state = self.state.meet(abstract1).assign(stmt, zero) self.flag = -1 return self # we do both cases _active, _inactive = deepcopy(self.state), deepcopy(self.state) expr1 = PyTexpr1.var(self.environment, stmt) cond1 = PyTcons1.make(expr1, ConsTyp.AP_CONS_SUPEQ) abstract1 = self.domain(self.state.manager, self.environment, array=PyTcons1Array([cond1])) _active = _active.meet(abstract1) expr2 = PyTexpr1.var(self.environment, stmt) neg = PyTexpr1.unop(TexprOp.AP_TEXPR_NEG, expr2, rtype, rdir) cond2 = PyTcons1.make(neg, ConsTyp.AP_CONS_SUP) abstract2 = self.domain(self.state.manager, self.environment, array=PyTcons1Array([cond2])) zero = PyTexpr1.cst(self.environment, PyMPQScalarCoeff(0.0)) _inactive = _inactive.meet(abstract2).assign(stmt, zero) if _active.is_bottom(): self.flag = -1 elif _inactive.is_bottom(): self.flag = 1 join = _active.join(_inactive) self.state = join return self
def dict_to_texpr(todict, env): texpr = PyTexpr1.cst(env, PyMPQScalarCoeff(PyMPQScalar(todict['_']))) for var, val in reversed(list(todict.items())): if var != '_': coeff = PyTexpr1.cst(env, PyMPQScalarCoeff(PyMPQScalar(val))) dim = PyTexpr1.var(env, PyVar(var)) term = PyTexpr1.binop(TexprOp.AP_TEXPR_MUL, coeff, dim, TexprRtype.AP_RTYPE_REAL, TexprRdir.AP_RDIR_RND) texpr = PyTexpr1.binop(TexprOp.AP_TEXPR_ADD, term, texpr, TexprRtype.AP_RTYPE_REAL, TexprRdir.AP_RDIR_RND) return texpr
def test_substitute(self): e = PyEnvironment([PyVar('x0'), PyVar('y')], [PyVar('z')]) x0 = PyLinexpr1(e) x0.set_coeff(PyVar('x0'), PyMPQScalarCoeff(1)) x0.set_cst(PyMPQScalarCoeff(3)) t0 = PyTexpr1(x0) c0 = PyTcons1.make(t0, ConsTyp.AP_CONS_SUPEQ) self.assertEqual(str(c0), '3 + 1 · x0 >= 0') x1 = PyLinexpr1(e) x1.set_coeff(PyVar('x0'), PyMPQScalarCoeff(1)) x1.set_cst(PyMPQScalarCoeff(-1)) t1 = PyTexpr1(x1) c1 = PyTcons1.make(t1, ConsTyp.AP_CONS_SUPEQ) self.assertEqual(str(c1), '-1 + 1 · x0 >= 0') self.assertEqual(str(c0.substitute(PyVar('x0'), t1)), '3 + 1 · (-1 + 1 · x0) >= 0')
def get_cst(self): cst = self.lincons1.lincons0.linexpr0.contents.cst if cst.discr == CoeffDiscr.AP_COEFF_INTERVAL: if cst.val.interval.contents.inf.contents.discr == ScalarDiscr.AP_SCALAR_MPQ: result = PyMPQIntervalCoeff() elif cst.val.interval.contents.inf.contents.discr == ScalarDiscr.AP_SCALAR_MPFR: result = PyMPFRIntervalCoeff(0, 0) else: # cst.val.interval.contents.inf.contents.discr == ScalarDiscr.AP_SCALAR_DOUBLE result = PyDoubleIntervalCoeff() else: # CoeffDiscr.AP_COEFF_SCALAR if cst.val.scalar.contents.discr == ScalarDiscr.AP_SCALAR_MPQ: result = PyMPQScalarCoeff() elif cst.val.scalar.contents.discr == ScalarDiscr.AP_SCALAR_MPFR: result = PyMPFRScalarCoeff(0) else: # cst.val.scalar.contents.discr == ScalarDiscr.AP_SCALAR_DOUBLE result = PyDoubleScalarCoeff() libapron.ap_coeff_set(result.coeff, self.lincons1.lincons0.linexpr0.contents.cst) return result
def get_coeff(self, var: PyVar): coeff = libapron.ap_linexpr1_coeffref(self, var._as_parameter_) if coeff.contents.discr == CoeffDiscr.AP_COEFF_INTERVAL: discr = coeff.contents.val.interval.contents.inf.contents.discr if discr == ScalarDiscr.AP_SCALAR_MPQ: result = PyMPQIntervalCoeff() elif discr == ScalarDiscr.AP_SCALAR_MPFR: result = PyMPFRIntervalCoeff(0, 0) else: # discr == ScalarDiscr.AP_SCALAR_DOUBLE result = PyDoubleIntervalCoeff() else: # CoeffDiscr.AP_COEFF_SCALAR if coeff.contents.val.scalar.contents.discr == ScalarDiscr.AP_SCALAR_MPQ: result = PyMPQScalarCoeff() elif coeff.contents.val.scalar.contents.discr == ScalarDiscr.AP_SCALAR_MPFR: result = PyMPFRScalarCoeff(0) else: # coeff.contents.val.scalar.contents.discr == ScalarDiscr.AP_SCALAR_DOUBLE result = PyDoubleScalarCoeff() libapron.ap_linexpr1_get_coeff(result, self, var._as_parameter_) return result
def get_cst(self): cst = deepcopy(self.linexpr1.linexpr0.contents.cst) if cst.discr == CoeffDiscr.AP_COEFF_INTERVAL: if cst.val.interval.contents.inf.contents.discr == ScalarDiscr.AP_SCALAR_MPQ: result = PyMPQIntervalCoeff(cst) elif cst.val.interval.contents.inf.contents.discr == ScalarDiscr.AP_SCALAR_MPFR: result = PyMPFRIntervalCoeff(cst) else: assert cst.val.interval.contents.inf.contents.discr == ScalarDiscr.AP_SCALAR_DOUBLE result = PyDoubleIntervalCoeff(cst) else: assert cst.discr == CoeffDiscr.AP_COEFF_SCALAR if cst.val.scalar.contents.discr == ScalarDiscr.AP_SCALAR_MPQ: result = PyMPQScalarCoeff(cst) elif cst.val.scalar.contents.discr == ScalarDiscr.AP_SCALAR_MPFR: result = PyMPFRScalarCoeff(cst) else: assert cst.val.scalar.contents.discr == ScalarDiscr.AP_SCALAR_DOUBLE result = PyDoubleScalarCoeff(cst) return result
def test_substitute(self): e = PyEnvironment([PyVar('x0'), PyVar('y')], [PyVar('z')]) x0 = PyLinexpr1(e) x0.set_coeff(PyVar('x0'), PyMPQScalarCoeff(1)) x0.set_cst(PyMPQScalarCoeff(3)) t0 = PyTexpr1(x0) self.assertEqual(str(t0), '3 + 1 · x0') x1 = PyLinexpr1(e) x1.set_coeff(PyVar('x0'), PyMPQScalarCoeff(1)) x1.set_cst(PyMPQScalarCoeff(-1)) t1 = PyTexpr1(x1) self.assertEqual(str(t1), '-1 + 1 · x0') self.assertEqual(str(t0.substitute(PyVar('x0'), t1)), '3 + 1 · (-1 + 1 · x0)') x2 = PyLinexpr1(e) x2.set_coeff(PyVar('x0'), PyMPQScalarCoeff(1)) x2.set_cst(PyMPQScalarCoeff(2)) t2 = PyTexpr1(x2) self.assertEqual(str(t2), '2 + 1 · x0')
def visit_Literal(self, expr: 'Literal', environment=None, usub=False) -> PyTexpr1: if usub: cst = PyMPQScalarCoeff(-float(expr.val)) else: cst = PyMPQScalarCoeff(float(expr.val)) return PyTexpr1.cst(environment, cst)
def test_neg(self): self.assertEqual(-PyMPQScalarCoeff(-1, 2), PyMPQScalarCoeff(1, 2)) self.assertEqual(-PyMPQScalarCoeff(0), PyMPQScalarCoeff(0)) self.assertEqual(-PyMPQScalarCoeff(1, 2), PyMPQScalarCoeff(-1, 2))
def test_cmp(self): self.assertTrue(PyMPQScalarCoeff(-1, 2) < PyMPQScalarCoeff()) self.assertFalse(PyMPQScalarCoeff() < PyMPQScalarCoeff(-1, 2)) self.assertTrue(PyMPQScalarCoeff() == PyMPQScalarCoeff(0)) self.assertFalse(PyMPQScalarCoeff() == PyMPQScalarCoeff(-1, 2)) self.assertTrue(PyMPQScalarCoeff() > PyMPQScalarCoeff(-1, 2)) self.assertFalse(PyMPQScalarCoeff(-1, 2) > PyMPQScalarCoeff())
def test_deepcopy(self): c0 = PyMPQScalarCoeff() c1 = deepcopy(c0) c2 = c0 self.assertNotEqual(id(c0), id(c1)) self.assertEqual(id(c0), id(c2))
def test_init(self): self.assertEqual(str(PyMPQScalarCoeff()), '0')