Sympy:手动使用平等

时间:2014-03-07 01:15:22

标签: python sympy

我目前正在做数学课程,我的目标是理解概念和过程,而不是尽快处理问题集。在求解方程时,我希望能够自己捅它们,而不是让它们为我解决。

假设我们有一个非常简单的等式z + 1 = 4 - 如果我自己解决这个问题,我显然会从双方减去1,但我无法弄清楚sympy是否提供了一个简单的方法这样做的方式。目前我能想出的最佳解决方案是:

from sympy import *
z = symbols('z')
eq1 = Eq(z + 1, 4)
Eq(eq1.lhs - 1, eq1.rhs - 1)
# Output:
# z == 3

更明显的表达式eq1 - 1仅从左侧减去。我怎样才能像这样一步一步地使用sympy来平等地工作(即没有让solve()方法给我答案)?任何指向实际上可能具有同情性的操作的指针都将受到赞赏。

1 个答案:

答案 0 :(得分:7)

https://github.com/sympy/sympy/issues/5031#issuecomment-36996878有一个“做”方法和讨论,可以让你对平等的双方“做”操作。它不被接受为SymPy的补充,但它是一个可以使用的简单附加组件。为方便起见,粘贴在这里:

def do(self, e, i=None, doit=False):
    """Return a new Eq using function given or a model
    model expression in which a variable represents each
    side of the expression.

    Examples
    ========

    >>> from sympy import Eq
    >>> from sympy.abc import i, x, y, z
    >>> eq = Eq(x, y)

    When the argument passed is an expression with one
    free symbol that symbol is used to indicate a "side"
    in the Eq and an Eq will be returned with the sides
    from self replaced in that expression. For example, to
    add 2 to both sides:

    >>> eq.do(i + 2)
    Eq(x + 2, y + 2)

    To add x to both sides:

    >>> eq.do(i + x)
    Eq(2*x, x + y)

    In the preceding it was actually ambiguous whether x or i
    was to be added but the rule is that any symbol that are
    already in the expression are not to be interpreted as the
    dummy variable. If we try to add z to each side, however, an 
    error is raised because now it is unclear whether i or z is being
    added:

    >>> eq.do(i + z)
    Traceback (most recent call last):
    ...
    ValueError: not sure what symbol is being used to represent a side

    The ambiguity must be resolved by indicating with another parameter 
    which is the dummy variable representing a side:

    >>> eq.do(i + z, i)
    Eq(x + z, y + z)

    Alternatively, if only one Dummy symbol appears in the expression then
    it will be automatically used to represent a side of the Eq.

    >>> eq.do(2*Dummy() + z)
    Eq(2*x + z, 2*y + z)

    It is also possible to do Eq/Eq operations:
    >>> eq.do(i + Eq(x, 2))
    Eq(2*x, y + 2)

    Operations like differentiation must be passed as a
    lambda:

    >>> Eq(x, y).do(lambda i: i.diff(x))
    Eq(1, 0)

    Because doit=False by default, the result is not evaluated. to
    evaluate it, either use the doit method or pass doit=True.

    >>> _.doit == Eq(x, y).do(lambda i: i.diff(x), doit=True)
    True
    """
    if not isinstance(e, (FunctionClass, Lambda, type(lambda:1))):
      e = S(e)
      if len(e.args) == 2:
          a, b = e.args
          if isinstance(a, Equality)  or isinstance(b, Equality):
              if isinstance(b, Symbol):
                  return self.func(e.func(a.lhs, self.lhs),
                      e.func(a.rhs, self.rhs), evaluate=doit)
              if isinstance(a, Symbol):
                  return self.func(e.func(self.lhs, b.lhs),
                      e.func(self.rhs, b.rhs), evaluate=doit)
              raise ValueError('expecting 1 arg to be a symbol')
      imaybe = e.free_symbols - self.free_symbols
      if not imaybe:
          raise ValueError('expecting a symbol')
      if imaybe and i and i not in imaybe:
          raise ValueError('indicated i not in given expression')
      if len(imaybe) != 1 and not i:
          d = [i for i in i if isinstance(i, Dummy)]
          if len(d) != 1:
              raise ValueError(
                  'not sure what symbol is being used to represent a side')
          i = set(d)
      i = i.pop()
      f = lambda side: e.subs(i, side)
    else:
      f = e
    return self.func(*[f(side) for side in self.args], evaluate=doit)

from sympy.core.relational import Equality
Equality.do = do