ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 10 Jan 2019 11:25:21 +0100Number of elements in vectorhttps://ask.sagemath.org/question/44982/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 vector`b = 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.Thu, 10 Jan 2019 10:03:49 +0100https://ask.sagemath.org/question/44982/number-of-elements-in-vector/Answer by FrédéricC for <p>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 vector<code>b = A * x</code>. Here's my code:</p>
<pre><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
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/44982/number-of-elements-in-vector/?answer=44983#post-id-44983This can be found using len(v)Thu, 10 Jan 2019 11:25:21 +0100https://ask.sagemath.org/question/44982/number-of-elements-in-vector/?answer=44983#post-id-44983