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 10:03:49 +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.FjolfrinThu, 10 Jan 2019 10:03:49 +0100https://ask.sagemath.org/question/44982/Defining and manipulating vector equations with cross and dot productshttps://ask.sagemath.org/question/24421/defining-and-manipulating-vector-equations-with-cross-and-dot-products/Hello, I have been experimenting with Sage to see what it can or can't do. Consider the following simple problem.
Show $[ \mathbf{A} \times (\mathbf{B} \times \mathbf{C}) ] + [ \mathbf{B} \times (\mathbf{C} \times \mathbf{A}) ] + [ \mathbf{C} \times (\mathbf{A} \times \mathbf{B}) ] = 0 $ where $\mathbf{A}, \mathbf{B}, \mathbf{C} \in \mathbb{R}^3$. In Sage I can do this in one line
eqn = A.cross_product(B.cross_product(C)) + B.cross_product(C.cross_product(A)) + C.cross_product(A.cross_product(B))
where A,B and C are elements of $SR^3$. Now I can show component wise `eqn[0].expand()` `eqn[1].expand()` `eqn[2].expand()` that it's zero.
A much simpler way is to use the identity $\mathbf{A} \times ( \mathbf{B} \times \mathbf{C} ) = \mathbf{B}( \mathbf{A} \cdot \mathbf{C} ) - \mathbf{C}( \mathbf{A} \cdot \mathbf{B} )$ and plug it in. Yet this is easier done by hand than by computer.
My question is can Sage do this? Can I define a vector equation in sage, and sub in vector identities to manipulate or simplify the equation?
Thanks
NahsiNTue, 07 Oct 2014 22:58:01 +0200https://ask.sagemath.org/question/24421/