[sage-support] Vertical and Horizontal join of matrices

1,379 views
Skip to first unread message

VictorMiller

unread,
May 20, 2010, 6:15:55 PM5/20/10
to sage-support
Does the Matrix class have methods for vertical and horizontal joins
of matrices (as in Magma)? That is

if A is an m by n matrix and B is an r by n matrix then
VerticalJoin(A,B) would by the (m+r) by n matrix with A "on top" and B
"on the bottom". Similarly, if A is m by n and B is m by r then
HorizontalJoin(A,B) would be an m by (n+r) matrix with A "on the left"
and B "on the right". I though that Numeric Python used to have
something like this with a method called concatenate, but I can't find
that in numpy.

Victor

--
To post to this group, send email to sage-s...@googlegroups.com
To unsubscribe from this group, send email to sage-support...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-support
URL: http://www.sagemath.org

Robert Bradshaw

unread,
May 20, 2010, 6:19:43 PM5/20/10
to sage-s...@googlegroups.com
On May 20, 2010, at 3:15 PM, VictorMiller wrote:

> Does the Matrix class have methods for vertical and horizontal joins
> of matrices (as in Magma)? That is
>
> if A is an m by n matrix and B is an r by n matrix then
> VerticalJoin(A,B) would by the (m+r) by n matrix with A "on top" and B
> "on the bottom". Similarly, if A is m by n and B is m by r then
> HorizontalJoin(A,B) would be an m by (n+r) matrix with A "on the left"
> and B "on the right". I though that Numeric Python used to have
> something like this with a method called concatenate, but I can't find
> that in numpy.

You're probably looking for stack and augment.

sage: M = random_matrix(ZZ, 3, 3)
sage: M.stack(M)
[ -1 1 -5]
[ 72 -2 1]
[ -2 2 -20]
[ -1 1 -5]
[ 72 -2 1]
[ -2 2 -20]
sage: M.augment(M)
[ -1 1 -5 -1 1 -5]
[ 72 -2 1 72 -2 1]
[ -2 2 -20 -2 2 -20]

- Robert

Jason Grout

unread,
May 20, 2010, 6:37:56 PM5/20/10
to sage-s...@googlegroups.com
On 05/20/2010 05:19 PM, Robert Bradshaw wrote:
> On May 20, 2010, at 3:15 PM, VictorMiller wrote:
>
>> Does the Matrix class have methods for vertical and horizontal joins
>> of matrices (as in Magma)? That is
>>
>> if A is an m by n matrix and B is an r by n matrix then
>> VerticalJoin(A,B) would by the (m+r) by n matrix with A "on top" and B
>> "on the bottom". Similarly, if A is m by n and B is m by r then
>> HorizontalJoin(A,B) would be an m by (n+r) matrix with A "on the left"
>> and B "on the right". I though that Numeric Python used to have
>> something like this with a method called concatenate, but I can't find
>> that in numpy.
>
> You're probably looking for stack and augment.
>
> sage: M = random_matrix(ZZ, 3, 3)
> sage: M.stack(M)
> [ -1 1 -5]
> [ 72 -2 1]
> [ -2 2 -20]
> [ -1 1 -5]
> [ 72 -2 1]
> [ -2 2 -20]
> sage: M.augment(M)
> [ -1 1 -5 -1 1 -5]
> [ 72 -2 1 72 -2 1]
> [ -2 2 -20 -2 2 -20]


Also block_matrix, which internally uses stack and augment:

sage: M=random_matrix(ZZ,3,4)
sage: M
[-1 -1 2 1]
[ 1 3 2 -3]
[-1 -1 2 -1]
sage: block_matrix([M,M],nrows=2,ncols=1)
[-1 -1 2 1]
[ 1 3 2 -3]
[-1 -1 2 -1]
[-----------]
[-1 -1 2 1]
[ 1 3 2 -3]
[-1 -1 2 -1]
sage: block_matrix([M,M],nrows=1,ncols=2)
[-1 -1 2 1|-1 -1 2 1]
[ 1 3 2 -3| 1 3 2 -3]
[-1 -1 2 -1|-1 -1 2 -1]

Thanks,

Jason

VictorMiller

unread,
May 20, 2010, 7:45:24 PM5/20/10
to sage-support
Thanks!



On May 20, 6:19 pm, Robert Bradshaw <rober...@math.washington.edu>
wrote:
Reply all
Reply to author
Forward
0 new messages