SymPy:删除矩阵的多行
是否有一种方法可以删除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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用切片或外部索引选择所需的行:
You can use slicing or outer indexing to select the rows that you want:
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 therow_del
function did support deleting multiple rows at once, the syntax for it would not look likerow_del(1:2)
, because that's not valid Python.关于您的后续问题,使用 Python 工作的好处之一是您可以编写自己的实用函数来使事情按照您想要的方式工作。因此,您可以创建一个函数来创建一个矩阵,该矩阵会根据需要自动返回索引。另请注意,选择带有列表的行或列会将它们按请求的顺序排列:
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: