diff --git a/Nerd_STF/Mathematics/Algebra/Matrix.cs b/Nerd_STF/Mathematics/Algebra/Matrix.cs index 8ea5e0f..f97ad07 100644 --- a/Nerd_STF/Mathematics/Algebra/Matrix.cs +++ b/Nerd_STF/Mathematics/Algebra/Matrix.cs @@ -1,6 +1,4 @@ -using System.Buffers; - -namespace Nerd_STF.Mathematics.Algebra; +namespace Nerd_STF.Mathematics.Algebra; public class Matrix : IMatrix { @@ -319,6 +317,27 @@ public class Matrix : IMatrix SetRow(rowB, dataA); } + public Matrix SolveRowEchelon() + { + Matrix result = (Matrix)MemberwiseClone(); + + // Scale the first row so the first element of that row is 1. + result.ScaleRowMutable(0, 1 / result[0, 0]); + + // For each row afterwards, subtract the required amount from all rows before it and normalize. + for (int r1 = 1; r1 < result.Size.x; r1++) + { + int min = Mathf.Min(r1, result.Size.y); + for (int r2 = 0; r2 < min; r2++) + { + result.AddRowMutable(r1, r2, -result[r1, r2]); + } + result.ScaleRowMutable(r1, 1 / result[r1, r1]); + } + + return result; + } + public override bool Equals([NotNullWhen(true)] object? obj) { if (obj == null) return base.Equals(obj);