python - 从列创建 sympy 矩阵

标签 python matrix sympy

我试图通过从现有矩阵中选择列来创建一个sympy矩阵(用于计算主次数)。目前我正在这样做:

>>> A = Matrix(3,5,[2,3,4,1,34,23,12,54,5,0,0,0,3,4,5])
>>> l = [A[:,i].T for i in [2,3,0]]
>>> M = Matrix(l).T
>>> M
Matrix([
[ 4, 1,  2],
[54, 5, 23],
[ 3, 4,  0]])

但这对我来说似乎很浪费(尤其是需要转置两次。我不知道这是否很耗时)。有没有更好的办法?如果我只需要行列式,是否有更好的方法?

最佳答案

您可以使用[2, 3, 0]作为索引。

>>> A = Matrix(3, 5, [2,3,4,1,34,23,12,54,5,0,0,0,3,4,5])
>>> A[:, [2,3,0]]
Matrix([
[ 4, 1,  2],
[54, 5, 23],
[ 3, 4,  0]])
<小时/>

低版本不支持使用list作为索引,可以使用Matrix.hstack :

>>> Matrix.hstack(*(A.col(i) for i in [2,3,0]))
Matrix([
[ 4, 1,  2],
[54, 5, 23],
[ 3, 4,  0]])

Matrix.row_join:

>>> # from functools import reduce  # For Python 3.x
>>> reduce(Matrix.row_join, (A.col(i) for i in [2,3,0]), Matrix(3,0,[]))
Matrix([
[ 4, 1,  2],
[54, 5, 23],
[ 3, 4,  0]])

关于python - 从列创建 sympy 矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43777147/

相关文章:

python - 如何访问localhost :4200 running on ec2 instance

python - 有限域 : Compute the inverse of a matrix

python - 如何模式匹配和更改表达式中所有出现的子表达式?

python - 在 PySpark 中使用 'window' 函数按天分组时出现问题

python - 我的 Python 代码出了什么问题?

python - MatLab 和 numpy 中 Hermitian 矩阵特征值的差异

r - 从列表中提取累积的唯一元素

sympy - 获取 sympy.factor 中的因子列表

python - 省略某些替换值时使用 SymPy subs 方法

python - 在 App Engine 中从一个 RequestHandler 发送到另一个 RequestHandler