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.Tue, 05 Nov 2013 03:36:07 +0100Sum a list of vectorshttps://ask.sagemath.org/question/10658/sum-a-list-of-vectors/I'm given a list of rational vectors, i.e., something like [(1,2,3), (1/2, 4, 5), (-1, 0, 0)], and I need to sum all vectors of this list.
Even though I searched a bit, I cannot find the right sage command for this task. For example, sum() does not seem to work here. At least I couldn't figure out how to apply it to a list of such type.
Of course I could use a loop and an additional variable that saves the partial sums. But there has to be a more elegant way.Mon, 04 Nov 2013 05:18:21 +0100https://ask.sagemath.org/question/10658/sum-a-list-of-vectors/Answer by tmonteil for <p>I'm given a list of rational vectors, i.e., something like [(1,2,3), (1/2, 4, 5), (-1, 0, 0)], and I need to sum all vectors of this list.</p>
<p>Even though I searched a bit, I cannot find the right sage command for this task. For example, sum() does not seem to work here. At least I couldn't figure out how to apply it to a list of such type.</p>
<p>Of course I could use a loop and an additional variable that saves the partial sums. But there has to be a more elegant way.</p>
https://ask.sagemath.org/question/10658/sum-a-list-of-vectors/?answer=15652#post-id-15652When you type:
sage: (1,2,3)
(1, 2, 3)
You get a tuple, not a vector:
sage: type((1,2,3))
<type 'tuple'>
For tuple, the sum is the concatenation:
sage: (1,2,3)+(1/2, 4, 5)
(1, 2, 3, 1/2, 4, 5)
So to sum them as vectors, you have fist to transform them into vectors, as follows:
sage: L= [(1,2,3), (1/2, 4, 5), (-1, 0, 0)]
sage: sum(vector(v) for v in L)
(1/2, 6, 8)
Mon, 04 Nov 2013 05:20:41 +0100https://ask.sagemath.org/question/10658/sum-a-list-of-vectors/?answer=15652#post-id-15652Comment by tmonteil for <p>When you type:</p>
<pre><code>sage: (1,2,3)
(1, 2, 3)
</code></pre>
<p>You get a tuple, not a vector:</p>
<pre><code>sage: type((1,2,3))
<type 'tuple'>
</code></pre>
<p>For tuple, the sum is the concatenation:</p>
<pre><code>sage: (1,2,3)+(1/2, 4, 5)
(1, 2, 3, 1/2, 4, 5)
</code></pre>
<p>So to sum them as vectors, you have fist to transform them into vectors, as follows:</p>
<pre><code>sage: L= [(1,2,3), (1/2, 4, 5), (-1, 0, 0)]
sage: sum(vector(v) for v in L)
(1/2, 6, 8)
</code></pre>
https://ask.sagemath.org/question/10658/sum-a-list-of-vectors/?comment=16802#post-id-16802Can you tell me how is the list constructed ?Mon, 04 Nov 2013 05:31:30 +0100https://ask.sagemath.org/question/10658/sum-a-list-of-vectors/?comment=16802#post-id-16802Comment by Samsa for <p>When you type:</p>
<pre><code>sage: (1,2,3)
(1, 2, 3)
</code></pre>
<p>You get a tuple, not a vector:</p>
<pre><code>sage: type((1,2,3))
<type 'tuple'>
</code></pre>
<p>For tuple, the sum is the concatenation:</p>
<pre><code>sage: (1,2,3)+(1/2, 4, 5)
(1, 2, 3, 1/2, 4, 5)
</code></pre>
<p>So to sum them as vectors, you have fist to transform them into vectors, as follows:</p>
<pre><code>sage: L= [(1,2,3), (1/2, 4, 5), (-1, 0, 0)]
sage: sum(vector(v) for v in L)
(1/2, 6, 8)
</code></pre>
https://ask.sagemath.org/question/10658/sum-a-list-of-vectors/?comment=16800#post-id-16800I don't use vector as a variable name. And the list is constructed as list = [vector(item.sage()) for item in list]. I have to use .sage() because the items in list are GAP elements.Mon, 04 Nov 2013 05:41:58 +0100https://ask.sagemath.org/question/10658/sum-a-list-of-vectors/?comment=16800#post-id-16800Comment by Samsa for <p>When you type:</p>
<pre><code>sage: (1,2,3)
(1, 2, 3)
</code></pre>
<p>You get a tuple, not a vector:</p>
<pre><code>sage: type((1,2,3))
<type 'tuple'>
</code></pre>
<p>For tuple, the sum is the concatenation:</p>
<pre><code>sage: (1,2,3)+(1/2, 4, 5)
(1, 2, 3, 1/2, 4, 5)
</code></pre>
<p>So to sum them as vectors, you have fist to transform them into vectors, as follows:</p>
<pre><code>sage: L= [(1,2,3), (1/2, 4, 5), (-1, 0, 0)]
sage: sum(vector(v) for v in L)
(1/2, 6, 8)
</code></pre>
https://ask.sagemath.org/question/10658/sum-a-list-of-vectors/?comment=16803#post-id-16803I tried that before but the error I get is "TypeError: 'sage.modules.vector_integer_dense.Vector_integer_dense' object is not callable"Mon, 04 Nov 2013 05:26:16 +0100https://ask.sagemath.org/question/10658/sum-a-list-of-vectors/?comment=16803#post-id-16803Comment by Samsa for <p>When you type:</p>
<pre><code>sage: (1,2,3)
(1, 2, 3)
</code></pre>
<p>You get a tuple, not a vector:</p>
<pre><code>sage: type((1,2,3))
<type 'tuple'>
</code></pre>
<p>For tuple, the sum is the concatenation:</p>
<pre><code>sage: (1,2,3)+(1/2, 4, 5)
(1, 2, 3, 1/2, 4, 5)
</code></pre>
<p>So to sum them as vectors, you have fist to transform them into vectors, as follows:</p>
<pre><code>sage: L= [(1,2,3), (1/2, 4, 5), (-1, 0, 0)]
sage: sum(vector(v) for v in L)
(1/2, 6, 8)
</code></pre>
https://ask.sagemath.org/question/10658/sum-a-list-of-vectors/?comment=16789#post-id-16789Dear tmonteil, I made an error. Everything you suggested in your answers works for me now. Sorry for the confusion and thanks for your help.Tue, 05 Nov 2013 03:36:07 +0100https://ask.sagemath.org/question/10658/sum-a-list-of-vectors/?comment=16789#post-id-16789Comment by tmonteil for <p>When you type:</p>
<pre><code>sage: (1,2,3)
(1, 2, 3)
</code></pre>
<p>You get a tuple, not a vector:</p>
<pre><code>sage: type((1,2,3))
<type 'tuple'>
</code></pre>
<p>For tuple, the sum is the concatenation:</p>
<pre><code>sage: (1,2,3)+(1/2, 4, 5)
(1, 2, 3, 1/2, 4, 5)
</code></pre>
<p>So to sum them as vectors, you have fist to transform them into vectors, as follows:</p>
<pre><code>sage: L= [(1,2,3), (1/2, 4, 5), (-1, 0, 0)]
sage: sum(vector(v) for v in L)
(1/2, 6, 8)
</code></pre>
https://ask.sagemath.org/question/10658/sum-a-list-of-vectors/?comment=16801#post-id-16801ok i guess that you use the word `vector` as a variable name. You should not because the existing `vector` function is useful. You can do ` reset('vector')` before.Mon, 04 Nov 2013 05:33:42 +0100https://ask.sagemath.org/question/10658/sum-a-list-of-vectors/?comment=16801#post-id-16801Comment by tmonteil for <p>When you type:</p>
<pre><code>sage: (1,2,3)
(1, 2, 3)
</code></pre>
<p>You get a tuple, not a vector:</p>
<pre><code>sage: type((1,2,3))
<type 'tuple'>
</code></pre>
<p>For tuple, the sum is the concatenation:</p>
<pre><code>sage: (1,2,3)+(1/2, 4, 5)
(1, 2, 3, 1/2, 4, 5)
</code></pre>
<p>So to sum them as vectors, you have fist to transform them into vectors, as follows:</p>
<pre><code>sage: L= [(1,2,3), (1/2, 4, 5), (-1, 0, 0)]
sage: sum(vector(v) for v in L)
(1/2, 6, 8)
</code></pre>
https://ask.sagemath.org/question/10658/sum-a-list-of-vectors/?comment=16799#post-id-16799Could you please tell me how to construct you list of items ?Mon, 04 Nov 2013 11:28:13 +0100https://ask.sagemath.org/question/10658/sum-a-list-of-vectors/?comment=16799#post-id-16799