Task
Given a matrix, your program/function should output a row-equivalent matrix in checkerboard form ( \$A_{ij}=0\$ if and only if \$i+j\$ is odd).
Two matrices are defined to be row-equivalent if and only if one can be obtained from the other by a sequence of elementary row operations (EROs), where each ERO consists of performing one of the following moves:
- Swapping two rows
- Multiplying one row by a nonzero rational constant
- Adding a rational multiple of one row to another row
Since there are multiple possible outputs for each input, please include a way to verify that the output is row-equivalent to the input, or explain enough of your algorithm for it to be clear that the output is valid.
Example
Input:
2 4 6 8
0 2 0 4
1 2 5 4
Subtracting row 2 from row 3 yields
2 4 6 8
0 2 0 4
1 0 5 0
Subtracting double row 2 from row 1 yields
2 0 6 0
0 2 0 4
1 0 5 0
That is one possible output. Another possible matrix output is
1 0 3 0
0 1 0 2
1 0 4 0,
which is also row-equivalent to the given matrix and is also in checkerboard form.
Constraints
- The given matrix will have at least as many columns as rows and contain only integers (your output may use rational numbers, but this is not strictly necessary since you can multiply by a constant to obtain only integers in the output).
- You may assume that the rows of the matrix are linearly independent
- You may assume that it is possible to express the given matrix in checkerboard form
Input and output may be in any reasonable format that unambiguously represents an m×n
matrix.
Sample Test Cases
Each input is followed by a possible output.
1 2 3
4 5 5
6 5 4
1 0 1
0 1 0
1 0 2
1 2 3
4 5 5
2 0 -1
1 0 1
0 1 0
1 0 2
2 4 6 8
0 2 0 4
1 2 5 4
1 0 3 0
0 1 0 2
1 0 4 0
1 2 3 2 5
6 7 6 7 6
1 2 1 2 1
1 0 1 0 1
0 1 0 1 0
1 0 2 0 3
3 2 1 10 4 18
24 31 72 31 60 19
6 8 18 9 15 7
8 4 8 20 13 36
3 0 1 0 4 0
0 1 0 5 0 9
2 0 6 0 5 0
0 8 0 9 0 7
3 2 1 10 4 18
24 31 72 31 60 19
0 4 16 -11 7 -29
8 4 8 20 13 36
3 0 1 0 4 0
0 1 0 5 0 9
2 0 6 0 5 0
0 8 0 9 0 7
1 0 0 0 -2
0 1 0 1 0
0 0 1 0 2
3 0 1 0 -4
0 2 0 2 0
5 0 3 0 -4
Related: