【问题标题】:Replace "a**b" to pow(a,b) [duplicate]将“a**b”替换为 pow(a,b) [重复]
【发布时间】:2017-11-24 00:35:26
【问题描述】:

例如,我有以下代码:

import sympy as sp
t = sp.Symbol("t")
f = t**2
print(f)

输出:t**2

我可以使用 sympy 获得输出 pow(t,2) 吗?

谢谢。

【问题讨论】:

    标签: sympy symbolic-math


    【解决方案1】:

    你可以尝试破解字符串打印机:

    from sympy.printing import StrPrinter
    StrPrinter._print_Pow = lambda self, expr: "pow(%s, %s)" % (self._print(expr.base), self._print(expr.exp))
    

    即这覆盖了StrPrinter类中的_print_Pow方法,该方法负责打印Pow对象的字符串形式。

    【讨论】:

      猜你喜欢
      • 2012-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      • 1970-01-01
      • 2021-10-06
      • 2011-06-21
      • 2015-07-30
      相关资源
      最近更新 更多