示例#1
0
        public static double Det(double[][] L, double[][] U, int n)
        {
            double detL  = MatrixUtils.DeterminantSqM(L, n);
            double detU  = MatrixUtils.DeterminantSqM(U, n);
            double detLU = detL * detU;

            return(detLU);
        }
示例#2
0
        static void Main(string[] args)
        {
            Random    rnd = new Random();
            const int n   = 10;

            // Conditions.
            double[][] A = new double[n][];
            A[0] = new double[] { 208, 90, -46, 5, -79, -116, -41, 45, -67, 94 };
            A[1] = new double[] { 90, 185, -40, 120, -1, 27, 18, 48, -31, 38 };
            A[2] = new double[] { -46, -40, 235, 15, 22, -47, -92, -46, -2, -149 };
            A[3] = new double[] { 5, 120, 15, 190, 13, 111, 39, 21, -32, -37 };
            A[4] = new double[] { -79, -1, 22, 13, 176, 62, 24, -36, 128, 43 };
            A[5] = new double[] { -116, 27, -47, 111, 62, 186, 94, 4, 21, -16 };
            A[6] = new double[] { -41, 18, -92, 39, 24, 94, 194, 154, 123, 110 };
            A[7] = new double[] { 45, 48, -46, 21, -36, 4, 154, 249, 130, 127 };
            A[8] = new double[] { -67, -31, -2, -32, 128, 21, 123, 130, 233, 117 };
            A[9] = new double[] { 94, 38, -149, -37, 43, -16, 110, 127, 117, 246 };

            double[] B = new double[] { -234, 55, 140, -163, -88, -40, -197, -99, 30, 250 };

            // Buffers.
            double[][] Inv = new double[n][];
            double[][] R   = new double[n][];
            double[][] L   = new double[n][];
            double[][] Lt  = new double[n][];
            double[]   X   = new double[n];

            for (int i = 0; i < n; i++)
            {
                Inv[i] = new double[n];
                R[i]   = new double[n];
                L[i]   = new double[n];
                Lt[i]  = new double[n];
                X[i]   = 0;
                for (int j = 0; j < n; j++)
                {
                    Inv[i][j] = 0;
                    R[i][j]   = 0;
                    L[i][j]   = 0;
                    Lt[i][j]  = 0;
                }
            }

            Console.WriteLine("A matrix: ");
            MatrixUtils.ShowSqM(A, n);

            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();
            Console.WriteLine("Solving A using Choletsky.\n");
            Choletsky.CholetskyL(A, L, n);
            stopWatch.Stop();

            Console.WriteLine("Resulting L matrix:");
            MatrixUtils.ShowSqM(L, n);

            Console.WriteLine("transposed l matrix");
            MatrixUtils.CopySqM(Lt, L, n);
            MatrixUtils.TransposeSqM(Lt, n);
            MatrixUtils.ShowSqM(Lt, n);

            Console.WriteLine("Check for Choletsky L matrix: A=L*L^t");
            MatrixUtils.MultiplySqM(L, Lt, R, n);
            MatrixUtils.ShowSqM(R, n);

            Console.WriteLine("Get the roots using Choletsky method.");
            Choletsky.Solve(L, Lt, B, X, n);

            Console.WriteLine("Roots are:");
            MatrixUtils.ShowVector(X, n);

            double det = Choletsky.Det(L, n);

            Console.WriteLine("Determinant is: {0}\n", det);

            Stopwatch stopWatch2 = new Stopwatch();

            stopWatch2.Start();
            Console.WriteLine("Inverting using Choletsky method.");
            Choletsky.Inv(L, Lt, Inv, n);
            stopWatch2.Stop();
            Console.WriteLine("Inverted matrix:");
            MatrixUtils.ShowSqM(Inv, n);

            Console.WriteLine("Check for the inverted matrix:");
            MatrixUtils.MultiplySqM(A, Inv, R, n);
            MatrixUtils.ShowSqM(R, n);

            Console.WriteLine("Aglorithm time: {0}", stopWatch.Elapsed);
            Console.WriteLine("Inverting matrix time: {0}", stopWatch2.Elapsed);

            Console.ReadKey();
        }
