循环遍历 sage 中矩阵的行

Jak*_*lat 1 python numpy matrix sage

我正在尝试在 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;
Run Code Online (Sandbox Code Playgroud)

Fre*_*Foo 5

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

for row in W:
Run Code Online (Sandbox Code Playgroud)

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

for i, row in enumerate(W):
Run Code Online (Sandbox Code Playgroud)

或(更丑)

for i in xrange(len(W.shape[0])):
Run Code Online (Sandbox Code Playgroud)