Ask Your Question
3

What's the vector equivalent to Pythons list addition? `[1,2]+[3,4]=[1,2,3,4]`

asked 2010-12-09 22:53:16 -0500

mandrake gravatar image

What's the best way of appending vectors in sage? I'm after the equivalent of Python list addition [1,2]+[3,4]=[1,2,3,4]

edit retag flag offensive close merge delete

Comments

This would be super useful to have.

FrédéricC gravatar imageFrédéricC ( 2015-12-22 11:14:12 -0500 )edit

2 answers

Sort by » oldest newest most voted
3

answered 2010-12-10 06:02:54 -0500

niles gravatar image

You could define two homomorphisms into the free module of rank len(v1)+len(v2):

sage: v1 = vector([1,2])
sage: v2 = vector([3,4])
sage: R = v1.parent(); R
Ambient free module of rank 2 over the principal ideal domain Integer Ring

sage: F = FreeModule(ZZ,4)
sage: F.gens()
((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1))

sage: phi1 = R.hom(F.gens()[0:2]); phi1
Free module morphism defined by the matrix
[1 0 0 0]
[0 1 0 0]
Domain: Ambient free module of rank 2 over the principal ideal domain ...
Codomain: Ambient free module of rank 4 over the principal ideal domain ...
sage: phi2 = R.hom(F.gens()[2:4]); phi2
Free module morphism defined by the matrix
[0 0 1 0]
[0 0 0 1]
Domain: Ambient free module of rank 2 over the principal ideal domain ...
Codomain: Ambient free module of rank 4 over the principal ideal domain ...

sage: phi1(v1) + phi2(v2)
(1, 2, 3, 4)

Note that this may have some aesthetic appeal, but your vector(v1.list() + v2.list()) is much more direct :)

edit flag offensive delete link more

Comments

That, I beliveve, I would never have found myself :) Thanks

mandrake gravatar imagemandrake ( 2010-12-15 18:47:55 -0500 )edit
3

answered 2010-12-09 22:56:37 -0500

mandrake gravatar image

updated 2010-12-09 23:51:03 -0500

I'll start off with the hugely ugly create lists from vectors, append and create vector again.

>>> v1 = vector([1,2])
>>> v2 = vector([3,4])
>>> vector( list(v1) + list(v2) )
(1,2,3,4)
edit flag offensive delete link more

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: 2010-12-09 22:53:16 -0500

Seen: 1,267 times

Last updated: Dec 10 '10