Exemplo n.º 1
0
            public IMatrix Solve(IMatrix b)
            {
                if (b.Rows != _lu.Rows) throw new ArgumentException("Invalid matrix dimensions.");
                if (!IsNonSingular) throw new InvalidOperationException("Matrix is singular");

                // Copy right hand side with pivoting
                int count = b.Columns;
                IMatrix x = b.Submatrix(_pivotVector, 0, count - 1);

                //int rows = LU.Rows;
                int columns = _lu.Columns;
                double[][] lu = _lu.Array;

                // Solve L*Y = B(piv,:)
                for (int k = 0; k < columns; k++)
                {
                    for (int i = k + 1; i < columns; i++)
                    {
                        for (int j = 0; j < count; j++)
                        {
                            x[i, j] -= x[k, j]*lu[i][k];
                        }
                    }
                }

                // Solve U*X = Y;
                for (int k = columns - 1; k >= 0; k--)
                {
                    for (int j = 0; j < count; j++)
                    {
                        x[k, j] /= lu[k][k];
                    }

                    for (int i = 0; i < k; i++)
                    {
                        for (int j = 0; j < count; j++)
                        {
                            x[i, j] -= x[k, j]*lu[i][k];
                        }
                    }
                }

                return x;
            }