Exemplo n.º 1
0
        public static void applyMethod()
        {
            double detA = 1;
            int    n    = A.GetLength(0);

            //знаходимо U
            double[,] U = new double[n, n];
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    if (i == j)
                    {
                        double sum = 0;
                        for (int k = 1; k <= i - 1; k++)
                        {
                            sum += Math.Pow(U[k - 1, i - 1], 2);
                        }
                        U[i - 1, i - 1] = Math.Sqrt(A[i - 1, i - 1] - sum);
                        detA           *= Math.Pow(U[i - 1, i - 1], 2);
                    }
                    else if (j > i)
                    {
                        double sum = 0;
                        for (int k = 1; k <= i - 1; k++)
                        {
                            sum += U[k - 1, i - 1] * U[k - 1, j - 1];
                        }
                        U[i - 1, j - 1] = 1 / U[i - 1, i - 1] * (A[i - 1, j - 1] - sum);
                    }
                    else
                    {
                        U[i - 1, j - 1] = 0;
                    }
                }
            }

            Console.WriteLine("U");
            Auxiliary.printMatrix(U);
            Console.WriteLine("------------------------------------------");

            //знаходимо транспоновану U
            double[,] UT = Auxiliary.transponateMatrix(U);
            Console.WriteLine("Transponated U");
            Auxiliary.printMatrix(UT);
            Console.WriteLine("------------------------------------------");

            //розв'язуємо перше рівняння
            Console.WriteLine("UT * y = b");
            double[] y = new double[n];
            for (int i = 0; i < n; i++)
            {
                double sum = 0;
                for (int k = i - 1; k >= 0; k--)
                {
                    sum += y[k] * UT[i, k];
                }
                y[i] = (B[i] - sum) / UT[i, i];
            }
            Console.WriteLine("y");
            Auxiliary.printArray(y);
            Console.WriteLine("------------------------------------------");

            //розв'язуємо друге рівняння
            Console.WriteLine("U * x = y");
            double[] x = new double[n];
            for (int i = n - 1; i >= 0; i--)
            {
                double sum = 0;
                for (int k = i + 1; k < n; k++)
                {
                    sum += x[k] * U[i, k];
                }
                x[i] = (y[i] - sum) / U[i, i];
            }
            Console.WriteLine("x");
            Auxiliary.printArray(x);
            Console.WriteLine("------------------------------------------");
            Console.WriteLine("detA = " + detA);

            //шукаємо обернену матрицю -> Ut*y=ei & U*x=y
            double[,] inverseMatrix = new double[n, n];
            for (int k = 0; k < n; k++)
            {
                double[] E = new double[n];
                Array.Fill(E, 0);
                E[k] = 1;

                //y
                double[] y1 = new double[n];
                for (int i = 0; i < n; i++)
                {
                    double sum = 0;
                    for (int p = i - 1; p >= 0; p--)
                    {
                        sum += y1[p] * UT[i, p];
                    }
                    y1[i] = (E[i] - sum) / UT[i, i];
                }

                //x
                double[] x1 = new double[n];
                for (int i = n - 1; i >= 0; i--)
                {
                    double sum = 0;
                    for (int p = i + 1; p < n; p++)
                    {
                        sum += x1[p] * U[i, p];
                    }
                    x1[i] = (y1[i] - sum) / U[i, i];
                }

                for (int i = 0; i < n; i++)
                {
                    inverseMatrix[i, k] = x1[i];
                }
            }
            Console.WriteLine("\nInverse matrix");
            Auxiliary.printMatrix(inverseMatrix);
        }
