# Sum a list of vectors

Anonymous

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 close merge delete

Sort by ยป oldest newest most voted

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)

more

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

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

Can you tell me how is the list constructed ?

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

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

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

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

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