numl.Math.LinearAlgebra.Matrix.SwapRow C# (CSharp) Метод

SwapRow() публичный Метод

Swap row.
public SwapRow ( int from, int to ) : void
from int Source for the.
to int to.
Результат void
        public void SwapRow(int from, int to)
        {
            Swap(from, to, VectorType.Row);
        }

Usage Example

Пример #1
0
        /// <summary>Inverses the given matrix.</summary>
        /// <exception cref="SingularMatrixException">Thrown when a Singular Matrix error condition occurs.</exception>
        /// <param name="mat">Matrix.</param>
        /// <returns>A Matrix.</returns>
        private static Matrix Inverse(Matrix mat)
        {
            // working space
            Matrix matrix = new Matrix(mat.Rows, 2 * mat.Cols);

            // copy over colums
            for (int i = 0; i < mat.Cols; i++)
            {
                matrix[i, VectorType.Col] = mat[i, VectorType.Col];
            }

            // fill in identity
            for (int i = mat.Cols; i < 2 * mat.Cols; i++)
            {
                matrix[i - mat.Cols, i] = 1;
            }

            int    maxrow;
            double c;

            for (int y = 0; y < matrix.Rows; y++)
            {
                maxrow = y;
                for (int y2 = y + 1; y2 < matrix.Rows; y2++)
                {
                    if (System.Math.Abs(matrix[y2, y]) > System.Math.Abs(matrix[maxrow, y]))
                    {
                        maxrow = y2;
                    }
                }

                // swap rows
                matrix.SwapRow(maxrow, y);

                // uh oh
                if (System.Math.Abs(matrix[y][y]) <= 0.00000000001)
                {
                    throw new SingularMatrixException("Matrix is becoming unstable!");
                }

                for (int y2 = y + 1; y2 < matrix.Rows; y2++)
                {
                    c = matrix[y2, y] / matrix[y, y];
                    for (int x = y; x < matrix.Cols; x++)
                    {
                        matrix[y2, x] -= matrix[y, x] * c;
                    }
                }
            }

            // back substitute
            for (int y = matrix.Rows - 1; y >= 0; y--)
            {
                c = matrix[y][y];
                for (int y2 = 0; y2 < y; y2++)
                {
                    for (int x = matrix.Cols - 1; x > y - 1; x--)
                    {
                        matrix[y2, x] -= matrix[y, x] * matrix[y2, y] / c;
                    }
                }

                matrix[y, y] /= c;
                for (int x = matrix.Rows; x < matrix.Cols; x++)
                {
                    matrix[y, x] /= c;
                }
            }

            // generate result
            Matrix result = new Matrix(mat.Rows);

            for (int i = mat.Cols; i < 2 * mat.Cols; i++)
            {
                result[i - mat.Cols, VectorType.Col] = matrix[i, VectorType.Col];
            }

            return(result);
        }
All Usage Examples Of numl.Math.LinearAlgebra.Matrix::SwapRow