如何访问行操作的sympy矩阵元素?

2024-10-16 22:36:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找一种访问sypy矩阵元素以执行行操作的方法,但似乎找不到这样做的方法,也找不到任何描述该过程的现有文档。在

例如,假设我有以下代码:

import sympy as sp
from sympy import *

matrix = sp.Matrix([[3,2,2],[1,2,3]])

我想划分第一行和第二列中的元素,在本例中是2。我能想到的一个非常老套的方法是:

^{pr2}$

但现在矩阵的第一行是

[3/2,1,1]

这次我想把这行再除以3/2,我之前的方法不适用。如何执行这些行操作,以及如何让它们更新原始矩阵?(即,当我将一行除以3时,它会更新原始矩阵中的行,而不仅仅返回一个单独的矩阵,只反映更新的行)

矩阵r1与sympe交换有何方法?在

编辑:

我发现我可以通过简单地使用matrix[row#,:]/matrix[row#,column#]来完成问题的除法部分,但是我仍然不确定如何将这个行操作直接反映在原始矩阵中,或者如何进行行交换。在


Tags: 方法代码from文档import元素过程as
2条回答

当我遇到这样的问题时,我会尝试搜索目录以获得帮助:

>>> [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"

所以看起来两者都可以用。在

^{pr2}$

或者

>>> 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新手,但对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        ⎦

这是一个浮点除法;我怀疑用一个不同的除数,我可以做一个正确的分数除法。在

^{pr2}$

相关问题 更多 >