Ask Your Question

Sum a list of vectors

asked 2013-11-04 05:18:21 +0200

anonymous user


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.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2013-11-04 05:20:41 +0200

tmonteil gravatar image

updated 2013-11-04 05:24:04 +0200

When 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)
edit flag offensive delete link more


I tried that before but the error I get is "TypeError: 'sage.modules.vector_integer_dense.Vector_integer_dense' object is not callable"

Samsa gravatar imageSamsa ( 2013-11-04 05:26:16 +0200 )edit

Can you tell me how is the list constructed ?

tmonteil gravatar imagetmonteil ( 2013-11-04 05:31:30 +0200 )edit

ok 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.

tmonteil gravatar imagetmonteil ( 2013-11-04 05:33:42 +0200 )edit

I 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.

Samsa gravatar imageSamsa ( 2013-11-04 05:41:58 +0200 )edit

Could you please tell me how to construct you list of items ?

tmonteil gravatar imagetmonteil ( 2013-11-04 11:28:13 +0200 )edit

Your Answer

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

Add Answer

Question Tools


Asked: 2013-11-04 05:18:21 +0200

Seen: 2,319 times

Last updated: Nov 04 '13