/// <summary>
        /// Solves system of linear equations using LU/LUP decomposition, forward and backward substitution.
        /// </summary>
        /// <param name="A">Values on the left side of system of linear equations which are multiplied by unknown vector x.</param>
        /// <param name="b">Vector containing values from the right side of system of linear equations.</param>
        /// <param name="epsilon">Constant used for checking if we are dividing value by zero.</param>
        /// <returns>Vector x (result of solving system of linear equations).</returns>

        public static Matrica SolveSystemOfLinearEquations(Matrica A, Matrica b, double epsilon)
        {
            Console.WriteLine("Matrica A:");
            Console.WriteLine("============");
            A.WriteMatrixInConsole();
            Console.WriteLine();

            Console.WriteLine("Vektor b:");
            Console.WriteLine("===========");
            b.WriteMatrixInConsole();
            Console.WriteLine();

            Matrica copyOfA = new Matrica();

            copyOfA.Equals(A);
            Matrica copyOfb = new Matrica();

            copyOfb.Equals(b);

            try
            {
                A.LUDecomposition(epsilon);

                Console.WriteLine("Matrica L:");
                Console.WriteLine("============");
                Matrica L = A.GetLMatrixFromLU();
                L.WriteMatrixInConsole();
                Console.WriteLine();

                Console.WriteLine("Matrica U:");
                Console.WriteLine("============");
                Matrica U = A.GetUMatrixFromLU();
                U.WriteMatrixInConsole();
                Console.WriteLine();

                Matrica y = ForwardSubstitution(A, b);

                Console.WriteLine("Vektor y:");
                Console.WriteLine("===========");
                y.WriteMatrixInConsole();
                Console.WriteLine();

                Matrica x = BackwardSubstitution(A, y, epsilon);

                Console.WriteLine("Vektor x:");
                Console.WriteLine("===========");
                x.WriteMatrixInConsole();
                Console.WriteLine();

                return(x);
            }
            catch (DivideByZeroException)
            {
                Console.WriteLine("Nije moguće riješiti ovaj sustav LU dekompozicijom. Izvođenje LUP dekompozicije...");
                A = copyOfA;
                b = copyOfb;
                Matrica P = A.LUPDecomposition(epsilon, b);
                Console.WriteLine("Matrica P:");
                Console.WriteLine("============");
                P.WriteMatrixInConsole();
                Console.WriteLine();

                Console.WriteLine("Matrica L:");
                Console.WriteLine("============");
                Matrica L = A.GetLMatrixFromLU();
                L.WriteMatrixInConsole();
                Console.WriteLine();

                Console.WriteLine("Matrica U:");
                Console.WriteLine("============");
                Matrica U = A.GetUMatrixFromLU();
                U.WriteMatrixInConsole();
                Console.WriteLine();

                Matrica y = ForwardSubstitution(A, b);

                Console.WriteLine("Vektor y:");
                Console.WriteLine("===========");
                y.WriteMatrixInConsole();
                Console.WriteLine();

                Matrica x = BackwardSubstitution(A, y, epsilon);

                Console.WriteLine("Vektor x:");
                Console.WriteLine("===========");
                x.WriteMatrixInConsole();
                Console.WriteLine();

                return(x);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Solves system of linear equations using LU/LUP decomposition, forward and backward substitution.
        /// </summary>
        /// <param name="A">Values on the left side of system of linear equations which are multiplied by unknown vector x.</param>
        /// <param name="b">Vector containing values from the right side of system of linear equations.</param>
        /// <param name="epsilon">Constant used for checking if we are dividing value by zero.</param>
        /// <returns>Vector x (result of solving system of linear equations).</returns>

        public static Matrica SolveSystemOfLinearEquations(Matrica A, Matrica b, double epsilon)
        {
            Console.WriteLine("Matrix A:");
            Console.WriteLine("============");
            A.WriteMatrixInConsole();
            Console.WriteLine();

            Console.WriteLine("Vector b:");
            Console.WriteLine("===========");
            b.WriteMatrixInConsole();
            Console.WriteLine();

            Matrica copyOfA = new Matrica();

            copyOfA.Equals(A);
            Matrica copyOfb = new Matrica();

            copyOfb.Equals(b);

            try
            {
                A.LUDecomposition(epsilon);

                Console.WriteLine("Matrix L:");
                Console.WriteLine("============");
                Matrica L = A.GetLMatrixFromLU();
                L.WriteMatrixInConsole();
                Console.WriteLine();

                Console.WriteLine("Matrix U:");
                Console.WriteLine("============");
                Matrica U = A.GetUMatrixFromLU();
                U.WriteMatrixInConsole();
                Console.WriteLine();

                Matrica y = ForwardSubstitution(A, b);

                Console.WriteLine("Vector y:");
                Console.WriteLine("===========");
                y.WriteMatrixInConsole();
                Console.WriteLine();

                Matrica x = BackwardSubstitution(A, y, epsilon);

                Console.WriteLine("Vector x:");
                Console.WriteLine("===========");
                x.WriteMatrixInConsole();
                Console.WriteLine();

                return(x);
            }
            catch (DivideByZeroException)
            {
                Console.WriteLine("It is not possible to solve this system using LU decomposition. Executing LUP decomposition...");
                A = copyOfA;
                b = copyOfb;
                Matrica P = A.LUPDecomposition(epsilon, b);
                Console.WriteLine("Matrix P:");
                Console.WriteLine("============");
                P.WriteMatrixInConsole();
                Console.WriteLine();

                Console.WriteLine("Matrix L:");
                Console.WriteLine("============");
                Matrica L = A.GetLMatrixFromLU();
                L.WriteMatrixInConsole();
                Console.WriteLine();

                Console.WriteLine("Matrix U:");
                Console.WriteLine("============");
                Matrica U = A.GetUMatrixFromLU();
                U.WriteMatrixInConsole();
                Console.WriteLine();

                Matrica y = ForwardSubstitution(A, b);

                Console.WriteLine("Vector y:");
                Console.WriteLine("===========");
                y.WriteMatrixInConsole();
                Console.WriteLine();

                Matrica x = BackwardSubstitution(A, y, epsilon);

                Console.WriteLine("Vector x:");
                Console.WriteLine("===========");
                x.WriteMatrixInConsole();
                Console.WriteLine();

                return(x);
            }
        }