Sage RO Row Operations

Sage will perform individual row operations on a matrix. This can get a bit tedious, but it is better than doing the computations (wrong, perhaps) by hand, and it can be useful when building up more complicated procedures for a matrix.

For each row operation, there are two similar methods. One changes the matrix “in-place” while the other creates a new matrix that is a modified version of the original. This is an important distinction that you should understand for every new Sage command you learn that might change a matrix or vector.

Consider the first row operation, which swaps two rows. There are two matrix methods to do this, a “with” version that will create a new, changed matrix, which you will likely want to save, and a plain version that will change the matrix it operates on “in-place.”. The copy() function, which is a general-purpose command, is a way to make a copy of a matrix before you make changes to it. Study the example below carefully, and then read the explanation following. (Remember that counting begins with zero.)









Here is how each of these four matrices comes into existence.

  1. A is our original matrix, created from a list of entries.
  2. B is not a new copy of A, it is just a new name for referencing the exact same matrix internally.
  3. C is a brand new matrix, stored internally separate from A, but with identical contents.
  4. D is also a new matrix, which is created by swapping the rows of A

And here is how each matrix is affected by the commands.

  1. A is changed twice “in-place”. First, its rows are swapped rows a “plain” matrix method. Then its entry in the lower-right corner is set to -6.
  2. B is just another name for A. So whatever changes are made to A will be evident when we ask for the matrix by the name B. And vice-versa.
  3. C is a copy of the original A and does not change, since no subsequent commands act on C.
  4. D is a new copy of A, created by swapping the rows of A. Once created from A, it has a life of its own, as illustrated by the change in its entry in the upper-left corner to -1.

An interesting experiment is to rearrange some of the lines above (or add new ones) and predict the result.

Just as with the first row operation, swapping rows, Sage supports the other two row operations with natural sounding commands, with both “in-place” versions and new-matrix versions.









Notice that the order of the arguments might feel a bit odd, compared to how we write and think about row operations. Also note how the “with” versions leave a trail of matrices for each step while the plain versions just keep changing A.