方法一(手动)
查看代码,派生类 https://github.com/sympy/sympy/blob/723630c/sympy/core/function.py#L1048是顶层逻辑所在的地方。这只是顶层部分。从那时起,计算需要计算内部不同节点的导数表达式树 https://en.wikipedia.org/wiki/Abstract_syntax_tree.
表达式树的每个特定节点的逻辑位于_eval_derivative method https://github.com/sympy/sympy/search?q=%22_eval_derivative%22对应于每个特定的节点类型。
这将允许您向这些添加代码_eval_derivative
方法以便追踪整个过程并找到所有步骤。
方法2(使用示踪剂)
Python有多个tracing https://en.wikipedia.org/wiki/Tracing_(software)包。蟒蛇猎人 https://python-hunter.readthedocs.org写于@ionelmc https://stackoverflow.com/users/23658/ionelmc实际上非常好并且非常适合这个用例。
在许多其他功能中,它允许在函数开始执行时安装某些回调,并在函数返回其值时安装另一个回调。事实上这正是我们所需要的。
下面是一个展示如何使用它的示例(我在 Python 3.7.3、SymPy 1.7 和 Hunter 3.3.1 上运行并测试了它):
import hunter
import sys
from hunter import Q, When, Stop
hunter.trace(
Q(module_contains="sympy",function='_eval_derivative',kind_in=["call","return"],action=hunter.CallPrinter(repr_func=str))
)
from sympy import *
x = symbols('x')
f = 1/(x * sin(x)**2)
f.diff(x)
因此,这允许我们选择我们想要检查的数据结构,我们想要如何打印它们,并且它允许我们看到微分过程的中间步骤:
[...]7/site-packages/sympy/core/power.py:1267 call => _eval_derivative(self=sin(x)**(-2), s=x)
[...]7/site-packages/sympy/core/power.py:1267 call => _eval_derivative(self=<sympy.core.power.Pow object at 0x7f5925337150>, s=<sympy.core.symbol.Symbol object at 0x7f5925b6a2b0>)
[...]ite-packages/sympy/core/function.py:598 call => _eval_derivative(self=sin(x), s=x)
[...]ite-packages/sympy/core/function.py:598 call => _eval_derivative(self=<sympy.functions.elementary.trigonometric.sin object at 0x7f592589ee08>, s=<sympy.core.symbol.Symbol object at 0x7f5925b6a2b0>)
[...]ite-packages/sympy/core/function.py:612 return <= _eval_derivative: cos(x)
[...]ite-packages/sympy/core/function.py:612 return <= _eval_derivative: <sympy.functions.elementary.trigonometric.cos object at 0x7f592525fef8>
[...]7/site-packages/sympy/core/power.py:1271 return <= _eval_derivative: -2*cos(x)/sin(x)**3
[...]7/site-packages/sympy/core/power.py:1271 return <= _eval_derivative: <sympy.core.mul.Mul object at 0x7f5925259b48>
[...]7/site-packages/sympy/core/power.py:1267 call => _eval_derivative(self=1/x, s=x)
[...]7/site-packages/sympy/core/power.py:1267 call => _eval_derivative(self=<sympy.core.power.Pow object at 0x7f5925337200>, s=<sympy.core.symbol.Symbol object at 0x7f5925b6a2b0>)
[...]7/site-packages/sympy/core/power.py:1271 return <= _eval_derivative: -1/x**2
[...]7/site-packages/sympy/core/power.py:1271 return <= _eval_derivative: <sympy.core.mul.Mul object at 0x7f5925259f10>
如果您还想覆盖diff
函数你可以改变上面的代码并有function_in=['_eval_derivative','diff']
。这样不仅可以看到部分结果,还可以看到调用的部分diff
函数及其返回值。
方法 3(使用跟踪器,构建调用图并将其可视化)
使用 graphviz、latex 和示踪剂(同样,蟒蛇猎人 https://python-hunter.readthedocs.io/)你实际上可以看到调用图 https://en.wikipedia.org/wiki/Call_graph更清楚。渲染每个中间步骤的所有公式确实需要一些时间,因为pdflatex
正在使用(不过我确信乳胶有更快的渲染器)。
每个节点的值采用以下格式:
function_name
argument => return_value
好像有几个diff
参数等于返回值的节点,我目前不确定如何解释。
如果该图以某种方式提到了每个规则的应用位置(我想不出一个简单的方法来做到这一点),那么它可能会更有用。
这也是代码:
import hunter
import sys
from hunter import Q, When, Stop, Action
from hunter.actions import ColorStreamAction
formula_ltx = r'''
\documentclass[border=2pt,varwidth]{letter}
\usepackage{amsmath}
\pagenumbering{gobble}
\begin{document}
\[ \texttt{TITLE} \]
\[ FORMULA \]
\end{document}
'''
# ==============
# == Tracing ===
# ==============
from sympy.printing.latex import LatexPrinter, print_latex, latex
global call_tree_root
# a node object to hold an observed function call
# with its argument, its return value and its function name
class Node(object):
def __init__(self, arg=None, retval=None, func_name=None):
self.arg = arg
self.retval = retval
self.arg_ascii = ""
self.retval_ascii = ""
self.func_name = func_name
self.uid = 0
self.children = []
# this is a hunter action where we build a call graph and populate it
# so we can later render it
#
# CGBAction (Call Graph Builder Action)
class CGBAction(ColorStreamAction):
def __init__(self, *args, **kwargs):
super(ColorStreamAction, self).__init__(*args, **kwargs)
# a custom call stack
self.tstack = []
global call_tree_root
call_tree_root = Node(arg="",func_name="root")
self.node_idx = 1
self.tstack.append(call_tree_root)
def __call__(self, event):
if event.kind in ['return','call']:
if event.kind == 'return':
print(str(event.arg))
if len(self.tstack) > 0:
top = self.tstack.pop()
top.retval = latex(event.arg)
top.retval_ascii = str(event.arg)
elif event.kind == 'call':
print(str(event.locals.get('self')))
new = Node()
new.uid = self.node_idx
new.arg = latex(event.locals.get('self'))
new.arg_ascii = str(event.locals.get('self'))
top = self.tstack[-1]
self.tstack.append(new)
top.children.append(new)
new.func_name = event.module + ":" + event.function
self.node_idx += 1
hunter.trace(
Q(module_contains="sympy",function_in=['_eval_derivative','diff'],kind_in=["call","return"],action=CGBAction)
)
from sympy import *
x = symbols('x')
f = 1 / (x * sin(x)**2)
#f = 1 / (x * 3)
#f = sin(exp(cos(x)*asin(x)))
f.diff(x)
# ============================
# == Call graph rendering ====
# ============================
import os
import re
OUT_DIR="formulas"
if not os.path.exists(OUT_DIR):
os.mkdir(OUT_DIR)
def write_formula(prefix,uid,formula,title):
TEX = uid + prefix + ".tex"
PDF = uid + prefix + ".pdf"
PNG = uid + prefix + ".png"
TEX_PATH = OUT_DIR + "/" + TEX
with open(TEX_PATH,"w") as f:
ll = formula_ltx
ll = ll.replace("FORMULA",formula)
ll = ll.replace("TITLE",title)
f.write(ll)
# compile formula
CMD = """
cd formulas ;
pdflatex {TEX} ;
convert -trim -density 300 {PDF} -quality 90 -colorspace RGB {PNG} ;
""".format(TEX=TEX,PDF=PDF,PNG=PNG)
os.system(CMD)
buf_nodes = ""
buf_edges = ""
def dfs_tree(x):
global buf_nodes, buf_edges
arg = ("" if x.arg is None else x.arg)
rv = ("" if x.retval is None else x.retval)
arg = arg.replace("\r","")
rv = rv.replace("\r","")
formula = arg + "\\Rightarrow " + rv
print(x.func_name + " -> " + x.arg_ascii + " -> " + x.retval_ascii)
x.func_name = x.func_name.replace("_","\\_")
write_formula("",str(x.uid),formula,x.func_name)
buf_nodes += """
{0} [image="{0}.png" label=""];
""".format(x.uid);
for y in x.children:
buf_edges += "{0} -> {1};\n".format(x.uid,y.uid);
dfs_tree(y)
dfs_tree(call_tree_root)
g = open(OUT_DIR + "/graph.dot", "w")
g.write("digraph g{")
g.write(buf_nodes)
g.write(buf_edges)
g.write("}\n")
g.close()
os.system("""cd formulas ; dot -Tpng graph.dot > graph.png ;""")
将 SymPy 逻辑映射到微分规则
我认为剩下的一步是将中间节点从 SymPy 映射到微分规则 https://en.wikipedia.org/wiki/Differentiation_rules。以下是我能够绘制的一些内容:
-
产品规则 https://en.wikipedia.org/wiki/Product_rule映射到sympy.core.mul.Mul._eval_derivative https://github.com/sympy/sympy/blob/3c45367/sympy/core/mul.py#L905
-
链式法则 https://en.wikipedia.org/wiki/Chain_rule映射到sympy.core.function.Function._eval_derivative https://github.com/sympy/sympy/blob/3c453674cf39d18c70c27bb20d074ccd248304fe/sympy/core/function.py#L598
-
Sum rule https://en.wikipedia.org/wiki/Differentiation_rules#Differentiation_is_linear映射到sympy.core.add.Add._eval_derivative https://github.com/sympy/sympy/blob/3c453674cf39d18c70c27bb20d074ccd248304fe/sympy/core/add.py#L403
- 求和的导数映射为sympy.concrete.summations.Sum._eval_derivative https://github.com/sympy/sympy/blob/706007ca2fe279020e099d36dd1db0e33123ac4c/sympy/concrete/summations.py#L267
- 乘积的 n 阶导数莱布尼兹一般法则 https://en.wikipedia.org/wiki/General_Leibniz_rule映射到sympy.core.mul.Mul._eval_derivative_n_times https://github.com/sympy/sympy/blob/26703f859341dc06f00be81247083634671c4220/sympy/core/mul.py#L985
-
广义幂律 https://en.wikipedia.org/wiki/Differentiation_rules#Generalized_power_rule映射到sympy.core.power.Pow._eval_derivative https://github.com/sympy/sympy/blob/706007ca2fe279020e099d36dd1db0e33123ac4c/sympy/core/power.py#L1269
我还没见过分数课sympy.core
所以也许商规则 https://en.wikipedia.org/wiki/Quotient_rule是通过间接处理产品规则 https://en.wikipedia.org/wiki/Product_rule, and a 广义幂律 https://en.wikipedia.org/wiki/Differentiation_rules#Generalized_power_rule指数为-1。
Running
为了让它运行,你需要:
sudo apt-get install graphviz imagemagick texlive texlive-latex-base
还有文件/etc/ImageMagick-6/policy.xml
必须使用以下行进行更新,以允许从PDF->PNG
:
<policy domain="coder" rights="read|write" pattern="PDF" />
还有另一个调用图库叫做jonga https://github.com/bwohlberg/jonga但它有点通用,并且不允许完全过滤掉不需要的呼叫。