鉴于为数值编写的现有函数,Sympy是否有建议的方法来扩展它处理符号?例如:
import numpy as np
def foo(ax, ay, az):
A = np.matrix([[1. , ax, 0],
[0 , 1./ay, 0],
[0 , 0, np.cos(az)]])
return A
foo(1, 2, 3)
# We want this to work too:
import sympy as spy
ax, ay, az = spy.symbols(('ax', 'ay', 'az'))
foo(ax, ay, az)
如何扩展foo()
处理交集符号ax, ay, az
,从而返回符号矩阵A
?是否有任何公用事业可以帮助解决这个问题?
答案 0 :(得分:0)
import numpy as np
import sympy as spy
def foo(ax, ay, az):
A = np.matrix([[1., ax, 0 ],
[0 , 1./ay, 0 ],
[0 , 0 , spy.cos(az) if type(az) is spy.Symbol else np.cos(az)]])
return A
print foo(1, 2, 3)
ax, ay, az = spy.symbols(('ax', 'ay', 'az'))
print foo(ax, ay, az)
>>>
[[ 1. 1. 0. ]
[ 0. 0.5 0. ]
[ 0. 0. -0.9899925]]
[[1.0 ax 0]
[0 1.0/ay 0]
[0 0 cos(az)]]