def processFiles(files): for f in files: print 'Processing file', f with open(f, 'r') as fhandle: equations = fhandle.read().split('\n') print equations for equation in equations: if equation[0] == '#': continue varequ = equation.split('|') if len(varequ) == 2: var, equ = varequ else: var = 'x' equ = varequ[0] var = var.strip() equ = eqparser.parse(equ.strip()) print 'var: %s, equ: %s' % (var, equ) print 'Simplifying with AStar search:' start = time.time() res = eqsimplifier.simplify(equ, var, restart=restart, alpha_val=alpha_val) end = time.time() print 'Simplified in ', end - start, ' seconds' print res
def processFiles(files): for f in files: print "Processing file", f with open(f, "r") as fhandle: equations = fhandle.read().split("\n") print equations for equation in equations: if equation[0] == "#": continue varequ = equation.split("|") if len(varequ) == 2: var, equ = varequ else: var = "x" equ = varequ[0] var = var.strip() equ = eqparser.parse(equ.strip()) print "var: %s, equ: %s" % (var, equ) print "Simplifying with AStar search:" start = time.time() res = eqsimplifier.simplify(equ, var, restart=restart, alpha_val=alpha_val) end = time.time() print "Simplified in ", end - start, " seconds" print res
def reparse(expression): parse_term = math.like_terms(expression) parse_term = parse_term.replace(" ","") #print("in reparse:" + str(parse_term)) p = eqparser.parse(parse_term) print"This is parsed as: " + repr(p) print "In infix form: " + str(p) return p
def reparse(expression): parse_term = math.like_terms(expression) parse_term = parse_term.replace(" ", "") #print("in reparse:" + str(parse_term)) p = eqparser.parse(parse_term) print "This is parsed as: " + repr(p) print "In infix form: " + str(p) return p
def __init__(self, name, equation_string, inputs=None, outputs=None, auto_dep=None, ocaml_to_parse=False): equation_tree = eqparser.parse(equation_string) ParsedComputation.__init__(self, name, "EqProg", equation_tree, equation_string, inputs=inputs, outputs=outputs, auto_dep=auto_dep) self.ocaml_to_parse = ocaml_to_parse
def parse(): try: s = raw_input('eq > ') # use input() on Python 3 except EOFError: print s1 = s.replace(" ", "") s1 = math.like_terms(s1) s1 = s1.replace(" ", "") #print("in parse: " + str(s1)) p = eqparser.parse(s1) print "This is parsed as: " + repr(p) print "In infix form: " + str(p) return p
def parse(): try: s = raw_input('eq > ') # use input() on Python 3 except EOFError: print s1 = s.replace(" ","") s1 = math.like_terms(s1) s1 = s1.replace(" ","") #print("in parse: " + str(s1)) p = eqparser.parse(s1) print "This is parsed as: " + repr(p) print "In infix form: " + str(p) return p
def test_llg(): print "Testing LLG single material" from quantity import Constant, SpaceField, Quantities C1 = Constant("C1", subfields=False, value=Value(-0.125)) C2 = Constant("C2", subfields=False, value=Value(-0.0625)) C3 = Constant("C3", subfields=False, value=Value(0.25)) C4 = Constant("C4", subfields=False, value=Value(0.0)) C5 = Constant("C5", subfields=False, value=Value(0.0)) m = SpaceField("m", [3], subfields=True) dmdt = SpaceField("dmdt", [3], subfields=True) dm_dcurrent = SpaceField("dm_dcurrent", [3], subfields=False) pin = SpaceField("pin", subfields=False) H_total = SpaceField("H_total", [3], subfields=True) quantities = Quantities([C1, C2, C3, C4, C5, m, dmdt, dm_dcurrent, pin, H_total]) eq_rhs = """ %range i:3; (dmdt(i) <- ( C1 * (eps(i,j,k) * m(j) * H_total(k))_(j:3,k:3) + C2 * ( eps(i,j,k) * m(j) * eps(k,p,q) * m(p) * H_total(q))_(j:3,k:3,p:3,q:3) + C3 * (1.0 - (m(j)*m(j))_(j:3)) * m(i) + C4 * ( eps(i,j,k) * m(j) * eps(k,p,q) * m(p) * dm_dcurrent(q))_(j:3,k:3,p:3,q:3) + C5 * (eps(i,j,k) * m(j) * dm_dcurrent(k))_(j:3,k:3))*pin)_(i:3);""" #eq_rhs = """dmdt(0) <- (-m(i))_(i:3);""" eq_ccode = """ if (have_dmdt_a) { dmdt_a(0) = (-0.125*(m_a(1)*H_total_a(2) + -1.0*m_a(2)*H_total_a(1)) + -0.0625*(m_a(1)*m_a(0)*H_total_a(1) + -1.0*m_a(1)*m_a(1)*H_total_a(0) + m_a(2)*m_a(0)*H_total_a(2) + -1.0*m_a(2)*m_a(2)*H_total_a(0)) + 0.25*(1.0 - (m_a(0)*m_a(0) + m_a(1)*m_a(1) + m_a(2)*m_a(2)))*m_a(0))*pin; }; if (have_dmdt_a) { dmdt_a(1) = (-0.125*(-1.0*m_a(0)*H_total_a(2) + m_a(2)*H_total_a(0)) + -0.0625*(-1.0*m_a(0)*m_a(0)*H_total_a(1) + m_a(0)*m_a(1)*H_total_a(0) + m_a(2)*m_a(1)*H_total_a(2) + -1.0*m_a(2)*m_a(2)*H_total_a(1)) + 0.25*(1.0 - (m_a(0)*m_a(0) + m_a(1)*m_a(1) + m_a(2)*m_a(2)))*m_a(1))*pin; }; if (have_dmdt_a) { dmdt_a(2) = (-0.125*(m_a(0)*H_total_a(1) + -1.0*m_a(1)*H_total_a(0)) + -0.0625*(-1.0*m_a(0)*m_a(0)*H_total_a(2) + m_a(0)*m_a(2)*H_total_a(0) + -1.0*m_a(1)*m_a(1)*H_total_a(2) + m_a(1)*m_a(2)*H_total_a(1)) + 0.25*(1.0 - (m_a(0)*m_a(0) + m_a(1)*m_a(1) + m_a(2)*m_a(2)))*m_a(2))*pin; };""" context = EqSimplifyContext(quantities=quantities, material='a') context.expand_indices = True parse_tree = parse(eq_rhs).simplify(context=context) my_result = parse_tree.get_ccode() #.replace("\n", "") assert compare_strings(my_result, eq_ccode), \ ("Simplified of '%s' is '%s', but '%s' is expected." % (eq_rhs, my_result, eq_ccode)) print "passed"
def test_consistency(): print "Testing that parsed tree can be translated to original string" strings = [ "%range j:3, k:3; \nH_t(j) <- H_ext(j) - m*H_exch(j, 1) + m/H_anis(j);", "%range j:3, k:3; \nH_t(j) <- H_ext(j) - (m*H_exch(j, 1) + m/H_anis(j))*x;", "\na <- b + c;", "\na <- 1.0*b*c;", "\na <- 1.0 - 1.0*b*c;", "\na <- -a - 1.0*b*c;", "\na <- -1.23;", "\na <- -1.23 - 1.0*b*c;", "%local m(1); %range a:1, b:2; %range c:3; \na <- b;", "%local m(1, 2), bla(4, 5); %local h(12); %range a:1, b:2; " "%range c:3; \na <- b;" ] for string in strings: backnforth = str(parse(string)) assert backnforth == string, ("Original='%s' but parsed ='%s'." % (string, backnforth)) print "passed"
def test_consistency(): print "Testing that parsed tree can be translated to original string" strings = ["%range j:3, k:3; \nH_t(j) <- H_ext(j) - m*H_exch(j, 1) + m/H_anis(j);", "%range j:3, k:3; \nH_t(j) <- H_ext(j) - (m*H_exch(j, 1) + m/H_anis(j))*x;", "\na <- b + c;", "\na <- 1.0*b*c;", "\na <- 1.0 - 1.0*b*c;", "\na <- -a - 1.0*b*c;", "\na <- -1.23;", "\na <- -1.23 - 1.0*b*c;", "%local m(1); %range a:1, b:2; %range c:3; \na <- b;", "%local m(1, 2), bla(4, 5); %local h(12); %range a:1, b:2; " "%range c:3; \na <- b;"] for string in strings: backnforth = str(parse(string)) assert backnforth == string, ("Original='%s' but parsed ='%s'." % (string, backnforth)) print "passed"
def interactive(): while 1: try: s = raw_input('Eq >. ') x = raw_input('Var >. ') except EOFError: print break p = eqparser.parse(s) print 'This is parsed at: ' + repr(p) print 'This is parsed at: ' + str(p) print 'Variable to evaluate: ' + x print 'Simplifying with AStar search:' start = time.time() res = eqsimplifier.simplify(p, x, restart=restart, alpha_val=alpha_val) end = time.time() print 'Simplified in ', end - start, ' seconds' print res
def interactive(): while 1: try: s = raw_input("Eq >. ") x = raw_input("Var >. ") except EOFError: print break p = eqparser.parse(s) print "This is parsed at: " + repr(p) print "This is parsed at: " + str(p) print "Variable to evaluate: " + x print "Simplifying with AStar search:" start = time.time() res = eqsimplifier.simplify(p, x, restart=restart, alpha_val=alpha_val) end = time.time() print "Simplified in ", end - start, " seconds" print res
def test_simplify_quantities(): print "Testing simplification of quantities" from quantity import Constant, SpaceField, Quantities zero = Constant("zero", subfields=False, value=Value(0.0)) gamma = Constant("gamma", subfields=False, value=Value(1.23)) m = SpaceField("m", [3], subfields=True) H_ext = SpaceField("H_ext", [3]) context = \ EqSimplifyContext(quantities=Quantities([gamma, m, H_ext, zero])) strings = [("m(0) <- -zero*(m(1)*H_ext(2) - m(2)*H_ext(1));", "m(0) <- 0.0;")] for string, result in strings: parse_tree = parse(string).simplify(context=context) my_result = str(parse_tree).replace("\n", "") assert my_result == result, ("Simplified of '%s' is '%s', but '%s' " "is expected." % (string, my_result, result)) print "passed"
def test_multimaterial(): print "Testing multimaterial simplification" from quantity import Constant, SpaceField, Quantities C1 = Constant("C1", subfields=False, value=Value(-0.17688)) C2 = Constant("C2", subfields=True, value=Value(-0.08844)) m = SpaceField("m", [3], subfields=True) dmdt = SpaceField("dmdt", [3], subfields=True) quantities = Quantities([C1, C2, m, dmdt]) strings = [("%range i:3; m <- 0;", "%range i:3; m_Py <- 0.0; m_Co <- 0.0;"),] context = EqSimplifyContext(quantities=quantities, material=['Py', 'Co']) for string, result in strings: my_result = \ str(parse(string).simplify(context=context)).replace("\n", "") assert compare_strings(my_result, result), \ ("Simplified of '%s' is '%s', but '%s' is expected." % (string, my_result, result)) print "passed"
def test_llg_multimaterial(): print "Testing LLG multi-material" from quantity import Constant, SpaceField, Quantities C1 = Constant("C1", subfields=False, value=Value(-0.125)) C2 = Constant("C2", subfields=False, value=Value(-0.0625)) C3 = Constant("C3", subfields=False, value=Value(0.25)) C4 = Constant("C4", subfields=False, value=Value(0.0)) C5 = Constant("C5", subfields=False, value=Value(0.0)) m = SpaceField("m", [3], subfields=True) dmdt = SpaceField("dmdt", [3], subfields=True) dm_dcurrent = SpaceField("dm_dcurrent", [3], subfields=False) pin = SpaceField("pin", subfields=False) H_total = SpaceField("H_total", [3], subfields=True) quantities = Quantities( [C1, C2, C3, C4, C5, m, dmdt, dm_dcurrent, pin, H_total]) eq_rhs = """%range i:3, j:3, k:3, p:3, q:3; dmdt(i) <- C1 * eps(i,j,k) * m(j) * H_total(k) * pin + C2 * eps(i,j,k) * m(j) * eps(k,p,q) * m(p) * H_total(q) * pin + C3 * (1.0 - m(j)*m(j)) * m(i) * pin + C4 * eps(i,j,k) * m(j) * eps(k,p,q) * m(p) * dm_dcurrent(q) * pin + C5 * eps(i,j,k) * m(j) * dm_dcurrent(k) * pin;""" result = condensed_string("""%range i:3, j:3, k:3, p:3, q:3; dmdt_Py(i) <- -0.125*eps(i,j,k)*m_Py(j)*H_total_Py(k)*pin + -0.0625*eps(i,j,k)*m_Py(j)*eps(k,p,q)*m_Py(p)*H_total_Py(q)*pin + 0.25*(1.0 - m_Py(j)*m_Py(j))*m_Py(i)*pin; dmdt_Co(i) <- -0.125*eps(i,j,k)*m_Co(j)*H_total_Co(k)*pin + -0.0625*eps(i,j,k)*m_Co(j)*eps(k,p,q)*m_Co(p)*H_total_Co(q)*pin + 0.25*(1.0 - m_Co(j)*m_Co(j))*m_Co(i)*pin;""") context = EqSimplifyContext(quantities=quantities, material=['Py', 'Co']) parse_tree = parse(eq_rhs).simplify(context=context) my_result = condensed_string(str(parse_tree)) assert compare_strings(my_result, result), \ ("Simplified of '%s' is '%s', but '%s' is expected." % (eq_rhs, my_result, result)) print "passed"
def test_llg_multimaterial(): print "Testing LLG multi-material" from quantity import Constant, SpaceField, Quantities C1 = Constant("C1", subfields=False, value=Value(-0.125)) C2 = Constant("C2", subfields=False, value=Value(-0.0625)) C3 = Constant("C3", subfields=False, value=Value(0.25)) C4 = Constant("C4", subfields=False, value=Value(0.0)) C5 = Constant("C5", subfields=False, value=Value(0.0)) m = SpaceField("m", [3], subfields=True) dmdt = SpaceField("dmdt", [3], subfields=True) dm_dcurrent = SpaceField("dm_dcurrent", [3], subfields=False) pin = SpaceField("pin", subfields=False) H_total = SpaceField("H_total", [3], subfields=True) quantities = Quantities([C1, C2, C3, C4, C5, m, dmdt, dm_dcurrent, pin, H_total]) eq_rhs = """%range i:3, j:3, k:3, p:3, q:3; dmdt(i) <- C1 * eps(i,j,k) * m(j) * H_total(k) * pin + C2 * eps(i,j,k) * m(j) * eps(k,p,q) * m(p) * H_total(q) * pin + C3 * (1.0 - m(j)*m(j)) * m(i) * pin + C4 * eps(i,j,k) * m(j) * eps(k,p,q) * m(p) * dm_dcurrent(q) * pin + C5 * eps(i,j,k) * m(j) * dm_dcurrent(k) * pin;""" result = condensed_string("""%range i:3, j:3, k:3, p:3, q:3; dmdt_Py(i) <- -0.125*eps(i,j,k)*m_Py(j)*H_total_Py(k)*pin + -0.0625*eps(i,j,k)*m_Py(j)*eps(k,p,q)*m_Py(p)*H_total_Py(q)*pin + 0.25*(1.0 - m_Py(j)*m_Py(j))*m_Py(i)*pin; dmdt_Co(i) <- -0.125*eps(i,j,k)*m_Co(j)*H_total_Co(k)*pin + -0.0625*eps(i,j,k)*m_Co(j)*eps(k,p,q)*m_Co(p)*H_total_Co(q)*pin + 0.25*(1.0 - m_Co(j)*m_Co(j))*m_Co(i)*pin;""") context = EqSimplifyContext(quantities=quantities, material=['Py', 'Co']) parse_tree = parse(eq_rhs).simplify(context=context) my_result = condensed_string(str(parse_tree)) assert compare_strings(my_result, result), \ ("Simplified of '%s' is '%s', but '%s' is expected." % (eq_rhs, my_result, result)) print "passed"
def test_simplify(): print "Testing simplification" strings = [("a <- 0;", "a <- 0.0;"), ("abc <- def;", "abc <- def;"), ("a <- 0*(b + c);", "a <- 0.0;"), ("a <- 1*(b + c);", "a <- (b + c);"), ("a <- 2*(b + c);", "a <- 2.0*(b + c);"), ("a <- 2*(b + c)*2.5;", "a <- 5.0*(b + c);"), ("a <- 2*(b + c)*2.5/5.0;", "a <- (b + c);"), ("a <- 1*(1*b + c*1)*1/1;", "a <- (b + c);"), ("a <- 1*(1*b + c*1)*0/1;", "a <- 0.0;"), ("a <- (2);", "a <- 2.0;"), ("a <- 1*(0.5 + b - (5 - 4)/2);", "a <- b;"), ("a <- -1*-1;", "a <- 1.0;"), ("a <- --1;", "a <- 1.0;"), ("a <- +b/(2*4);", "a <- 0.125*b;"), ("a <- -b/(2*4);", "a <- -0.125*b;")] # need to fix this for string, result in strings: my_result = str(parse(string).simplify()).replace("\n", "") assert my_result == result, ("Simplified of '%s' is '%s', but '%s' " "is expected." % (string, my_result, result)) print "passed"
def test_multimaterial(): print "Testing multimaterial simplification" from quantity import Constant, SpaceField, Quantities C1 = Constant("C1", subfields=False, value=Value(-0.17688)) C2 = Constant("C2", subfields=True, value=Value(-0.08844)) m = SpaceField("m", [3], subfields=True) dmdt = SpaceField("dmdt", [3], subfields=True) quantities = Quantities([C1, C2, m, dmdt]) strings = [ ("%range i:3; m <- 0;", "%range i:3; m_Py <- 0.0; m_Co <- 0.0;"), ] context = EqSimplifyContext(quantities=quantities, material=['Py', 'Co']) for string, result in strings: my_result = \ str(parse(string).simplify(context=context)).replace("\n", "") assert compare_strings(my_result, result), \ ("Simplified of '%s' is '%s', but '%s' is expected." % (string, my_result, result)) print "passed"
def goal_test(self, state): if(state.type == 'EQUALS' and state.children[0].leaf == self.goal): return True else: return False def path_cost(self, c, state1, action, state2): return c + 1 def value(self, state): return 0 while 1: try: s = raw_input('eq > ') # use input() on Python 3 var = raw_input('var > ') except EOFError: print break p = eqparser.parse(s) problem =Problem(p,var) result = search.astar_search(problem, lambda n: HFunction(n.state, var, -1) ).state s = segment(result) print s #print "In infix form: " + str(p)
def __init__(self, keywords, pattern, modifier): self.keywords = keywords self.pattern = eqparser.parse(pattern) self.modifier = eqparser.parse(modifier) print 'pattern:', self.pattern, ' -> ', self.modifier
import eqparser from eqparser import Node as eqNode import operator import search from helperfns import isX from helperfns import isSimplifiedSubTree ops = { "+": operator.add, "-": operator.sub, "*": operator.mul, "/":operator.div, "^":operator.pow, "=":operator.eq } specials = ["sin", "cos", "tan", "log", "ln", "sqrt"] arithtypes = ["INT", "FLOAT"] counter = 0 s=raw_input("eq>") r=raw_input("eq2>") p = eqparser.parse(s) p2=eqparser.parse(r) q = p.children[1] currentstate = p # Function to do arithmetic operations for more than one levels def solveThis(x): c1 = x.children[0] c2 = x.children[1] if c1.children != []: c1 = solveThis(c1) if c1.children[0].leaf in specials or c1.children[0].type in ["VARIABLENAME", "SYMBOL"]: return c1.children[0] if c1.children[1].leaf in specials or c1.children[1].type in ["VARIABLENAME", "SYMBOL"]: return c1.children[1]
newDiff1.children.append(treeCopy) newDiff1.children.append(treeCopy) newNode1 = eqparser.Node("BINARYOP",[],'*') root.children[0].leaf = 'sin' newSub1 = eqparser.Node("BINARYOP",[],'-') newSub2 = eqparser.Node("INT",[],0) newSub1.children.append(newSub2) newSub1.children.append(root.children[0]) newNode1.children.append(newSub1) newNode1.children.append(newDiff1) return newNode1 return 0 if __name__ == "__main__": while 1: try: s = "diff(x*cos(x),x) = 0" # use input() on Python 3 except EOFError: print break root = eqparser.parse(s) newRoot = diff(root.children[0]) print "Old Tree = " + repr(root) print str(root) print "New Tree = " + repr(newRoot) print str(newRoot) break
return self.is_var_in(Node.children) elif not Node.children or len(Node.children)==0: return False else: if len(Node.children)==1: return self.is_var_in(Node.children) else: return self.is_var_in(Node.children[0]) or self.is_var_in(Node.children[1]) def outPut(self): if self.error == 0: print "x = undefined" return print "The final result is:"+self.calculate(self.result) while 1: try: s = raw_input('eq > ') # use input() on Python 3 var = raw_input('var > ') except EOFError: print break p = eqparser.parse(s) print "This is parsed at: " + repr(p) x = equaSimplifier(p,var) #print "This is parsed at: " + repr(p) #print "In infix form: " + str(p)