SymPy:删除矩阵的多行

发布于 2025-01-17 19:52:29 字数 518 浏览 5 评论 0原文

是否有一种方法可以删除sympy中矩阵的多行(不使用numpy)?

我知道.row_del()一次只能删除一行。

from sympy import *

a_1_1, a_1_2, a_1_3 = symbols ('a_1_1 a_1_2 a_1_3')
a_2_1, a_2_2, a_2_3 = symbols ('a_2_1 a_2_2 a_2_3')
a_3_1, a_3_2, a_3_3 = symbols ('a_3_1 a_3_2 a_3_3')

A = Matrix ([
    [a_1_1, a_1_2, a_1_3], 
    [a_2_1, a_2_2, a_2_3], 
    [a_3_1, a_3_2, a_3_3]
])

A.row_del (1 : 2)

(尚未?)不起作用:

    A.row_del (1 : 2)
                 ^
SyntaxError: invalid syntax

Is there a method to delete multiple rows of a matrix in SymPy (without using NumPy)?

I understand that .row_del() can only delete one row at a time.

from sympy import *

a_1_1, a_1_2, a_1_3 = symbols ('a_1_1 a_1_2 a_1_3')
a_2_1, a_2_2, a_2_3 = symbols ('a_2_1 a_2_2 a_2_3')
a_3_1, a_3_2, a_3_3 = symbols ('a_3_1 a_3_2 a_3_3')

A = Matrix ([
    [a_1_1, a_1_2, a_1_3], 
    [a_2_1, a_2_2, a_2_3], 
    [a_3_1, a_3_2, a_3_3]
])

A.row_del (1 : 2)

does not (yet?) work:

    A.row_del (1 : 2)
                 ^
SyntaxError: invalid syntax

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倒带 2025-01-24 19:52:29

您可以使用切片或外部索引选择所需的行:

In [8]: A = MatrixSymbol('A', 3, 3).as_explicit()

In [9]: A
Out[9]: 
⎡A₀₀  A₀₁  A₀₂⎤
⎢             ⎥
⎢A₁₀  A₁₁  A₁₂⎥
⎢             ⎥
⎣A₂₀  A₂₁  A₂₂⎦

In [10]: A[:1,:]
Out[10]: [A₀₀  A₀₁  A₀₂]

In [11]: A[[0,2],:]
Out[11]: 
⎡A₀₀  A₀₁  A₀₂⎤
⎢             ⎥
⎣A₂₀  A₂₁  A₂₂⎦

You can use slicing or outer indexing to select the rows that you want:

In [8]: A = MatrixSymbol('A', 3, 3).as_explicit()

In [9]: A
Out[9]: 
⎡A₀₀  A₀₁  A₀₂⎤
⎢             ⎥
⎢A₁₀  A₁₁  A₁₂⎥
⎢             ⎥
⎣A₂₀  A₂₁  A₂₂⎦

In [10]: A[:1,:]
Out[10]: [A₀₀  A₀₁  A₀₂]

In [11]: A[[0,2],:]
Out[11]: 
⎡A₀₀  A₀₁  A₀₂⎤
⎢             ⎥
⎣A₂₀  A₂₁  A₂₂⎦
佼人 2025-01-24 19:52:29

Python 中仅支持方括号内的用于选择项目切片的 : 语法。这就是错误是 SyntaxError 的原因。因此,即使 row_del 函数确实支持一次删除多行,它的语法也不会像 row_del(1:2) 那样,因为这不是有效的 Python。

The : syntax to select a slice of items is only supported inside of square brackets in Python. That's why the error is SyntaxError. So even if the row_del function did support deleting multiple rows at once, the syntax for it would not look like row_del(1:2), because that's not valid Python.

深者入戏 2025-01-24 19:52:29

关于您的后续问题,使用 Python 工作的好处之一是您可以编写自己的实用函数来使事情按照您想要的方式工作。因此,您可以创建一个函数来创建一个矩阵,该矩阵会根据需要自动返回索引。另请注意,选择带有列表的行或列会将它们按请求的顺序排列:

>>> a1rc = lambda a,r,c: MatrixSymbol(a, r+1, c+1).as_explicit()[1:,1:]
>>> a1rc("A",3,4)
Matrix([
[A[1, 1], A[1, 2], A[1, 3], A[1, 4]],
[A[2, 1], A[2, 2], A[2, 3], A[2, 4]],
[A[3, 1], A[3, 2], A[3, 3], A[3, 4]]])
>>> _[[2,0],:]  # row 2 will come first, then row 0
Matrix([
[A[3, 1], A[3, 2], A[3, 3], A[3, 4]],
[A[1, 1], A[1, 2], A[1, 3], A[1, 4]]])

Regarding your follow-up question, part of the beauty of working in Python is that you can write your own utility functions to make things work the way you want. So you could make a function to create a matrix that automatically comes back with indices as desired. Note, too, that selecting rows or columns with a list will put them in the requested order:

>>> a1rc = lambda a,r,c: MatrixSymbol(a, r+1, c+1).as_explicit()[1:,1:]
>>> a1rc("A",3,4)
Matrix([
[A[1, 1], A[1, 2], A[1, 3], A[1, 4]],
[A[2, 1], A[2, 2], A[2, 3], A[2, 4]],
[A[3, 1], A[3, 2], A[3, 3], A[3, 4]]])
>>> _[[2,0],:]  # row 2 will come first, then row 0
Matrix([
[A[3, 1], A[3, 2], A[3, 3], A[3, 4]],
[A[1, 1], A[1, 2], A[1, 3], A[1, 4]]])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文