nalgebra icon indicating copy to clipboard operation
nalgebra copied to clipboard

Native Gauss-Jordan Elimination

Open sciguyryan opened this issue 2 years ago • 3 comments

Hi.

Just a quick question. Is there a native implementation of the Gauss-Jordan elimination method (or something similar)? I'm working on a chemical equation balancer, and I'm using RREF (via Gauss-Jordan elimination) as a stage in that process.

I went through the effort of writing my own Matrix implementation before I was pointed in this direction... and yours is far better so I'm going to swap it out. Fortunately it was not a waste of time since I learned a lot by implementing it.

If anyone could point me in the right direction, that would be fantastic.

sciguyryan avatar Jul 01 '22 13:07 sciguyryan

There certainly is no implementation of one in nalgebra. In numerical linear algebra, Gauss elimination is usually supplanted by the LU decomposition (which is in fact Gaussian elimination but we build the matrix factors in the process).

To my knowledge, (reduced) row echelon form is something that is very rarely done numerically (rather it's mostly used as a teaching tool, for doing computations by hand). Are you sure you really need RREF? If you do, I suppose there is no implementation in nalgebra. If there is a different way to solve your problem, then perhaps there is something you can use.

Andlon avatar Jul 01 '22 15:07 Andlon

Are you sure you really need RREF? If you do, I suppose there is no implementation in nalgebra. If there is a different way to solve your problem, then perhaps there is something you can use.

Possibly not, I'm using the method I am familiar with, but there are likely others that can work too. I'll try to summarize that I would typically do here.

Take all of the terms on the right and left hand side and tokenize them. For each unique symbol (H for Hydrogen, He for Helium and so on.) count the number of instances of each of those symbols. Put that into a matrix. For example, with the following equation:

Ca(OH)₂ + H₃PO₄ ->Ca₃(PO₄)₂ + H₂0

The unique tokens are Ca, O, H and P, indexed 0 to 3 respectively.

From that we can build a matrix of the number of instances of each symbol (terms from the right-hand side must be negative), as follows:

{ [ 1, 2, 2, 0], [0, 4, 3, 1], [-3, -8, 0, -2], [0, -1, -2, 0] }

From there I would use RREF to reduce the matrix, which allows it to be solved (by computing the null-space of the matrix, that is how I was taught to do it). In this specific example the end result would be something like this: { [1/2], [1/3], [1/6], [1] }. Identifying the LCM will tell tell you the coefficients for each of the components of the chemical equation.

There might be another way to achieve that result, but I don't know what that would be. If you have any ideas, I would love to hear them. I'm always up for learning new ways to solve problems.

sciguyryan avatar Jul 01 '22 16:07 sciguyryan

There certainly is no implementation of one in nalgebra. In numerical linear algebra, Gauss elimination is usually supplanted by the LU decomposition (which is in fact Gaussian elimination but we build the matrix factors in the process).

To my knowledge, (reduced) row echelon form is something that is very rarely done numerically (rather it's mostly used as a teaching tool, for doing computations by hand). Are you sure you really need RREF? If you do, I suppose there is no implementation in nalgebra. If there is a different way to solve your problem, then perhaps there is something you can use.

Actually the RREF is useful numerically. For example it is used for finding the columns that form a basis for the span of a rectangular matrix. It's very useful in geometry applications that need to find bases of sub-spaces.

Makogan avatar Aug 15 '23 18:08 Makogan