First time here? Check out the FAQ!

Ask Your Question
1

How to concatenate 2 vectors in a third ...

asked 3 years ago

ypcman gravatar image

Hi. I can't find in the documentation an elegant solution for this quite simple question :

a = vector([1,2,3])
b = vector([4,5])

How to find c = (1,2,3,4,5)

a+ b doesn't works like in Python ...

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
1

answered 3 years ago

rburing gravatar image

Recently there was a question about this on sage-devel, which led to the following nice suggestion:

sage: a = vector([1,2,3])
sage: b = vector([4,5])
sage: from itertools import chain
sage: c = vector(chain(a,b)); c
(1, 2, 3, 4, 5)
Preview: (hide)
link

Comments

Smart ! Danke I will create & use a simple function :

concatene_vectors(a,b):
    return vector(chain(a,b))
ypcman gravatar imageypcman ( 3 years ago )

Click the "check mark" button near the answer to accept it and mark your question as solved.

slelievre gravatar imageslelievre ( 3 years ago )
0

answered 3 years ago

Emmanuel Charpentier gravatar image

updated 3 years ago

Alternate solution :

sage: vector(vector([1, 2, 3]).list()+vector([4, 5]).list())
(1, 2, 3, 4, 5)

Hence :

sage: def concatenate_vectors(*x):
....:     return vector(reduce(lambda a,b:a+b, map(lambda u:u.list(), x)))
....: 
sage: concatenate_vectors(vector([1, 2, 3]), vector([4, 5]), vector([6]))
(1, 2, 3, 4, 5, 6)

HTH,

Preview: (hide)
link

Comments

1

Or vector(list(a) + list(b)), using the notation in the original question.

John Palmieri gravatar imageJohn Palmieri ( 3 years ago )

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 3 years ago

Seen: 1,158 times

Last updated: Aug 08 '21