Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a standard solution for Gauss elimination in Python?

Is there somewhere in the cosmos of scipy/numpy/... a standard method for Gauss-elimination of a matrix?

One finds many snippets via google, but I would prefer to use "trusted" modules if possible.

like image 744
flonk Avatar asked Mar 26 '13 13:03

flonk


People also ask

What is solution of Gaussian elimination?

The solution of this system is therefore (x, y) = (2, 1), as noted in Example 1. Gaussian elimination is usually carried out using matrices. This method reduces the effort in finding the solutions by eliminating the need to explicitly write the variables at each step. The previous example will be redone using matrices.

What is the limitation of Gauss elimination method?

Answer: The gaussian elimination method may produce inaccurate results when the terms in the augumented matrix are rounded off. ... When you convert the system of equations into matrix form, you might want to round off the co-efficients to say 2 significant digits (0.1445 would be rounded off to 0.14).

What are the rules of Gauss elimination method?

(1) Write the given system of linear equations in matrix form AX = B, where A is the coefficient matrix, X is a column matrix of unknowns and B is the column matrix of the constants. (2) Reduce the augmented matrix [A : B] by elementary row operations to get [A' : B']. (3) We get A' as an upper triangular matrix.


2 Answers

I finally found, that it can be done using LU decomposition. Here the U matrix represents the reduced form of the linear system.

from numpy import array from scipy.linalg import lu  a = array([[2.,4.,4.,4.],[1.,2.,3.,3.],[1.,2.,2.,2.],[1.,4.,3.,4.]])  pl, u = lu(a, permute_l=True) 

Then u reads

array([[ 2.,  4.,  4.,  4.],        [ 0.,  2.,  1.,  2.],        [ 0.,  0.,  1.,  1.],        [ 0.,  0.,  0.,  0.]]) 

Depending on the solvability of the system this matrix has an upper triangular or trapezoidal structure. In the above case a line of zeros arises, as the matrix has only rank 3.

like image 199
flonk Avatar answered Sep 20 '22 17:09

flonk


One function that can be worth checking is _remove_redundancy, if you wish to remove repeated or redundant equations:

import numpy as np import scipy.optimize  a = np.array([[1.,1.,1.,1.],               [0.,0.,0.,1.],               [0.,0.,0.,2.],               [0.,0.,0.,3.]]) print(scipy.optimize._remove_redundancy._remove_redundancy(a, np.zeros_like(a[:, 0]))[0]) 

which gives:

[[1. 1. 1. 1.]  [0. 0. 0. 3.]] 

As a note to @flonk answer, using a LU decomposition might not always give the desired reduced row matrix. Example:

import numpy as np import scipy.linalg  a = np.array([[1.,1.,1.,1.],               [0.,0.,0.,1.],               [0.,0.,0.,2.],               [0.,0.,0.,3.]])  _,_, u = scipy.linalg.lu(a) print(u) 

gives the same matrix:

[[1. 1. 1. 1.]  [0. 0. 0. 1.]  [0. 0. 0. 2.]  [0. 0. 0. 3.]] 

even though the last 3 rows are linearly dependent.

like image 41
Bernardo Costa Avatar answered Sep 21 '22 17:09

Bernardo Costa