Ask Your Question

Revision history [back]

The insert_row method does this:

sage: m = identity_matrix(3)
sage: m
[1 0 0]
[0 1 0]
[0 0 1]
sage: m2= m.insert_row(2, [3,3,3])
sage: m2
[1 0 0]
[0 1 0]
[3 3 3]
[0 0 1]
sage: m # original matrix is unchanged
[1 0 0]
[0 1 0]
[0 0 1]

To insert a column, take the transpose, insert a row, and take the transpose again.

Another option:

sage: m = identity_matrix(3)
sage: m.rows()
[(1, 0, 0), (0, 1, 0), (0, 0, 1)]
sage: L = m.rows()
sage: L[2:2] = [(3,3,3)]
sage: L
[(1, 0, 0), (0, 1, 0), (3, 3, 3), (0, 0, 1)]
sage: matrix(L)
[1 0 0]
[0 1 0]
[3 3 3]
[0 0 1]

There is also a columns method.

I don't actually know how efficient these procedures are, but you can test them out using %timeit, for example.