Sympy:手动使用平等 - python

我目前正在上一门数学课程,其目的是理解概念和过程,而不是尽快解决问题。在求解方程式时,我想自己戳一下它们,而不是让它们为我求解。

假设我们有一个非常简单的等式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()方法只是给我答案)?任何有可能在对称相等的情况下进行操作的指针都将受到赞赏。

参考方案

https://github.com/sympy/sympy/issues/5031#issuecomment-36996878处有一个“ do”方法和讨论,使您可以对Equal的双方进行“ do”操作。它不被接受为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)

    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

Python:如何从字节中提取特定位? - python

我有一条消息,显示为14 09 00 79 3d 00 23 27。我可以通过调用message[4]从此消息中提取每个字节,例如,这将给我3d。如何从该字节中提取单个8位?例如,如何将24-27位作为单个消息?只需28位? 参考方案 要回答问题的第二部分,您可以使用按位运算来获取特定的位值# getting your message as int i = …

Python:检查是否存在维基百科文章 - python

我试图弄清楚如何检查Wikipedia文章是否存在。例如,https://en.wikipedia.org/wiki/Food 存在,但是https://en.wikipedia.org/wiki/Fod 不会,页面只是说:“维基百科没有此名称的文章。”谢谢! 参考方案 >>> import urllib >>> prin…

Python pytz时区函数返回的时区为9分钟 - python

由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…

Python GPU资源利用 - python

我有一个Python脚本在某些深度学习模型上运行推理。有什么办法可以找出GPU资源的利用率水平?例如,使用着色器,float16乘法器等。我似乎在网上找不到太多有关这些GPU资源的文档。谢谢! 参考方案 您可以尝试在像Renderdoc这样的GPU分析器中运行pyxthon应用程序。它将分析您的跑步情况。您将能够获得有关已使用资源,已用缓冲区,不同渲染状态上…

Python Pandas导出数据 - python

我正在使用python pandas处理一些数据。我已使用以下代码将数据导出到excel文件。writer = pd.ExcelWriter('Data.xlsx'); wrong_data.to_excel(writer,"Names which are wrong", index = False); writer.…