Exemplo n.º 2
0
        public static void applyMethod()
        {
            double detA = 1;
            int    n    = A.GetLength(0);

            //знаходимо альфа, бета та гама
            double[] alpha = new double[n];
            double[] beta  = new double[n];
            double[] gamma = new double[n];

            alpha[0] = 0;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (i == (j + 1))
                    {
                        alpha[i] = A[i, j];
                    }
                    if (i == j)
                    {
                        beta[i] = -A[i, j];
                    }
                    if (i == (j - 1))
                    {
                        gamma[i] = A[i, j];
                    }
                }
            }
            gamma[n - 1] = 0;

            //знаходимо P та Q
            detA *= beta[0];
            double[] P = new double[n];
            double[] Q = new double[n];

            P[0] = gamma[0] / beta[0];
            Q[0] = -B[0] / beta[0];
            for (int i = 1; i < n; i++)
            {
                P[i]  = gamma[i] / (beta[i] - alpha[i] * P[i - 1]);
                detA *= beta[i] - alpha[i] * P[i - 1];
                Q[i]  = (alpha[i] * Q[i - 1] - B[i]) / (beta[i] - alpha[i] * P[i - 1]);
            }
            Console.WriteLine("P");
            Auxiliary.printArray(P);
            Console.WriteLine("------------------------------------------");
            Console.WriteLine("\nQ");
            Auxiliary.printArray(Q);
            Console.WriteLine("------------------------------------------");

            Console.WriteLine("\ndetA: " + detA);

            //знаходимо корені
            double[] roots = new double[n];
            roots[n - 1] = (alpha[n - 1] * Q[n - 2] - B[n - 1]) / (beta[n - 1] - alpha[n - 1] * P[n - 2]);
            for (int i = n - 2; i >= 0; i--)
            {
                roots[i] = (P[i] * roots[i + 1]) + Q[i];
            }

            Console.WriteLine("\nRoots");
            Auxiliary.printArray(roots);
        }
Exemplo n.º 3
0
        public static void applyMethod()
        {
            double detA = 1;
            int    n    = A.GetLength(0);

            double[,] currA = A.Clone() as double[, ];
            double[] currB = B.Clone() as double[];

            double[,] prevA = A.Clone() as double[, ];
            double[] prevB = B.Clone() as double[];

            double[] roots = new double[n];

            Auxiliary.printExtendedMatrix(currA, currB);

            for (int k = 0; k < n; k++)
            {
                if (A[k, k] == 0)
                {
                    Console.WriteLine("Something went wrong: A[{0}][{0}] = 0!", k);
                    return;
                }

                double mainEl = currA[k, k];
                detA *= mainEl;

                //k-тий рядок на кожному кроці
                for (int j = k; j < n; j++)
                {
                    currA[k, j] = (Double)prevA[k, j] / mainEl;
                }
                currB[k] = (Double)prevB[k] / mainEl;

                //інші рядки
                for (int i = k + 1; i < n; i++)
                {
                    for (int j = k; j < n; j++)
                    {
                        currA[i, j] = prevA[i, j] - (prevA[i, k] * prevA[k, j]) / mainEl;
                    }
                    currB[i] = prevB[i] - (prevB[k] * prevA[i, k]) / mainEl;
                }

                prevA = currA.Clone() as double[, ];
                prevB = currB.Clone() as double[];

                Console.WriteLine("------------------------------------------");
                Console.WriteLine("k = " + (k + 1));
                Console.WriteLine("");
                Auxiliary.printExtendedMatrix(currA, currB);
            }
            //корені
            for (int i = n - 1; i >= 0; i--)
            {
                double sum = prevB[i];
                for (int j = n - 1; j >= i + 1; j--)
                {
                    sum = sum - prevA[i, j] * roots[j];
                }
                roots[i] = sum;
            }

            Console.WriteLine("------------------------------------------");
            Console.WriteLine("Roots:");
            Auxiliary.printArray(roots);
            Console.WriteLine("\ndetA = " + detA);
            Console.WriteLine("------------------------------------------");
        }
Exemplo n.º 4
0
        public static void findInverseMatrix()
        {
            int n = A.GetLength(0);

            double[,] inverseMatrix = new double[n, n];

            for (int m = 0; m < n; m++)
            {
                double[,] currA = A.Clone() as double[, ];
                double[,] prevA = A.Clone() as double[, ];

                double[] currE = new double[n];
                Array.Fill(currE, 0);
                currE[m] = 1;
                double[] prevE = currE.Clone() as double[];

                double[] roots = new double[n];

                for (int k = 0; k < n; k++)
                {
                    if (A[k, k] == 0)
                    {
                        Console.WriteLine("Something went wrong: A[{0}][{0}] = 0!", k);
                        return;
                    }

                    double mainEl = prevA[k, k];

                    //k-тий рядок на кожному кроці
                    for (int j = k; j < n; j++)
                    {
                        currA[k, j] = (Double)prevA[k, j] / mainEl;
                    }
                    currE[k] = (Double)prevE[k] / mainEl;

                    //інші рядки
                    for (int i = k + 1; i < n; i++)
                    {
                        for (int j = k; j < n; j++)
                        {
                            currA[i, j] = prevA[i, j] - (prevA[i, k] * prevA[k, j]) / mainEl;
                        }
                        currE[i] = prevE[i] - (prevE[k] * prevA[i, k]) / mainEl;
                    }
                    prevA = currA.Clone() as double[, ];
                    prevE = currE.Clone() as double[];
                }
                //корені
                for (int i = n - 1; i >= 0; i--)
                {
                    double sum = prevE[i];
                    for (int j = n - 1; j >= i + 1; j--)
                    {
                        sum = sum - prevA[i, j] * roots[j];
                    }
                    roots[i]            = sum;
                    inverseMatrix[i, m] = sum;
                }
            }
            Console.WriteLine("Inverse matrix: \n");
            Auxiliary.printMatrix(inverseMatrix);
        }
