absorb coefficients into sympy expr

This commit is contained in:
Marcell Vazquez-Chanlatte 2016-07-08 20:59:56 -07:00
parent fc6be7b80b
commit 619c2c6524
2 changed files with 5 additions and 18 deletions

15
stl.py
View file

@ -17,12 +17,9 @@ t_sym = Symbol('t', positive=True)
class LinEq(namedtuple("LinEquality", ["terms", "op", "const"])): class LinEq(namedtuple("LinEquality", ["terms", "op", "const"])):
def __repr__(self): def __repr__(self):
n = len(self.terms) rep = "{lhs} {op} {c}"
rep = "{}" lhs = sum(t.id for t in self.terms)
if n > 1: return rep.format(lhs=lhs, op=self.op, c=self.const)
rep += " + {}"*(n - 1)
rep += " {op} {c}"
return rep.format(*self.terms, op=self.op, c=self.const)
def children(self): def children(self):
return [] return []
@ -34,12 +31,6 @@ class Var(namedtuple("Var", ["kind", "id", "time"])):
return "{i}{t}".format(k=self.kind.name, i=self.id, t=time_str) return "{i}{t}".format(k=self.kind.name, i=self.id, t=time_str)
class Term(namedtuple("Term", ["coeff", "var"])):
def __repr__(self):
coeff = str(self.coeff) + "*" if self.coeff != 1 else ""
return "{c}{v}".format(c=coeff, v=self.var)
class Interval(namedtuple('I', ['lower', 'upper'])): class Interval(namedtuple('I', ['lower', 'upper'])):
def __repr__(self): def __repr__(self):
return "[{},{}]".format(self.lower, self.upper) return "[{},{}]".format(self.lower, self.upper)

View file

@ -2,16 +2,12 @@
# TODO: consider using sympy to interpret stuff # TODO: consider using sympy to interpret stuff
# TODO: break out into seperate library # TODO: break out into seperate library
# TODO: allow matrix to be symbolically parsed in STL_GRAMMAR
# TODO: allow multiplication to be distributive # TODO: allow multiplication to be distributive
# TODO: support reference specific time points # TODO: support reference specific time points
# TODO: add Implies and Iff syntactic sugar # TODO: add Implies and Iff syntactic sugar
# TODO: add support for parsing Until # TODO: add support for parsing Until
# TODO: support variables on both sides of ineq # TODO: support variables on both sides of ineq
# TODO: Allow -x = -1*x # TODO: Allow -x = -1*x
# TODO: change way of parsing dt
# - Allow inside of time index
# - Allow dt*x rather than dt*1*x
from functools import partialmethod from functools import partialmethod
from collections import namedtuple from collections import namedtuple
@ -137,7 +133,7 @@ class STLVisitor(NodeVisitor):
def visit_term(self, _, children): def visit_term(self, _, children):
coeffs, var = children coeffs, var = children
c = coeffs[0] if coeffs else 1 c = coeffs[0] if coeffs else 1
return stl.Term(c, var) return lens(var).id*c
def visit_coeff(self, _, children): def visit_coeff(self, _, children):
dt, coeff, *_ = children dt, coeff, *_ = children
@ -147,7 +143,7 @@ class STLVisitor(NodeVisitor):
def visit_terms(self, _, children): def visit_terms(self, _, children):
if isinstance(children[0], list): if isinstance(children[0], list):
term, _1, sgn ,_2, terms = children[0] term, _1, sgn ,_2, terms = children[0]
terms = lens(terms)[0].coeff * sgn terms = lens(terms)[0].id * sgn
return [term] + terms return [term] + terms
else: else:
return children return children