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

标签 python numpy sympy

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

enter image description here

我的代码如下:

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 的完整解决方案。您所需要做的就是在 MyCalculator 定义顶部的 exprStr 元组中输入所需的表达式,然后所有依赖项满足内容都应该自行处理:

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 - 计算器依赖树 Python (sympy/numpy),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53673242/

相关文章:

python - 转换为稀疏矩阵 - TypeError : no supported conversion for types: (dtype ('0' ), )

python - 如何在 SymPy 中获得等式的两边?

python - 如何在 PyCharm 解析器的文档字符串中指定类或函数类型

python - 我在为模板渲染动态数据时遇到问题

python - 广度优先搜索 : how to get currency exchange?

python - Pandas 列对象分桶值提取

python - sympy 中的多元泰勒近似

python - 无法评估 sympy 中的跟踪

python - 如何在 python 中将 url 编码的字符串转换为正确的 unicode 字符串

python - Django - 来自应用程序目录的静态文件