Python 如何访问行操作的symphy矩阵元素?
我正在寻找一种访问Symphy矩阵元素以执行行操作的方法,但似乎找不到这样做的方法,也找不到任何描述该过程的现有文档 例如,假设我有以下代码:Python 如何访问行操作的symphy矩阵元素?,python,matrix,sympy,Python,Matrix,Sympy,我正在寻找一种访问Symphy矩阵元素以执行行操作的方法,但似乎找不到这样做的方法,也找不到任何描述该过程的现有文档 例如,假设我有以下代码: import sympy as sp from sympy import * matrix = sp.Matrix([[3,2,2],[1,2,3]]) 我想将第一行和第二列中的元素分开,在本例中为2。我能想到的一种非常简单的方法是: a = int(matrix.row(0).col(2)[0]) matrix.row(0)/a 但是现在矩阵的第
import sympy as sp
from sympy import *
matrix = sp.Matrix([[3,2,2],[1,2,3]])
我想将第一行和第二列中的元素分开,在本例中为2。我能想到的一种非常简单的方法是:
a = int(matrix.row(0).col(2)[0])
matrix.row(0)/a
但是现在矩阵的第一行是
[3/2,1,1]
这次我想再次将行除以3/2,我以前的方法对此不起作用。如何执行这些行操作,以及如何让它们更新原始矩阵?(即,当我将一行除以3时,它会更新原始矩阵中的行,而不仅仅返回一个单独的矩阵来反映更新后的行)
还有,是否有任何简单的方法可以使用Symphy矩阵进行行交换/交换(即r1 r2)
编辑:
我发现我可以简单地使用
矩阵[row#,:]/matrix[row#,column#]
来完成问题的除法部分,但我仍然不确定如何将此行操作直接反映在原始矩阵中,或者如何进行行交换。当我有这样的问题时,我会尝试搜索目录寻求帮助:
>>> [w for w in dir(Matrix) if 'op' in w and not w.startswith('_')]
[col_op, copy, copyin_list, copyin_matrix, elementary_col_op, elementary_row_op,
row_op, zip_row_op]
>>> help(Matrix.row_op)
Help on method row_op in module sympy.matrices.dense:
row_op(self, i, f) unbound sympy.matrices.dense.MutableDenseMatrix method
In-place operation on row ``i`` using two-arg functor whose args are
interpreted as ``(self[i, j], j)``.
...
>>> help(Matrix.elementary_row_op)
Help on method elementary_row_op in module sympy.matrices.matrices:
elementary_row_op(self, op='n->kn', row=None, k=None, row1=None, row2=None) unbound
sympy.matrices.dense.MutableDenseMatrix method
Performs the elementary row operation `op`.
`op` may be one of
* "n->kn" (row n goes to k*n)
* "n<->m" (swap row n and row m)
* "n->n+km" (row n goes to row n + k*row m)
Parameters
==========
op : string; the elementary row operation
row : the row to apply the row operation
k : the multiple to apply in the row operation
row1 : one row of a row swap
row2 : second row of a row swap or row "m" in the row operation
"n->n+km"
或
当我遇到这样的问题时,我会尝试搜索目录以寻求帮助:
>>> [w for w in dir(Matrix) if 'op' in w and not w.startswith('_')]
[col_op, copy, copyin_list, copyin_matrix, elementary_col_op, elementary_row_op,
row_op, zip_row_op]
>>> help(Matrix.row_op)
Help on method row_op in module sympy.matrices.dense:
row_op(self, i, f) unbound sympy.matrices.dense.MutableDenseMatrix method
In-place operation on row ``i`` using two-arg functor whose args are
interpreted as ``(self[i, j], j)``.
...
>>> help(Matrix.elementary_row_op)
Help on method elementary_row_op in module sympy.matrices.matrices:
elementary_row_op(self, op='n->kn', row=None, k=None, row1=None, row2=None) unbound
sympy.matrices.dense.MutableDenseMatrix method
Performs the elementary row operation `op`.
`op` may be one of
* "n->kn" (row n goes to k*n)
* "n<->m" (swap row n and row m)
* "n->n+km" (row n goes to row n + k*row m)
Parameters
==========
op : string; the elementary row operation
row : the row to apply the row operation
k : the multiple to apply in the row operation
row1 : one row of a row swap
row2 : second row of a row swap or row "m" in the row operation
"n->n+km"
或
我仍然有点像
sympy
新手,但在numpy
方面很有知识。那么,让我们看看sympy
的行为是否与之类似
在isympy
会话中:
In [67]: M = Matrix([[3,2,2],[1,2,3]])
In [68]: M
Out[68]:
⎡3 2 2⎤
⎢ ⎥
⎣1 2 3⎦
In [69]: M[0,:] # a row, using a numpy style indexing
Out[69]: [3 2 2]
In [70]: M[0,1] # an element
Out[70]: 2
In [71]: M[0,:]/M[0,1] # division, producing a new matrix
Out[71]: [3/2 1 1]
In [72]: M # no change to M
Out[72]:
⎡3 2 2⎤
⎢ ⎥
⎣1 2 3⎦
In [73]: M[0,:]/=M[0,1] # but with a /= (Python syntax)
In [74]: M
Out[74]:
⎡3/2 1 1⎤
⎢ ⎥
⎣ 1 2 3⎦
In [75]: M[0,:]/=3/2 # again
In [76]: M
Out[76]:
⎡1.0 0.666666666666667 0.666666666666667⎤
⎢ ⎥
⎣ 1 2 3 ⎦
这是一个浮点除法;我怀疑用一个不同的除数,我可以做一个适当的分数除法
In [83]: M = Matrix([[3,2,2],[1,2,3]])
In [84]: M[0,:]/=M[0,1]
In [85]: M[0,:]/=Rational(3,2)
In [86]: M
Out[86]:
⎡1 2/3 2/3⎤
⎢ ⎥
⎣1 2 3 ⎦
我仍然有点像
sympy
新手,但在numpy
方面很有知识。那么,让我们看看sympy
的行为是否与之类似
在isympy
会话中:
In [67]: M = Matrix([[3,2,2],[1,2,3]])
In [68]: M
Out[68]:
⎡3 2 2⎤
⎢ ⎥
⎣1 2 3⎦
In [69]: M[0,:] # a row, using a numpy style indexing
Out[69]: [3 2 2]
In [70]: M[0,1] # an element
Out[70]: 2
In [71]: M[0,:]/M[0,1] # division, producing a new matrix
Out[71]: [3/2 1 1]
In [72]: M # no change to M
Out[72]:
⎡3 2 2⎤
⎢ ⎥
⎣1 2 3⎦
In [73]: M[0,:]/=M[0,1] # but with a /= (Python syntax)
In [74]: M
Out[74]:
⎡3/2 1 1⎤
⎢ ⎥
⎣ 1 2 3⎦
In [75]: M[0,:]/=3/2 # again
In [76]: M
Out[76]:
⎡1.0 0.666666666666667 0.666666666666667⎤
⎢ ⎥
⎣ 1 2 3 ⎦
这是一个浮点除法;我怀疑用一个不同的除数,我可以做一个适当的分数除法
In [83]: M = Matrix([[3,2,2],[1,2,3]])
In [84]: M[0,:]/=M[0,1]
In [85]: M[0,:]/=Rational(3,2)
In [86]: M
Out[86]:
⎡1 2/3 2/3⎤
⎢ ⎥
⎣1 2 3 ⎦