【问题标题】:looping through the rows of a matrix in sage在 sage 中循环遍历矩阵的行
【发布时间】:2011-12-12 14:04:25
【问题描述】:

我正在尝试在 sage 中编写 Graham-Schmidt 过程,但无法弄清楚如何循环遍历数组的行。

def graham_schmidt(W):
    a=0
    U=W 
    for i in W.dims()[0]:# this is the not working part
        print w
        a=a+1
        for j in xrange(0,-2):
            a=a+1
            U[i]=U[i]-(transpose(U[j])*w)/(transpose(U[j])*U[j])*U[j]
    return a;

【问题讨论】:

  • W的类型是什么?你试过这个吗:for i in W.dims()[0]?变量w在哪里取值?

标签: python matrix numpy sage


【解决方案1】:

你把事情弄得太复杂了。如果W 不是稀疏矩阵,你可以这样做

for row in W:

由于还需要行索引,所以可以使用Python内置的enumerate

for i, row in enumerate(W):

或(更丑)

for i in xrange(len(W.shape[0])):

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    相关资源
    最近更新 更多