Ejemplo n.º 1
0
 def on_gleichung_changed(self,target,param):
     try:
         lhs = formula.Parser(self.lhs_Entry.get_text()).parseAst()
         
     except:
         self.lhs_Entry.get_style_context().add_class("red")
         lhs = self.gleichungslöser.lhs
     else:
         try:
             self.lhs_Entry.get_style_context().remove_class("red")
             #self.rhs_Entry.get_style_context().remove_class("red")
         except:
             pass
     try:
         rhs = formula.Parser(self.rhs_Entry.get_text()).parseAst()
         
     except:
         self.rhs_Entry.get_style_context().add_class("red")
         rhs = self.gleichungslöser.rhs
     else:
         try:
             #self.lhs_Entry.get_style_context().remove_class("red")
             self.rhs_Entry.get_style_context().remove_class("red")
         except:
             pass
         
     self.gleichungslöser = solver.AstSolver(lhs, rhs)
     self.fillTree()
     self.grid.remove(self.varsgrid)
     self.varsgrid = Gtk.Grid()
     vvars = self.gleichungslöser.evaluate()
     for (i,(var,value)) in enumerate(vvars.items()):
         self.varsgrid.attach(Gtk.Label(label="%s=%s"%(var,value)),1,i+1,1,1)
     self.grid.attach(self.varsgrid,1,3,3,1)
     self.show_all()
Ejemplo n.º 2
0
    def testSearchPath(self):
        p = formula.Parser("3*y+4*x")
        node = p.parseAst()

        path = node.searchPath(ast.AstVariable("x"))
        print("path=%s" % _readastpath(path))

        p = formula.Parser("x*y+4*x")
        node = p.parseAst()

        path = node.searchPath(ast.AstVariable("x"))
        print("path=%s" % _readastpath(path))
Ejemplo n.º 3
0
    def simplify_clicked(self, event):
        parser = formula.Parser(self.formula_entry.get_text())
        try:
            node = parser.parseAst()
        except:
            return

        node = node.simplify()
        self.formula_entry.set_text(str(node))
        self._show(node)
Ejemplo n.º 4
0
 def __init__(self,g):
     self.gleichung = g
     filename = os.path.dirname(__file__)
     filename = os.path.join(filename,"gleichungslöser_history.txt")
     fp = open(filename,"a")
     fp.write(self.gleichung+"\n")
     fp.close()
     l = [formula.Parser(i).parseAst() for i in g.split("=")]
     self.lhs = l[0]
     self.rhs = l[-1]
Ejemplo n.º 5
0
 def _test_formula(self, res, f, variables={}):
     o = formula.Parser(f)
     i = o.evaluate(variables)
     print("%s = %g" % (f, i))
     self.assertEqual(res, i)
     node = o.parseAst()
     print("%s = %s" % (f, node))
     print(repr(node))
     try:
         self.nodes += [node]
     except:
         self.nodes = [node]
     x = node.evaluate(variables)
     self.assertEqual(res, x)
Ejemplo n.º 6
0
    def testLinCombOne(self):
        p = formula.Parser("x+5+x")

        node = p.parseAst()
        path = node.searchPath(ast.AstVariable("x"))

        print("path=%s" % _readastpath(path))
        ret, rpath, other = node.reducePath(path, AstConstant(0.0))

        print("res = %s,%s,%s" % (ret, rpath, other))
        self.assertEqual([], rpath)
        self.assertEqual("(0.0-5.0)/(1.0+1.0)", str(other))

        other = other.simplify()
        self.assertEqual("-2.5", str(other))
Ejemplo n.º 7
0
    def __init__(self, state):
        self.f = state["f"]
        self.xmin = state["xmin"]
        self.xmax = state["xmax"]
        self.ymin = state["ymin"]
        self.ymax = state["ymax"]
        self.n = state["n"]
        self.n1 = self.n - 1

        self.ast = formula.Parser(self.f).parseAst()
        self.vvars = list(self.ast.findVars({}))
        log.debug("vvars=%s" % self.vvars)
        assert len(self.vvars) == 2
        self._i = 0
        self._j = 0
        self._x = self.xmin
