2015-10-06 72 views
1

我有一些符號計算下面的代碼:如何在sympy中批量替換?

from sympy import * 
import numpy 
A = MatrixSymbol('A',2,2) 
f = Matrix(A).det() # for example only, the actual function is of the same flavor 

如何替代A與相同尺寸的明確矩陣?我試着下面的代碼,但它不會產生期望的結果:

f.subs(A,Matrix(numpy.random.rand(2,2))) 

返回:

Matrix([ 
[ 0.204259256795002, 0.198427966386296], 
[0.0929675373783559, 0.516653291115676]])[0, 0]*Matrix([ 
[ 0.204259256795002, 0.198427966386296], 
[0.0929675373783559, 0.516653291115676]])[1, 1] - Matrix([ 
[ 0.204259256795002, 0.198427966386296], 
[0.0929675373783559, 0.516653291115676]])[0, 1]*Matrix([ 
[ 0.204259256795002, 0.198427966386296], 
[0.0929675373783559, 0.516653291115676]])[1, 0] 
+0

這你可能感興趣的: [SymPy - 替代sybolic在矩陣中的條目(http://stackoverflow.com/questions/23208838/sympy-substitute-sybolic-entries -in-A-矩陣); [如何用symPy和numPy替換矩陣符號](http://stackoverflow.com/questions/16904924/how-to-substitute-symbol-for-matrix-using-sympy-and-numpy);和https://github.com/sympy/sympy/issues/2962 – agold

回答

0

的替代工作。它默認情況下仍未評估。爲了評估它,使用doit(),像

>>> f.subs(A,Matrix(numpy.random.rand(2,2))).doit() 
-0.0866313884475072 
+0

抱歉,'doit()'似乎沒有評估我環境中的表達式 – lingxiao

+0

Python 3.5.0(默認,2015年9月27日,12:06 :50) [GCC 4.9.2] on linux 輸入「help」,「copyright」,「credits」或「license」以獲取更多信息。 >>>從sympy進口* >>>進口numpy的 >>> A = MatrixSymbol( 'A',2,2) >>> F =矩陣(A).det() >>> F。 (Matrix Matrix(numpy.random.rand(2,2)))。doit() Matrix([0,0] * Matrix([0.351174234097573,0.828920556066121], [0.354529006116848,0.332184714163859] [0.231174234097573,0.828920556066121],... – lingxiao

+0

>>> print(sympy .__ version__) 0.7.6.1 – lingxiao