Ask Your Question
0

Sum a list of vectors

asked 2013-11-03 22:18:21 -0500

anonymous user

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

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-11-03 22:20:41 -0500

tmonteil gravatar image

updated 2013-11-03 22:24:04 -0500

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

Comments

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-03 22:26:16 -0500 )edit

Can you tell me how is the list constructed ?

tmonteil gravatar imagetmonteil ( 2013-11-03 22:31:30 -0500 )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-03 22:33:42 -0500 )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-03 22:41:58 -0500 )edit

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

tmonteil gravatar imagetmonteil ( 2013-11-04 04:28:13 -0500 )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

Stats

Asked: 2013-11-03 22:18:21 -0500

Seen: 554 times

Last updated: Nov 03 '13