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.
Support augmented matrices in rref()
cc @siefkenj if interested (based on git blame).
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.
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]])
[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?
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):)
if not cols: cols = self.cols
for i in range(cols):
Use cols kwarg in rref instead of aug for augmented matrices
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?
Better choice example in docstring of rref
@bjodah The system you showed above is inconsistent. Is there an instance where you would want more information than "inconsistent"?
@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").
@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.
@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)
1, 0, 1 | z
0, 1, -1 | y - 2z
0, 0, 0 | x - y + 2z
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).