示例#3
0
        public static void GaussElimination(double[][] A, double[] X, int n)
        {
            int swappingFlag = 0;

            // Pivoting.
            for (int i = 0; i < n; i++)
            {
                for (int k = i + 1; k < n; k++)
                {
                    if (Math.Abs(A[i][i]) < Math.Abs(A[k][i]))
                    {
                        swappingFlag++;
                        for (int j = 0; j <= n; j++)
                        {
                            double temp = A[i][j];
                            A[i][j] = A[k][j];
                            A[k][j] = temp;
                        }
                    }
                }
            }

            Console.WriteLine("Matrix after pivoting:");
            MatrixUtils.ShowM(A, n, n + 1);

            // Elimination.
            for (int i = 0; i < n - 1; i++)
            {
                for (int k = i + 1; k < n; k++)
                {
                    double t = A[k][i] / A[i][i];
                    for (int j = 0; j <= n; j++)
                    {
                        //make the elements below the pivot elements equal to zero or elimnate the variables.
                        A[k][j] = A[k][j] - t * A[i][j];
                    }
                }
            }

            Console.WriteLine("Matrix after elimination:");
            MatrixUtils.ShowM(A, n, n + 1);

            // Check if some of the arguments on the main diagonal equals to 0.
            for (int i = 0; i < n; i++)
            {
                if (A[i][i] == 0)
                {
                    Console.WriteLine("Noticed FREE argument with index {0}, starting from 0", i);
                }
            }

            // Check if at least one B[i] after elimination does not equal to 0.
            bool matrixHasSolution = false;

            for (int i = 0; i < n; i++)
            {
                if (A[i][n] != 0)
                {
                    matrixHasSolution = true;
                }
            }
            if (!matrixHasSolution)
            {
                Console.WriteLine("Matrix A does not have solutions.");
                Console.WriteLine("Reason: after elimination there are no B[i], which does not equal to 0");
                throw new ArgumentException();
            }
            // Check if there are MORE AMOUNT OF VARIABLES THAN EQUATIONS.
            for (int i = 0; i < n; i++)
            { /* TODO */
            }

            // Back-substitution.
            for (int i = n - 1; i >= 0; i--)
            {
                X[i] = A[i][n];
                for (int j = n - 1; j > i; j--)
                {
                    X[i] -= A[i][j] * X[j];
                }
                X[i] = X[i] / A[i][i];
            }

            // Determinant of a matrix.
            double det = 1;

            for (int i = 0; i < n; i++)
            {
                det *= A[i][i];
            }
            det = (swappingFlag % 2 == 0) ? det : -det;
            Console.WriteLine("Determinant is: {0}\n", det);
        }
示例#4
0
        public static void Inv(double[][] A, int n)
        {
            // Init n*2 matrix to invert.
            double[][] Inv = new double[n][];
            for (int i = 0; i < n; i++)
            {
                Inv[i] = new double[n * 2];
                for (int j = 0; j < n * 2; j++)
                {
                    Inv[i][j] = 0;
                }
            }

            // LEFT SIDE: Fill up Inv matrix with values from A.
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    Inv[i][j] = A[i][j];
                }
            }

            // RIGHT SIDE: Full up with ones on the main diagonal.
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n * 2; j++)
                {
                    if (j == (i + n))
                    {
                        Inv[i][j] = 1;
                    }
                }
            }

            Console.WriteLine("Inverting, using such matrix:");
            MatrixUtils.ShowM(Inv, n, n * 2);

            // Pivoting.
            for (int i = n - 1; i >= 1; i--)
            {
                // if (Inv[i - 1][0] < Inv[i][0])
                if (Math.Abs(Inv[i - 1][0]) < Math.Abs(Inv[i][0]))
                {
                    for (int j = 0; j < n * 2; j++)
                    {
                        double d = Inv[i][j];
                        Inv[i][j]     = Inv[i - 1][j];
                        Inv[i - 1][j] = d;
                    }
                }
            }

            Console.WriteLine("Matrix after pivoting:");
            MatrixUtils.ShowM(Inv, n, n * 2);

            // Reducing.
            for (int j = 0; j < n; j++)
            {
                for (int i = 0; i < n; i++)
                {
                    if (i != j)
                    {
                        double d = Inv[i][j];
                        for (int k = 0; k < 2 * n; k++)
                        {
                            Inv[i][k] -= (Inv[j][k] / Inv[j][j]) * d;
                        }
                    }
                    else
                    {
                        double d = Inv[i][j];
                        for (int k = 0; k < 2 * n; k++)
                        {
                            Inv[i][k] /= d;
                        }
                    }
                }
            }

            Console.WriteLine("Matrix after reducing:");
            MatrixUtils.ShowM(Inv, n, n * 2);

            Console.WriteLine("The inversed matrix is on the right side.");
        }
