## Description

We can use sage to perform elementary row operations on a given matrix, say

(1)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)
A
```

#### Adding and Subtracting Rows

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]])
A.add_multiple_of_row(1, 0, 2)
A
```

#### Code

```
A = matrix(QQ, [[1, 2, 3], [4, 5, 6]])
A.add_multiple_of_row(1, 0, -1)
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)
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.

#### Scaling Rows

#### Code

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

#### Adding and Subtracting Rows

#### Code

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

#### Code

```
A = matrix(QQ, [[1, 2, 3], [4, 5, 6]])
B = A.with_added_multiple_of_row(1, 0, -1)
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

CC:

Primary Tags: Linear algebra: Matrices.

Secondary Tags: Matrices: Row operations.

## Related Cells

- Matrices in Sage. Matrices in Sage.

## Attribute

Permalink:

Author: R. Beezer

Date: 24 Feb 2020 15:23

Submitted by: Zane Corbiere