Ask Your Question
1

How to concatenate 2 vectors in a third ...

asked 2021-08-08 11:01:44 +0200

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

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2021-08-08 12:03:15 +0200

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

Comments

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

concatene_vectors(a,b):
    return vector(chain(a,b))
ypcman gravatar imageypcman ( 2021-08-08 12:28:41 +0200 )edit

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

slelievre gravatar imageslelievre ( 2021-08-21 14:22:37 +0200 )edit
0

answered 2021-08-08 17:37:49 +0200

Emmanuel Charpentier gravatar image

updated 2021-08-08 17:50:43 +0200

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,

edit flag offensive delete link more

Comments

1

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

John Palmieri gravatar imageJohn Palmieri ( 2021-08-08 18:44:55 +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

1 follower

Stats

Asked: 2021-08-08 11:01:44 +0200

Seen: 84 times

Last updated: Aug 08 '21