示例#5
0
        public static void Solve(double[][] A, double[] B, double[] X, int n)
        {
            double[] Tau   = new double[n];
            double[] Delta = new double[n];
            double[] Lmbd  = new double[n];
            for (int i = 0; i < n; i++)
            {
                Tau[i]   = 0;
                Delta[i] = 0;
                Lmbd[i]  = 0;
            }

            Console.WriteLine("Solving A matrix using triagonal algorithm.");
            MatrixUtils.ShowSqM(A, n);

            // The algorithm.
            // Note, that in ditriagonal method there are specific first and last iterations.

            // First iteration.
            Tau[0]   = A[0][0];
            Delta[0] = (-1) * A[0][1] / Tau[0];
            Lmbd[0]  = B[0] / Tau[0];

            // Other iterations.
            for (int i = 1; i < n - 1; i++)
            {
                Tau[i]   = A[i][i] + A[i][i - 1] * Delta[i - 1];
                Delta[i] = (-1) * A[i][i + 1] / Tau[i];
                Lmbd[i]  = (B[i] - A[i][i - 1] * Lmbd[i - 1]) / Tau[i];
            }

            // Last iteration.
            Tau[n - 1]   = A[n - 1][n - 1] + A[n - 1][n - 2] * Delta[n - 2];
            Delta[n - 1] = 0;
            Lmbd[n - 1]  = (B[n - 1] - A[n - 1][n - 2] * Lmbd[n - 2]) / Tau[n - 1];

            // Check for the coef znamennik.
            for (int i = 0; i < n; i++)
            {
                if (Tau[i] == 0)
                {
                    Console.WriteLine("TDMA is incorrect, because one of the znamennik equals to 0");
                }
            }

            Console.WriteLine("Coefficients:");
            MatrixUtils.ShowVector(Tau, n);
            Console.WriteLine("Alpha:");
            MatrixUtils.ShowVector(Delta, n);
            Console.WriteLine("Betha:");
            MatrixUtils.ShowVector(Lmbd, n);

            // Solving for X.
            X[n - 1] = Lmbd[n - 1];          // last element.
            for (int i = n - 2; i >= 0; i--) // others.
            {
                X[i] = Delta[i] * X[i + 1] + Lmbd[i];
            }


            double det = Det(Tau, n);

            Console.WriteLine("Determinant for the A matrix is: {0}\n", det);

            //// The algorithm.
            //for (int i = 1; i < n; i++)
            //{
            //    double m = Ad[i - 1] / Bd[i - 1];
            //    Bd[i] -= m * Cd[i - 1];
            //    B[i] -= m * B[i - 1];
            //}
            //// solve for the last x:
            //X[n - 1] = B[n - 1] / Bd[n - 1];
            //// remaining x, using back substitution:
            //for (int i = n - 2; i >= 0; i--)
            //    X[i] = (B[i] - Cd[i] * X[i + 1]) / Bd[i];
        }