Ejemplo n.º 8
0
 def var_changed(self, target, param, node):
     fopa = formula.Parser(target.get_text())
     try:
         i = fopa.evaluate()
     except:
         pass
     else:
         self.vars[self.entries_to_var[target]] = i
     if list(self.vars.values()).count(None) == 0:
         f = node.evaluate(self.vars)
         if isinstance(f, (float, int)):
             if f % 1 != 0:
                 self.formula_label.set_label("=%s" % f)
             else:
                 self.formula_label.set_label("=%s" % int(f))
         else:
             self.formula_label.set_label("=%s" % f)
Ejemplo n.º 9
0
 def okbutton_clicked(self, event):
     #print(self.astentry.get_text())
     p = formula.Parser(formula=self.astentry.get_text())
     print(type(p.formula), p.formula, dir(p.formula), sep="\n")
     ast = p.parseAst()
     searchPath = self.node.searchPath(ast)
     self.destroy()
     mywin = Gtk.Window()
     treeStore = Gtk.TreeStore(str)
     fillTree_from_searchPath(searchPath, treeStore)
     treeView = Gtk.TreeView.new_with_model(treeStore)
     renderer = Gtk.CellRendererText()
     column = Gtk.TreeViewColumn("searchPath", renderer, text=0)
     treeView.append_column(column)
     mywin.add(treeView)
     mywin.connect("destroy", Gtk.main_quit)
     mywin.show_all()
     Gtk.main()
Ejemplo n.º 10
0
    def testExpProdReduction(self):

        p = formula.Parser("(x-2)^3 * (x-2)^5")

        # (x-2)^(3+5) = 0
        # x-2 = root(3+5,0)
        # x = root(3+5,0)+2

        node = p.parseAst()
        path = node.searchPath(ast.AstVariable("x"))

        print("path=%s" % _readastpath(path))
        ret, rpath, other = node.reducePath(path, AstConstant(0.0))

        ret, rpath, other = ret.reducePath(rpath, other)
        ret, rpath, other = ret.reducePath(rpath, other)

        print("res = %s,%s,%s" % (ret, rpath, other))
        self.assertEqual([], rpath)
        self.assertEqual("root(3.0+5.0,0.0)+2.0", str(other))

        other = other.simplify()
        self.assertEqual("2.0", str(other))
Ejemplo n.º 11
0
    def formula_changed(self, target, param):
        parser = formula.Parser(self.formula_entry.get_text())
        try:
            node = parser.parseAst()
        except:
            return

        old_vars = deepcopy(self.vars)
        self.vars.clear()
        node.findVars(self.vars)

        self.grid.remove(self.varsgrid)
        self.varsgrid = Gtk.Grid()
        l = list(self.vars.keys())
        self.var_to_entries = dict(zip(l,
                                       [Gtk.Entry(hexpand=True) for i in l]))
        self.entries_to_var.clear()
        for i, (var, entry) in enumerate(self.var_to_entries.items()):
            self.varsgrid.attach(Gtk.Label(var + ":"), 1, i, 1, 1)
            self.entries_to_var[entry] = var
            if var in old_vars and (old_vars[var] is not None):
                entry.set_text(str(old_vars[var]))
            entry.connect("notify::text", self.var_changed, node)
            self.varsgrid.attach(entry, 2, i, 1, 1)
        self.grid.attach(self.varsgrid, 1, 3, 2, 1)

        if len(self.vars) == 0:
            f = node.evaluate()
            if isinstance(f, (float, int)):
                if f % 1 != 0:
                    self.formula_label.set_label("=%s" % f)
                else:
                    self.formula_label.set_label("=%s" % int(f))
            else:
                self.formula_label.set_label("=%s" % f)

        self._show(node)
Ejemplo n.º 12
0
 def count_clicked(self, event):
     fp = formula.Parser(self.formula_entry.get_text())
     win2 = countwindow(fp.parseAst())
     win2.connect("destroy", Gtk.main_quit)
     win2.show_all()
     Gtk.main()
Ejemplo n.º 13
0
 def searchPath_clicked(self, event):
     fp = formula.Parser(self.formula_entry.get_text())
     win3 = searchPathwindow(fp.parseAst())
     win3.connect("destroy", Gtk.main_quit)
     win3.show_all()
     Gtk.main()