Macaulay2 » Documentation
Packages » Macaulay2Doc > matrices > concatenating matrices
next | previous | forward | backward | up | index | toc

concatenating matrices

Before Macaulay2 can concatenate matrices, the matrices in question must have entries in the same ring.

concatenate horizontally

Use | to concatenate two matrices horizontally.
i1 : R = ZZ/32003[a..f];
i2 : M = genericMatrix(R,a,3,2)

o2 = | a d |
     | b e |
     | c f |

             3      2
o2 : Matrix R  <-- R
i3 : N = matrix{{d^2,a*d},{b*c,b*d},{a,c}}

o3 = | d2 ad |
     | bc bd |
     | a  c  |

             3      2
o3 : Matrix R  <-- R
i4 : M|N

o4 = | a d d2 ad |
     | b e bc bd |
     | c f a  c  |

             3      4
o4 : Matrix R  <-- R

concatenate vertically

Use || to concatenate two matrices vertically.
i5 : P = matrix{{d^2,a*d,e*f},{b*c,b*d,b*e},{a,c,d}}

o5 = | d2 ad ef |
     | bc bd be |
     | a  c  d  |

             3      3
o5 : Matrix R  <-- R
i6 : transpose(M)||P

o6 = {-1} | a  b  c  |
     {-1} | d  e  f  |
     {0}  | d2 ad ef |
     {0}  | bc bd be |
     {0}  | a  c  d  |

             5      3
o6 : Matrix R  <-- R

making block matrices

The matrix function can take matrices as input as long as the sizes match up.
i7 : matrix{{id_(R^3),M,P},{random(R^1,R^3),random(R^1,R^3),random(R^1,R^2)}}

o7 = | 1   0    0     a    d    d2    ad   ef     |
     | 0   1    0     b    e    bc    bd   be     |
     | 0   0    1     c    f    a     c    d      |
     | 107 4376 -5570 3187 3783 -5307 8570 -15344 |

             4      8
o7 : Matrix R  <-- R
Also, the number input entries in each row must be equal. It might seem like we could form the same matrix with the input matrix{{id_(R^3),M,P},{random(R^1,R^8)}} since random(R^1,R^8) will construct a 1 by 8 matrix that has the same number of columns as matrix matrix{{id_(R^3),M,P}} but as input into the matrix function that row entry must have 3 entries.

direct sum of matrices as maps between modules

Use ++ to find the direct sum of two matrices.
i8 : R = ZZ/101[x,y,z];
i9 : p = matrix table(3,3,(i,j) -> R_i^j)

o9 = | 1 x x2 |
     | 1 y y2 |
     | 1 z z2 |

             3      3
o9 : Matrix R  <-- R
i10 : q = matrix table(3,3,(i,j) -> R_j^i)

o10 = | 1  1  1  |
      | x  y  z  |
      | x2 y2 z2 |

              3      3
o10 : Matrix R  <-- R
i11 : r=p++q

o11 = | 1 x x2 0  0  0  |
      | 1 y y2 0  0  0  |
      | 1 z z2 0  0  0  |
      | 0 0 0  1  1  1  |
      | 0 0 0  x  y  z  |
      | 0 0 0  x2 y2 z2 |

              6      6
o11 : Matrix R  <-- R
The components of a direct sum can be recovered later.
i12 : components r

o12 = {| 1 x x2 |, | 1  1  1  |}
       | 1 y y2 |  | x  y  z  |
       | 1 z z2 |  | x2 y2 z2 |

o12 : List