Ask Your Question
0

Number of elements in vector

asked 2019-01-10 10:03:49 +0200

Fjolfrin gravatar image

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.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2019-01-10 11:25:21 +0200

FrédéricC gravatar image

This can be found using len(v)

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2019-01-10 10:03:49 +0200

Seen: 1,117 times

Last updated: Jan 10 '19