如何创建特定的上三角矩阵?
How to create a specific upper triangular matrix?
我想在 python(使用 numpy
)中创建一个上三角矩阵,形式为:
[[ 1, c, c^2],
[ 0, 1, c ],
[ 0, 0, 1 ]])
其中 c
是有理数,矩阵的秩可能会有所不同(2、3、4、...)。除了创建行并堆叠它们之外,还有什么聪明的方法可以做到这一点吗?
可能有更直接的解决方案,但这是我想出的:
import numpy as np
c=5
m=np.triu(c**np.triu(np.ones((3,3)), 1).cumsum(axis =1))
print(m)
输出:
[[ 1. 5. 25.]
[ 0. 1. 5.]
[ 0. 0. 1.]]
r = 3
c = 3
i,j = np.indices((r,r))
np.triu(float(c)**(j-i))
结果:
array([[1., 3., 9.],
[0., 1., 3.],
[0., 0., 1.]])
我想在 python(使用 numpy
)中创建一个上三角矩阵,形式为:
[[ 1, c, c^2],
[ 0, 1, c ],
[ 0, 0, 1 ]])
其中 c
是有理数,矩阵的秩可能会有所不同(2、3、4、...)。除了创建行并堆叠它们之外,还有什么聪明的方法可以做到这一点吗?
可能有更直接的解决方案,但这是我想出的:
import numpy as np
c=5
m=np.triu(c**np.triu(np.ones((3,3)), 1).cumsum(axis =1))
print(m)
输出:
[[ 1. 5. 25.]
[ 0. 1. 5.]
[ 0. 0. 1.]]
r = 3
c = 3
i,j = np.indices((r,r))
np.triu(float(c)**(j-i))
结果:
array([[1., 3., 9.],
[0., 1., 3.],
[0., 0., 1.]])