Skip to content

Commit 6663315

Browse files
committed
helper method to concatenate vectors
1 parent e249bef commit 6663315

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/sage/modules/free_module_element.pyx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4148,6 +4148,37 @@ cdef class FreeModuleElement(Vector): # abstract base class
41484148
41494149
nintegrate=nintegral
41504150
4151+
def concatenate(self, other):
4152+
r"""
4153+
Return the result of concatenating this vector with another
4154+
vector over the same ring.
4155+
4156+
EXAMPLES::
4157+
4158+
sage: v = vector([1, 2, 3])
4159+
sage: w = vector([4, 5])
4160+
sage: v.concatenate(w)
4161+
(1, 2, 3, 4, 5)
4162+
sage: v.parent()
4163+
Ambient free module of rank 3 over the principal ideal domain Integer Ring
4164+
sage: w.parent()
4165+
Ambient free module of rank 2 over the principal ideal domain Integer Ring
4166+
sage: v.concatenate(w).parent()
4167+
Ambient free module of rank 5 over the principal ideal domain Integer Ring
4168+
4169+
The method fails when the vectors aren't defined over the same ring::
4170+
4171+
sage: w2 = vector(QQ, [4, 5])
4172+
sage: v.concatenate(w2)
4173+
Traceback (most recent call last):
4174+
...
4175+
ValueError: can only concatenate vectors over the same base ring
4176+
"""
4177+
R = self.parent().base_ring()
4178+
if other.parent().base_ring() != R:
4179+
raise ValueError('can only concatenate vectors over the same base ring')
4180+
return vector(R, list(self) + list(other))
4181+
41514182
#############################################
41524183
# Generic dense element
41534184
#############################################

0 commit comments

Comments
 (0)