Калькулятор Дерево зависимостей Python (sympy / numpy) - PullRequest
0 голосов
/ 07 декабря 2018

Я бы хотел, чтобы пользователи вводили конкретные значения, а затем система вычисляет многочисленные результаты, основываясь на том, что они - Моя программа очень усложняется всего несколькими функциями.Я включил пример с 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, но пока я не могу найти ничего, что будет работать

Ответы [ 2 ]

0 голосов
/ 07 декабря 2018

Существует онлайн-версия этого решения, которую вы можете попробовать сами.

Вот полное решение, использующее 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()))

Тестированиеit out:

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
0 голосов
/ 07 декабря 2018
In [107]: a=2.
In [108]: a=5.
In [109]: b=0.4
In [110]: c=lambda: a*b
In [111]: d=6.6
In [112]: e=lambda: b+d
In [113]: f=lambda: c()/e()
In [114]: print(a,b,c(), d, e(), f())
          5.0 0.4 2.0 6.6 7.0 0.2857142857142857

Вы, вероятно, можете захватить вышеуказанную логику в классе.Можно было бы хранить «переменные» как _a, _b и _d.Тогда a (), b () и d () могут быть функциями, которые возвращают _a и т. Д. ... Больше указатель, чем целый ответ, но это может помочь.

Использование структуры, подобной приведенной ниже, было бы возможносоздать ситуацию, когда вы всегда вызываете функцию и вам не нужно знать, когда использовать a и c (), но всегда использовать a () и c ().

In [121]: def var(init=0.0):
     ...:     def func(v=None):
     ...:         nonlocal init
     ...:         if v==None: return init
     ...:         init=v
     ...:     return func
     ...:

In [122]: a=var(100.)

In [123]: a()
Out[123]: 100.0

In [124]: a(25.)

In [125]: a()
Out[125]: 25.0
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...