예제 #1
0
        public static MyMatrix gaussSeidel(MyMatrix A, MyMatrix B)
        {
            int n = A.rowCount;

            if (A.rowCount != B.rowCount || A.rowCount != A.columnCount)
            {
                throw new Exception("Matrix A must be n*n! A rowCount must be equal B rowCount!");
            }
            MyMatrix X   = new MyMatrix(n, 1);
            double   sum = 0.0;

            while (true)
            {
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < i; j++)
                    {
                        sum -= A[i, j] * X[j, 0];
                    }
                    for (int j = i + 1; j < n; j++)
                    {
                        sum -= A[i, j] * X[j, 0];
                    }
                    if (A[i, i] == 0.0)
                    {
                        int row = findBiggestRowInColumn(A, i);
                        swapRows(A, i, row);
                        swapRows(B, i, row);
                    }
                    X[i, 0] = (B[i, 0] + sum) / A[i, i];
                    sum     = 0.0;
                }
                double norm1 = (B - (A * X)).countNorm();
                double norm2 = B.countNorm();
                if (norm1 / norm2 < epsilon)
                {
                    break;
                }
            }
            return(X);
        }
예제 #2
0
        public static MyMatrix gaussIterative(MyMatrix A, MyMatrix B, int version, out int saver)
        {
            int n = A.rowCount;

            if (A.rowCount != B.rowCount || A.rowCount != A.columnCount)
            {
                throw new Exception("Matrix A must be n*n! A rowCount must be equal B rowCount!");
            }
            if (version == 1)
            { //JACOBI
                MyMatrix X_NEW = new MyMatrix(n, 1);
                MyMatrix X_OLD = new MyMatrix(n, 1);
                double   sum   = 0.0;
                int      iter  = 0;
                while (true)
                {
                    for (int i = 0; i < n; i++)
                    {
                        for (int j = 0; j < n; j++)
                        {
                            if (j != i)
                            {
                                sum -= A[i, j] * X_OLD[j, 0];
                            }
                        }
                        if (A[i, i] == 0.0)
                        {
                            int row = findBiggestRowInColumn(A, i);
                            swapRows(A, i, row);
                            swapRows(B, i, row);
                        }
                        X_NEW[i, 0] = (B[i, 0] + sum) / A[i, i];
                        sum         = 0.0;
                    }
                    double norm1 = (B - (A * X_NEW)).countNorm();
                    double norm2 = B.countNorm();
                    if ((norm1 / norm2) < epsilon)
                    {
                        break;
                    }
                    for (int i = 0; i < n; i++)
                    {
                        X_OLD[i, 0] = X_NEW[i, 0];
                    }
                    iter++;
                }
                saver = iter;
                return(X_NEW);
            }
            else if (version == 2)
            { //SEIDEL
                MyMatrix X    = new MyMatrix(n, 1);
                double   sum  = 0.0;
                int      iter = 0;
                while (true)
                {
                    for (int i = 0; i < n; i++)
                    {
                        for (int j = 0; j < i; j++)
                        {
                            sum -= A[i, j] * X[j, 0];
                        }
                        for (int j = i + 1; j < n; j++)
                        {
                            sum -= A[i, j] * X[j, 0];
                        }
                        if (A[i, i] == 0.0)
                        {
                            int row = findBiggestRowInColumn(A, i);
                            swapRows(A, i, row);
                            swapRows(B, i, row);
                        }
                        X[i, 0] = (B[i, 0] + sum) / A[i, i];
                        sum     = 0.0;
                    }
                    double norm1 = (B - (A * X)).countNorm();
                    double norm2 = B.countNorm();
                    if (norm1 / norm2 < epsilon)
                    {
                        break;
                    }
                    iter++;
                }
                saver = iter;
                return(X);
            }
            else
            {
                throw new Exception("Unknown Gauss elimination version! 1 - base, 2 - partial, 3 - full");
            }
        }