Skip to content

Commit 3ea5fa9

Browse files
author
Release Manager
committed
gh-36971: add helper method to concatenate vectors Writing `vector(R, list(v) + list(w))` is a common way to concatenate two vectors `v` and `w` over some ring `R`. In this patch we introduce a simple helper method which allows writing `v.concatenate(w)` instead. URL: #36971 Reported by: Lorenz Panny Reviewer(s): grhkm21, Ruchit Jagodara
2 parents 3abc045 + 4d1f385 commit 3ea5fa9

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

src/sage/modules/free_module_element.pyx

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4148,6 +4148,87 @@ cdef class FreeModuleElement(Vector): # abstract base class
41484148
41494149
nintegrate=nintegral
41504150
4151+
def concatenate(self, other, *, ring=None):
4152+
r"""
4153+
Return the result of concatenating this vector with a sequence
4154+
of elements given by another iterable.
4155+
4156+
If the optional keyword argument ``ring`` is passed, this method
4157+
will return a vector over the specified ring (or fail). If no
4158+
base ring is given, the base ring is determined automatically by
4159+
the :func:`vector` constructor.
4160+
4161+
EXAMPLES::
4162+
4163+
sage: v = vector([1, 2, 3])
4164+
sage: w = vector([4, 5])
4165+
sage: v.concatenate(w)
4166+
(1, 2, 3, 4, 5)
4167+
sage: v.parent()
4168+
Ambient free module of rank 3 over the principal ideal domain Integer Ring
4169+
sage: w.parent()
4170+
Ambient free module of rank 2 over the principal ideal domain Integer Ring
4171+
sage: v.concatenate(w).parent()
4172+
Ambient free module of rank 5 over the principal ideal domain Integer Ring
4173+
4174+
Forcing a base ring is possible using the ``ring`` argument::
4175+
4176+
sage: v.concatenate(w, ring=QQ)
4177+
(1, 2, 3, 4, 5)
4178+
sage: v.concatenate(w, ring=QQ).parent()
4179+
Vector space of dimension 5 over Rational Field
4180+
4181+
::
4182+
4183+
sage: v.concatenate(w, ring=Zmod(3))
4184+
(1, 2, 0, 1, 2)
4185+
4186+
The method accepts arbitrary iterables of elements which can
4187+
be coerced to a common base ring::
4188+
4189+
sage: v.concatenate(range(4,8))
4190+
(1, 2, 3, 4, 5, 6, 7)
4191+
sage: v.concatenate(range(4,8)).parent()
4192+
Ambient free module of rank 7 over the principal ideal domain Integer Ring
4193+
4194+
::
4195+
4196+
sage: w2 = [4, QQbar(-5).sqrt()]
4197+
sage: v.concatenate(w2)
4198+
(1, 2, 3, 4, 2.236...*I)
4199+
sage: v.concatenate(w2).parent()
4200+
Vector space of dimension 5 over Algebraic Field
4201+
sage: w2 = vector(w2)
4202+
sage: v.concatenate(w2)
4203+
(1, 2, 3, 4, 2.236...*I)
4204+
sage: v.concatenate(w2).parent()
4205+
Vector space of dimension 5 over Algebraic Field
4206+
4207+
::
4208+
4209+
sage: w2 = polygen(QQ)^4 + 5
4210+
sage: v.concatenate(w2)
4211+
(1, 2, 3, 5, 0, 0, 0, 1)
4212+
sage: v.concatenate(w2).parent()
4213+
Vector space of dimension 8 over Rational Field
4214+
sage: v.concatenate(w2, ring=ZZ)
4215+
(1, 2, 3, 5, 0, 0, 0, 1)
4216+
sage: v.concatenate(w2, ring=ZZ).parent()
4217+
Ambient free module of rank 8 over the principal ideal domain Integer Ring
4218+
4219+
::
4220+
4221+
sage: v.concatenate(GF(9).gens())
4222+
(1, 2, 0, z2)
4223+
sage: v.concatenate(GF(9).gens()).parent()
4224+
Vector space of dimension 4 over Finite Field in z2 of size 3^2
4225+
"""
4226+
from itertools import chain
4227+
coeffs = chain(self, other)
4228+
if ring is not None:
4229+
return vector(ring, coeffs)
4230+
return vector(coeffs)
4231+
41514232
#############################################
41524233
# Generic dense element
41534234
#############################################

0 commit comments

Comments
 (0)