Sympy:手动处理等式 [英] Sympy: working with equalities manually

查看:27
本文介绍了Sympy:手动处理等式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在上一门数学课程,我的目标是理解概念和过程,而不是尽快解决问题集.解方程时,我更希望自己能戳一下,而不是让他们替我解.

I'm currently doing a maths course where my aim is to understand the concepts and process rather than crunch through problem sets as fast as possible. When solving equations, I'd like to be able to poke at them myself rather than have them solved for me.

假设我们有一个非常简单的方程 z + 1 = 4 - 如果我自己解决这个问题,我显然会从两边减去 1,但我不知道 z + 1 = 4code>sympy 提供了一种简单的方法来做到这一点.目前我能想到的最佳解决方案是:

Let's say we have the very simple equation z + 1 = 4- if I were to solve this myself, I would obviously subtract 1 from both sides, but I can't figure out if sympy provides a simple way to do this. At the moment the best solution I can come up with is:

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() 方法只是给我答案)?任何指向 sympy 等式实际可能的操作的指针将不胜感激.

Where the more obvious expression eq1 - 1 only subtracts from the left-hand side. How can I use sympy to work through equalities step-by-step like this (i.e. without getting the solve() method to just given me the answer)? Any pointers to the manipulations that are actually possible with sympy equalities would be appreciated.

推荐答案

https://github.com/sympy/sympy/issues/5031#issuecomment-36996878 这将允许您对相等的双方执行"操作.它未被接受为 SymPy 的附加组件,但它是您可以使用的简单附加组件.为了方便贴在这里:

There is a "do" method and discussion at https://github.com/sympy/sympy/issues/5031#issuecomment-36996878 that would allow you to "do" operations to both sides of an Equality. It's not been accepted as an addition to SymPy but it is a simple add-on that you can use. It is pasted here for convenience:

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)

    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)
      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 imaybe if isinstance(i, Dummy)]
          if len(d) != 1:
              raise ValueError(
                  'not sure what symbol is being used to represent a side')
          i = set(d)
      else:
          i = imaybe
      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

这篇关于Sympy:手动处理等式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