numl.Math.LinearAlgebra.Matrix.Pivot C# (CSharp) Method

Pivot() public static method

Pivots the given m.
Thrown when the requested operation is invalid.
public static Pivot ( Matrix M ) : Matrix
M Matrix The Matrix to process.
return Matrix
        public static Matrix Pivot(Matrix M)
        {
            if (M.Rows != M.Cols)
                throw new InvalidOperationException("Factorization requires a symmetric positive semidefinite matrix!");

            var m = M.Rows;
            var P = Identity(m);
            Tuple<int, double> row = new Tuple<int, double>(0, 0);
            for (int j = 0; j < m; j++)
            {
                row = new Tuple<int, double>(j, 0);
                for (int i = j; i < m; i++)
                    if (row.Item2 < System.Math.Abs(M[i, j]))
                        row = new Tuple<int, double>(i, System.Math.Abs(M[i, j]));

                if (row.Item1 != j)
                    P.SwapRow(j, row.Item1);
            }

            return P;
        }