Matrix Row Operations

## Description

We can use sage to perform elementary row operations on a given matrix. This is a bit tedious, so it may be best reserved for large matrices where the calculations are more difficult by hand.

## Sage Cell

#### Scaling Rows

We can multiply a row by a scalar with the .rescale_row() method. Note that the rows of a matrix start with row 0.

#### Code

``````A = matrix(QQ, [[1, 2, 3], [4, 5, 6]])
A.rescale_row(0, 2)
print(A)```
```

We can add and subtract rows with the .add_multiple_of_row() method. The method takes in 3 arguments: the row being modified comes first, then the row you are adding or subtracting to it, then a scalar multiple for the second row.

#### Code

``````A = matrix(QQ, [[1, 2, 3], [4, 5, 6]])
print(A)```
```

#### Code

``````A = matrix(QQ, [[1, 2, 3], [4, 5, 6]])
print(A)```
```

#### Swapping Rows

We can swap rows with the .swap_rows() method.

#### Code

``````A = matrix(QQ, [[1, 2, 3], [4, 5, 6]])
A.swap_rows(0, 1)
print(A)```
```

## Options

#### Creating New Matrices from the Row Operations

Each of the commands above has a 'with' version, which returns the matrix we're operating on and allows us to assign it to a new variable, rather than simply changing the original matrix. This is useful because it allows us to leave a trail showing the row operations we made.

#### Code

``````A = matrix(QQ, [[1, 2, 3], [4, 5, 6]])
B = A.with_rescaled_row(0, 2)
print('A', A)
print('B', B)```
```

#### Code

``````A = matrix(QQ, [[1, 2, 3], [4, 5, 6]])
print('A', A)
print('B', B)```
```

#### Code

``````A = matrix(QQ, [[1, 2, 3], [4, 5, 6]])
print('A', A)
print('B', B)```
```

#### Swapping Rows

We can swap rows with the .swap_rows() method.

#### Code

``````A = matrix(QQ, [[1, 2, 3], [4, 5, 6]])
B = A.with_swapped_rows(0, 1)
print('A', A)
print('B', B)```
```

## Tags

Primary Tags:

Secondary Tags:

A list of possible tags can be found at The WeBWorK Open Problem Library. For linear algebra tags see the Curated Courses Project.

## Related Cells

Any related cells go here. Provide a link to the page containing the information about the cell.