Why isn't there a "Gaussian Elimination" function in languages such as Julia or NumPy/Python?

Viewed 276

Why isn't there a Gaussian Elimination function in packages such as NumPy or Julia?

  • I know about LU decomposition
  • I know about the \ (backslash) operator

I still think it would be nice to perform Gaussian Elimination. For example, the following matrix:

[
4 -1 1 0
1  1 0 1
]
1 Answers

We can solve it with the backslash by:

[
4 -1
1  1
] \ [
1 0
0 1
]

The result will be the right two columns of the RREFed matrix (the left two columns will be the 2x2 identity, but the result of this code won't show the left two columns).

The general rule is for an m x n matrix, put m columns on the left side of the backslash, and the remaining n - m columns on the right. So if we wanted to perform Gaussian Elimination on the following matrix:

[
1 2 7 0 0 0
2 8 0 1 0 0
2 4 1 0 1 0
2 2 3 0 0 1
]

We would do

A = [
1 2 7 0
2 8 0 1
2 4 1 0
2 2 3 0
]
B = [
0 0
0 0
1 0
0 1
]
x = A \ B

And just as before, it will give us the resulting rightmost n - m columns of the RREFed matrix, and you assume the left m (which it does not show) are the m x m identity matrix.

Lastly, if we want to check how good our answer is (i.e. determine if it's exact or least squares), we can now run

norm(A * x - B)

to see how close we are to the 0 matrix.

Related