Exemplo n.º 5
0
        public static void applyMethod()
        {
            double detA = 1;
            int    n    = A.GetLength(0);

            double[,] currA = new double[n, n];
            double[,] prevA = A.Clone() as double[, ];

            double betta = 0;
            double m     = 0;

            double[] w = new double[n];     //i
            double[,] H = new double[n, n]; //i

            double[,] Qt = new double[n, n];

            double[,] I = new double[n, n];
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (i == j)
                    {
                        I[i, j] = 1;
                    }
                    else
                    {
                        I[i, j] = 0;
                    }
                }
            }

            for (int i = 0; i < n - 1; i++)
            {
                //знаходимо betta, m та w
                double sum = 0;
                for (int k = i; k < n; k++)
                {
                    sum += Math.Pow(prevA[i, k], 2);
                }
                betta = Math.Sign(-prevA[i, i]) * Math.Sqrt(sum);
                m     = 1 / Math.Sqrt(2 * Math.Pow(betta, 2) - 2 * betta * prevA[i, i]);
                for (int j = 0; j < n; j++)
                {
                    w[j] = m * prevA[i, j];
                    if (j == i)
                    {
                        w[j] -= m * betta;
                    }
                    if (i != 0)
                    {
                        for (int k = 0; k < i; k++)
                        {
                            w[k] = 0;
                        }
                    }
                }

                //знаходимо Hi
                for (int l = 0; l < n; l++)
                {
                    for (int p = 0; p < n; p++)
                    {
                        H[l, p] = I[l, p] - 2 * w[l] * w[p];
                    }
                }

                //знаходимо Qt
                double[,] q = new double[n, n];
                if (i == 0)
                {
                    Qt = H.Clone() as double[, ];
                }
                else
                {
                    for (int l = 0; l < n; l++)
                    {
                        for (int p = 0; p < n; p++)
                        {
                            double buff = 0;
                            for (int k = 0; k < n; k++)
                            {
                                buff += Qt[l, k] * H[k, p];
                            }
                            q[l, p] = buff;
                        }
                    }
                    Qt = q.Clone() as double[, ];
                }

                //знаходимо Ai
                for (int l = 0; l < n; l++)
                {
                    for (int p = 0; p < n; p++)
                    {
                        double buff = 0;
                        for (int k = 0; k < n; k++)
                        {
                            buff += prevA[l, k] * H[k, p];
                        }
                        currA[l, p] = buff;
                    }
                }
                Console.WriteLine("i = {0}", i);

                Console.WriteLine("\nA");
                Auxiliary.printMatrix(prevA);

                prevA = currA.Clone() as double[, ];

                Console.WriteLine("\nbetta{0} = {1}", i, betta);
                Console.WriteLine("\nm{0} = {1}", i, m);
                Console.WriteLine("\nw{0}", i);
                Auxiliary.printArray(w);
                Console.WriteLine("\nH{0}", i);
                Auxiliary.printMatrix(H);
                Console.WriteLine("\nA{0}", i);
                Auxiliary.printMatrix(currA);
                Console.WriteLine("------------------------------------------\n");
            }

            //detA
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (i == j)
                    {
                        detA *= currA[i, j];
                    }
                }
            }
            Console.WriteLine("detA = " + detA);

            //знаходимо y
            double[] y = new double[n];
            y[0] = B[0] / currA[0, 0];
            for (int i = 1; i < n; i++)
            {
                double sum = 0;
                for (int k = 0; k < i; k++)
                {
                    sum += y[k] * currA[i, k];
                }
                y[i] = (B[i] - sum) / currA[i, i];
            }
            Console.WriteLine("y");
            Auxiliary.printArray(y);

            //знаходимо x
            double[] x = new double[n];
            for (int i = 0; i < n; i++)
            {
                double sum = 0;
                for (int j = 0; j < n; j++)
                {
                    sum += Qt[i, j] * y[j];
                }
                x[i] = sum;
            }

            Console.WriteLine("\nRoots:");
            Auxiliary.printArray(x);
        }