Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Number of elements in vector

I want to implement the Gauss-Seidel method for solving linear systems for a university project, and I'm stuck on how to get the number of elements in the product vectorb = A * x. Here's my code:

def Gauss_Seidel(m, v, initial):

n = v.size() <---------
prev = vector(RR, n)
nex = vector(RR, n)

prev = initial

while abs(inf_vector_norm(nex) - inf_vector_norm(nex)) > 0.1:
    for i in xrange(n):
            s1 = sum(m[i,j] * nex[j] for j in xrange(i))
            s2 = sum(m[i,j] * prev[j] for j in xrange(j+1, n+1))
            nex[i] = (v[i] - s1 - s2) / m[i,i]
return nex

In the code, 'm' is the input matrix, 'v' is the product of the matrix and our variable vector, and 'initial' is the starting guess for Gauss-Seidel's Method. The line pointed by that arrow is the one I'm interested in, and it's showing what I want to accomplish. I searched for it on Sage textbooks, but they are oddly silent on the matter of vectors. Any ideas? Also any other recommendations and advice on my code is more than welcome.