如何访问行操作的 sympy 矩阵元素? [英] How to access sympy matrix elements for row operations?
问题描述
我正在寻找一种访问 sympy 矩阵元素以执行行操作的方法,但似乎无法想出一种方法或找到任何描述该过程的现有文档.
例如,假设我有以下代码:
import sympy as sp从 sympy 导入 *矩阵 = sp.Matrix([[3,2,2],[1,2,3]])
我想划分第一行和第二列的元素,在本例中为 2.我能想到的一种非常hacky的方法是执行以下操作:
a = int(matrix.row(0).col(2)[0])矩阵.row(0)/a
但现在我的矩阵的第一行是
[3/2,1,1]
而这次我想再次将行除以 3/2,对此我以前的方法不起作用.如何执行这些行操作,以及如何让它们更新原始矩阵?(即,当我将一行除以 3 时,它会更新原始矩阵中的行,而不仅仅是返回一个仅反映更新行的单独矩阵)
而且,是否有任何简单的方法可以使用 sympy 矩阵进行行交换/交换(即 r1 <--> r2)?
我发现我可以通过简单地使用 matrix[row#,:]/matrix[row#,column#]
来完成我的问题的除法部分,但我仍然不确定如何让这个行操作直接反映在原始矩阵中,或者如何做行交换.
当我有这样的问题时,我会尝试搜索目录寻求帮助:
<预><代码>>>>[w for w in dir(Matrix) if 'op' in w 而不是 w.startswith('_')][col_op,复制,copyin_list,copyin_matrix,elementary_col_op,elementary_row_op,row_op, zip_row_op]>>>帮助(Matrix.row_op)sympy.matrices.dense 模块中方法 row_op 的帮助:row_op(self, i, f) 未绑定的 sympy.matrices.dense.MutableDenseMatrix 方法使用两个参数函子对行i"进行就地操作,其参数为解释为``(self[i, j], j)``....>>>帮助(Matrix.elementary_row_op)sympy.matrices.matrices 模块中的elementary_row_op 方法帮助:element_row_op(self, op='n->kn', row=None, k=None, row1=None, row2=None) 未绑定sympy.matrices.dense.MutableDenseMatrix 方法执行基本的行操作 `op`.`op` 可能是其中之一* "n->kn"(第 n 行到 k*n)* "n<->m"(交换第 n 行和第 m 行)* "n->n+km"(第 n 行到第 n + k* 行 m)参数==========操作:字符串;基本行操作row : 应用行操作的行k : 在行操作中应用的倍数row1 : 一行一行交换row2 :行操作中的行交换或行m"的第二行"n->n+km"所以看起来两者都可以使用.
<预><代码>>>>m = 矩阵([[3,2,2],[1,2,3]])>>>m.row_op(0, lambda x, j: x/2)>>>米矩阵([[3/2, 1, 1],[ 1, 2, 3]])>>>m.row_op(0, lambda x, j: x/(3/2))>>>米矩阵([[1, 2/3, 2/3],[1, 2, 3]])或
<预><代码>>>>m = 矩阵([[3,2,2],[1,2,3]])>>>m.elementary_row_op('n->kn',row1=0,k=1/3)矩阵([[1, 2/3, 2/3],[1, 2, 3]])I'm looking for a way to access sympy matrix elements to perform row operations, but can't seem to come up with a way to do so or find any existing documentation that describes the process.
For example, let's say I have the following code:
import sympy as sp
from sympy import *
matrix = sp.Matrix([[3,2,2],[1,2,3]])
I want to divide the element in the first row and second column, which is 2 in this case. A really hacky way to do so that I can think of would be to do the following:
a = int(matrix.row(0).col(2)[0])
matrix.row(0)/a
But now the first row of my matrix is
[3/2,1,1]
and I want to divide the row again by 3/2 this time, for which my previous method does not work. How can I perform these row operations, and how can I have them update the original matrix? (i.e., when i divide a row by 3, it updates the row in the original matrix and doesn't just return a separate matrix reflecting just the updated row)
And, is there any simple way to do row swaps/interchanges (i.e. r1 <--> r2) with a sympy matrix?
EDIT:
I figured out that I can do the division portion of my question by simply using matrix[row#,:]/matrix[row#,column#]
, but I still am unsure of how to have this row operation be directly reflected in the original matrix, or how to do row swaps.
When I have a question like this I try to search the directory for assistance:
>>> [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"
So it looks like either could be used.
>>> m = Matrix([[3,2,2],[1,2,3]])
>>> m.row_op(0, lambda x, j: x/2)
>>> m
Matrix([
[3/2, 1, 1],
[ 1, 2, 3]])
>>> m.row_op(0, lambda x, j: x/(3/2))
>>> m
Matrix([
[1, 2/3, 2/3],
[1, 2, 3]])
or
>>> m = Matrix([[3,2,2],[1,2,3]])
>>> m.elementary_row_op('n->kn',row1=0,k=1/3)
Matrix([
[1, 2/3, 2/3],
[1, 2, 3]])
这篇关于如何访问行操作的 sympy 矩阵元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!