计算器依赖树Python(sympy / numpy) - python

我想让用户输入特定的值,然后系统根据这些值计算出许多结果-我的程序仅具有几个功能就变得非常复杂。我提供了一个包含3个简单函数和6个具有以下关系的变量的示例:

计算器依赖树Python(sympy / numpy) - python

我拥有的代码如下:

class MyCalculator:
def __init__(self):
    self.a = None
    self.b = None
    self.c = None
    self.d = None
    self.e = None
    self.f = None

def set(self, field, val):
    if field == "a": self.a = val
    if field == "b": self.b = val
    if field == "c": self.c = val
    if field == "d": self.d = val
    if field == "e": self.e = val

    for i in range(10):  # circle round a few times to ensure everything has computed
        if self.a and self.b:
            self.c = self.a * self.b

        if self.a and self.c:
            self.b = self.c / self.a

        if self.b and self.c:
            self.a = self.c / self.b

        if self.b and self.d:
            self.e = self.b + self.d

        if self.e and self.b:
            self.d = self.e - self.b

        if self.e and self.d:
            self.b = self.e - self.d

        if self.c and self.e:
            self.f = self.c / self.e

        if self.f and self.e:
            self.e = self.f * self.e

        if self.f and self.c:
            self.e = self.c / self.f

def status(self):
    print(f"a = {self.a} b = {self.b} c = {self.c} d = {self.d} e = {self.e} f = {self.f} ")

然后,如果我运行以下代码:

example1 = MyCalculator()
example1.set("a", 5)
example1.set("e", 7)
example1.set("c", 2)
example1.status()

这将打印出a = 5.0 b = 0.40000000000000036 c = 2.0000000000000018 d = 6.6 e = 7.0 f = 0.285714285714286

我想要一种更简单的方法,使用sympy和numpy之类的东西来达到相同的结果,但是到目前为止,我找不到任何能用的

参考方案

There's a live version of this solution online you can try for yourself

这是使用Sympy的完整解决方案。您需要做的就是在exprStr定义顶部的MyCalculator元组中输入所需的表达式,然后所有依赖项满足的东西都应该照顾好自己:

from sympy import S, solveset, Symbol
from sympy.parsing.sympy_parser import parse_expr

class MyCalculator:
    # sympy assumes all expressions are set equal to zero
    exprStr = (
        'a*b - c',
        'b + d - e',
        'c/e - f'
    )
    # parse the expression strings into actual expressions
    expr = tuple(parse_expr(es) for es in exprStr)

    # create a dictionary to lookup expressions based on the symbols they depend on
    exprDep = {}
    for e in expr:
        for s in e.free_symbols:
            exprDep.setdefault(s, set()).add(e)

    # create a set of the used symbols for input validation
    validSymb = set(exprDep.keys())

    def __init__(self, usefloat=False):
        """usefloat: if set, store values as standard Python floats (instead of the Sympy numeric types)
        """
        self.vals = {}
        self.numify = float if usefloat else lambda x: x

    def set(self, symb, val, _exclude=None):
        # ensure that symb is a sympy Symbol object
        if isinstance(symb, str): symb = Symbol(symb)
        if symb not in self.validSymb:
            raise ValueError("Invalid input symbol.\n"
                             "symb: %s, validSymb: %s" % (symb, self.validSymb))

        # initialize the set of excluded expressions, if needed
        if _exclude is None: _exclude = set()

        # record the updated value of symb
        self.vals[symb] = self.numify(val)
        # loop over all of the expressions that depend on symb
        for e in self.exprDep[symb]:
            if e in _exclude:
                # we've already calculated an update for e in an earlier recursion, skip it
                continue
            # mark that e should be skipped in future recursions
            _exclude.add(e)

            # determine the symbol and value of the next update (if any)
            nextsymbval = self.calc(symb, e)
            if nextsymbval is not None:
                # there is another symbol to update, recursively call self.set
                self.set(*nextsymbval, _exclude)

    def calc(self, symb, e):
        # find knowns and unknowns of the expression
        known = [s for s in e.free_symbols if s in self.vals]
        unknown = [s for s in e.free_symbols if s not in known]

        if len(unknown) > 1:
            # too many unknowns, can't do anything with this expression right now
            return None
        elif len(unknown) == 1:
            # solve for the single unknown
            nextsymb = unknown[0]
        else:
            # solve for the first known that isn't the symbol that was just changed
            nextsymb = known[0] if known[0] != symb else known[1]

        # do the actual solving
        sol = solveset(e, nextsymb, domain=S.Reals)

        # evaluate the solution given the known values, then return a tuple of (next-symbol, result)
        return nextsymb, sol.subs(self.vals).args[0]

    def __str__(self):
        return ' '.join(sorted('{} = {}'.format(k,v) for k,v in self.vals.items()))

测试出来:

mycalc = MyCalculator()
mycalc.set("a", 5)
mycalc.set("e", 7)
mycalc.set("c", 2)
print(mycalc)

输出:

a = 5 b = 2/5 c = 2 d = 33/5 e = 7 f = 2/7

关于Sympy的一件整洁的事情是它使用了理性的数学运算法则,从而避免了诸如2/7之类的任何奇怪的舍入错误。如果您希望将结果作为标准Python float值,则可以将usefloat标志传递给MyCalculator

mycalc = MyCalculator(usefloat=True)
mycalc.set("a", 5)
mycalc.set("e", 7)
mycalc.set("c", 2)
print(mycalc)

输出:

a = 5.0 b = 0.4 c = 2.0 d = 6.6 e = 7.0 f = 0.2857142857142857

单击按钮后如何从组合框更改变量值? - python

我正在使用未加权GPA计算器,并且对(Py)Qt Designer应用程序还是有点陌生​​。我遇到了一个问题,我不知道如何从ComboBoxes中获取结果以将其计算为名为gpa的变量。基本上,这就是我想发生的事情:如果letter_grade1 ComboBox是A +,则它将gpa加4.0如果letter_grade2 ComboBox是B,则它将gpa加…

numpy.savetxt“元组索引超出范围”? - python

我试图在文本文件中写几行,这是我使用的代码:import numpy as np # Generate some test data data = np.arange(0.0,1000.0,50.0) with file('test.txt', 'w') as outfile: outfile.write('…

numpy loadtxt单行/行作为列表 - python

我只有一个数据文件,例如: 1.2 2.1 3.2 我使用numpy版本1.3.0 loadtxt加载它 a,b,c = loadtxt("data.dat", usecols(0,1,2), unpack=True) 输出是浮点数而不是数组 a = 1.2 我希望它将是: a = array([1.2]) 如果我读取了多行文件,则该文件…

使用字典查找样式访问numpy,但保留numpy数组操作 - python

我想构造一个继承自numpy.ndarray的类,以便它可以作为numpy数组(+,-,*,/,...)进行常规操作。我唯一要更改的是我们访问数据中项目的方式。例如:import numpy as np from PIL import Image class Data(np.ndarray): """ Something mag…

Python / Scipy过滤器离散化 - python

我目前正在尝试从Matlab转向Python,并在多个方面取得了成功。但是,我经常使用的Matlab信号处理工具箱中的一个函数是impinvar函数,用于从其模拟版本计算数字滤波器。在Scipy.signal中,我仅发现bilinear函数可以执行类似的操作。但是,与Matlab bilinear function相比,它不需要可选参数来对频率进行一些预变形…