Ask Your Question

Revision history [back]

The stack method of matrices provides a workaround.

If a and b are matrices with the same number of columns, a.stack(b) produces a matrix with the rows of a stacked on top of the rows of b.

sage: a = identity_matrix(3)
sage: b = matrix([[3, 3, 3]])
sage: a[:2].stack(b).stack(a[2:])
[1 0 0]
[0 1 0]
[3 3 3]
[0 0 1]

This works over any ring.

sage: aa = a.change_ring(GF(5))
sage: bb = b.change_ring(GF(5))
sage: aa[:2].stack(bb).stack(aa[2:])
[1 0 0]
[0 1 0]
[3 3 3]
[0 0 1]

The stack method and augment methods of matrices provides provide a workaround.

If a and b are matrices with the have same number of columns, columns, a.stack(b) produces a is

  • the matrix with the rows of a stacked a stacked on top of b
  • equivalently, a with extra rows from b

If a and c have same number of rows, a.augment(c) is

  • the rows of b.matrix a augmented to the right by c
  • equivalently, a with extra columns from c

For adding rows, one can use thestack method.

sage: a = identity_matrix(3)
sage: b = matrix([[3, 3, 3]])
sage: a[:2].stack(b).stack(a[2:])
a[:2, :].stack(b).stack(a[2:, :])
[1 0 0]
[0 1 0]
[3 3 3]
[0 0 1]

This works over any ring.

sage: aa = a.change_ring(GF(5))
sage: bb = b.change_ring(GF(5))
sage: aa[:2].stack(bb).stack(aa[2:])
aa[:, :2].stack(bb).stack(aa[:, 2:])
[1 0 0]
[0 1 0]
[3 3 3]
[0 0 1]

For adding columns, one can similarly use the augment method.

sage: a = identity_matrix(3)
sage: c = matrix([[3], [3], [3]])
sage: a[:, :2].augment(c).augment(a[:, 2:])
[1 0 3 0]
[0 1 3 0]
[0 0 3 1]

This also works over any ring.

sage: aa = a.change_ring(GF(5))
sage: cc = c.change_ring(GF(5))
sage: aa[:, :2].augment(cc).augment(aa[:, 2:])
[1 0 3 0]
[0 1 3 0]
[0 0 3 1]