# Support augmented matrices in rref() #12035

Closed
wants to merge 3 commits into
from
+61 −2

None yet

### 3 participants

Member
commented Jan 10, 2017
 This is a way to allow rref to handle augmented matrices without introducing any backward incompatible changes. cf. https://en.wikipedia.org/wiki/Row_echelon_form#Systems_of_linear_equations and https://en.wikipedia.org/wiki/Augmented_matrix#Solution_of_a_linear_system Let me know what you think.
 bjodah `Support augmented matrices in rref()` `cdc7fc2`
Member
commented Jan 10, 2017
 cc @siefkenj if interested (based on git blame).
Contributor
commented Jan 11, 2017 edited
 Interesting. In general I am not in favor of adding unnecessary complexity to the math algorithms. Is there a reason you don't augment the matrix with `row_stack`/`col_stack`, row reduce, and then extract the augmented part? This would avoid the need to change the rref algorithm at all. Augmented matrices seem like they might be better served by a subclass of `MutableMatrix`. That is, you subclass `MutableMatrix` to `AugmentedMatrix` which is aware of which part is the main matrix and which part is the augmented part. Then you could do something like `AugmentedMatrix(...).rref().get_parts()` or some such.
Member
commented Jan 11, 2017
 The `rref` algortihm did not play well with an extra column, here is the matrix from my new test: ``````>>> from sympy import Matrix, symbols >>> x, y, z = symbols('x y z') >>> A = Matrix([[-1, 0, 1, x], [1, 2, 1, y], [1, 1, 0, z]]) >>> A.rref() (Matrix([ [1, 0, -1, 0], [0, 1, 1, 0], [0, 0, 0, 1]]), [0, 1, 3]) `````` as you can see the symbols are lost, perhaps it should be considered a bug in `rref`? An augmented matrix class could very well be useful, but perhaps it should handle more things than `rref` then?
Member
commented Jan 11, 2017
 It could also be possible to have a single augmented input matrix together with an optional keyword `cols` showing the extent of the main part of the matrix. (`if not cols: cols = self.cols`, `for i in range(cols):`)
 bjodah `Use cols kwarg in rref instead of aug for augmented matrices` `987c610`
Member
commented Jan 11, 2017
 It seems like `rref` in master branch currently handles an extra symbolic column in the augmented matrix when the original matrix has full rank (since the loop exits before reaching the augmented part), but not if the original matrix is rank deficient. The `cols` kwargs might be needed for this situtation?
 bjodah `Better choice example in docstring of rref` `9bee67e`
Contributor
 @bjodah The system you showed above is inconsistent. Is there an instance where you would want more information than "inconsistent"?
Member
commented Jan 11, 2017
 @siefkenj, are we talking about: ``````A = Matrix([[-1, 0, 1, x], [1, 2, 1, y], [1, 1, 0, z]]) `````` where the last column is part of the augmented matrix (and not the 3x3 matrix describing the system)? Then I would argue that it the third row is simply a linear combination of the above rows (row3 = ½row2 - ½row1) and hence the system is simply underdetermined. I'm writing a library that will deal with under determined systems all the time (with varying degree of "underdeterminedness").
Contributor
 @bjodah to get your expected behavior, you can augment with the identity, row reduce, extract the right hand side, and then multiply your "augmented" column `[x,y,z]` by what you extracted. This will avoid over-reducing but also won't affect the algorithm.
Member
commented Jan 12, 2017
 @siefkenj thank you for pointing this out. I didn't think about that, I realize I need to tackle my problem differently. This also made me realize that the result of rref on an augmented matrix corresponding to a under determined (although consistent) system like mine has not one, but multiple solutions: ``````1, 0, 1 | (y - x)/2 0, 1, -1 | x 0, 0, 0 | ½(x - y + 2z) `````` and ``````1, 0, 1 | z 0, 1, -1 | y - 2z 0, 0, 0 | x - y + 2z `````` and ``````1, 0, 1 | z 0, 1, -1 | x 0, 0, 0 | -(x - y + 2z) `````` which is natural since the system has an infinite number of solutions. I was simply hoping rref would make the system canonical, but I guess this is not possible (unless one define some extra criterion for canonicalization).
closed this Jan 12, 2017