Пример #1
0
        public void IterationsAlgoritmSystemIsWrong()
        {
            // Arrange
            double[]       firstEquationCoeffs     = new double[] { 4, -3, 2, -1 };
            double         firstEquationFreeMember = 8;
            LinearEquation equation1 = new LinearEquation(firstEquationCoeffs, firstEquationFreeMember);

            double[]       secondEquationCoeffs     = new double[] { 3, -2, 1, -3 };
            double         secondEquationFreeMember = 7;
            LinearEquation equation2 = new LinearEquation(secondEquationCoeffs, secondEquationFreeMember);

            double[]       thirdEquationCoeffs     = new double[] { 5, -3, 1, -8 };
            double         thirdEquationFreeMember = 1;
            LinearEquation equation3 = new LinearEquation(thirdEquationCoeffs, thirdEquationFreeMember);

            LinearEquation[]     equations      = new LinearEquation[] { equation1, equation2, equation3 };
            LinearEquationSystem equationSystem = new LinearEquationSystem(equations);

            double[] answer = new double[equations.Length];

            bool isExceptionThrown = false;

            try
            {
                answer = EquationSolver.Iterations(equationSystem);
            }
            catch (Exception)
            {
                isExceptionThrown = true;
            }
            finally
            {
                Assert.AreEqual(isExceptionThrown, true);
            }
        }
Пример #2
0
        public static void Main(string[] args)
        {
            double[,] a =
            {
                { 1.0, 2.0,  4.0 },
                { 3.0, 4.0,  9.0 },
                { 5.0, 1.0, 12.0 },
            };

            var A = Matrix.Build.DenseOfArray(a);

            double[] b =
            {
                1.0, 2.0, 3.0
            };

            var B = Vector.Build.DenseOfArray(b);

            A.SwapRows(1, 2);

            var system = new LinearEquationSystem(A, B);

            //var solution = system.SolveGauss(TimeSpan.FromMilliseconds(500));

            //foreach (var row in solution.Matrix.EnumerateRows())
            //{
            //    Console.WriteLine(string.Join(", ", row));
            //}

            //foreach (var row in solution.Vector)
            //{
            //    Console.WriteLine(string.Join(", ", row));
            //}
        }
Пример #3
0
        public void CanNotSolveLinearEquationSystemClearly()
        {
            // 0x = 0 - infinite solutions
            var equation = new LinearEquation(new[] { 0.0 }, 0);
            var results  = new LinearEquationSystem(equation).Solve(); // This will throw an exception

            Assert.AreEqual(0, results[0]);
        }
        public void ArgumentExceptionTest()
        {
            double[,] A = { { 0, 0 }, { 0, 0 } };
            double[]             B      = { 0, 0 };
            LinearEquationSystem system = new LinearEquationSystem(A, B);

            Assert.Throws <ArgumentException>(() => system.Solve());
        }
        public void FormatExceptionTest()
        {
            double[,] A = new double[, ] {
                { 10, 2, 3, 4 }, { 2, 4, 5, 6 }, { -5, 7, 8, 9 }, { 6, 1, 4, 3 }, { 2, 0, 5, 6 }
            };
            double[]             B = new double[] { 1, 3, 6, 9 };
            LinearEquationSystem system;

            Assert.Throws <FormatException>(() => system = new LinearEquationSystem(A, B));
        }
        public void ThreedimensionalArrayTest()
        {
            double[,] A = { { 2, 1, -1 }, { 3, 2, 2 }, { 1, 0, 1 } };
            double[]             B        = { 3, -7, -2 };
            double[]             expected = { 1, -2, -3 };
            LinearEquationSystem system   = new LinearEquationSystem(A, B);
            var actual = system.Solve();

            CollectionAssert.AreEqual(expected, actual);
        }
        public void TwodimensionalArrayTest()
        {
            double[,] A = { { 1, 2 }, { 1, 3 } };
            double[]             B        = { 2, 1 };
            double[]             expected = { 4, -1 };
            LinearEquationSystem system   = new LinearEquationSystem(A, B);
            var actual = system.Solve();

            CollectionAssert.AreEqual(expected, actual);
        }
Пример #8
0
        public void CanSolveSimpleLinearEquationSystem()
        {
            // 2x = 1
            var equation = new LinearEquation
            {
                Coefficients = new[] { 2.0 },
                Result       = 1
            };
            var linearEquationSystem = new LinearEquationSystem(equation);

            Assert.AreEqual(0.5, linearEquationSystem.Solve()[0]);
        }
Пример #9
0
        public void CanSolveLinearEquationSystem()
        {
            // x - y + 2z = 6
            var firstEquation = new LinearEquation(new[] { 1.0, -1.0, 2.0 }, 6);
            // 2x + 3y + 2z = 11
            var secondEquation = new LinearEquation(new[] { 2.0, 3.0, 2.0 }, 11);
            // 3x + 2y + z = 8
            var thirdEquation = new LinearEquation(new[] { 3.0, 2.0, 1.0 }, 8);

            var linearEquationSystem = new LinearEquationSystem(firstEquation, secondEquation, thirdEquation);

            double[] results = linearEquationSystem.Solve();
            Assert.AreEqual(1, results[0]);
            Assert.AreEqual(1, results[1]);
            Assert.AreEqual(3, results[2]);
        }
        public static LinearEquationSystem AddRow(this LinearEquationSystem system)
        {
            var rowCount = system.Matrix.RowCount;
            var vector   = Enumerable.Repeat(0.0, rowCount + 1);

            //Add Column
            system.Matrix = system.Matrix.InsertColumn(system.Matrix.ColumnCount, Vector.Build.DenseOfEnumerable(vector));

            //Add Row
            system.Matrix = system.Matrix.InsertRow(system.Matrix.ColumnCount, Vector.Build.DenseOfEnumerable(vector));

            //Add Row
            system.Vector = Vector.Build.DenseOfEnumerable(system.Vector.Append(0.0));

            return(system);
        }
Пример #11
0
        public void IterationsAlgoritmTest()
        {
            // Arrange
            double[] firstEquationCoeffs     = new double[] { 2, -3 };
            double   firstEquationFreeMember = 5;

            double[] secondEquationCoeffs     = new double[] { 1, -2 };
            double   secondEquationFreeMember = 3;

            LinearEquation equation1 = new LinearEquation(firstEquationCoeffs, firstEquationFreeMember);
            LinearEquation equation2 = new LinearEquation(secondEquationCoeffs, secondEquationFreeMember);

            LinearEquationSystem equationSystem = new LinearEquationSystem(new LinearEquation[] { equation1, equation2 });

            // Expect
            double[] expectedAnswer = new double[] { 1, -1 };

            // Actual
            double[] actualAnswer = EquationSolver.Iterations(equationSystem);

            Assert.AreEqual(expectedAnswer[0], actualAnswer[0], 0.01);
            Assert.AreEqual(expectedAnswer[1], actualAnswer[1], 0.01);
        }
Пример #12
0
        public static double[] Iterations(LinearEquationSystem system)
        {
            IterationsAlgoritm iterations = new IterationsAlgoritm(system.Matrix, system.RightPart);

            return(iterations.XVector);
        }
Пример #13
0
        public static double[] Gauss(LinearEquationSystem system)
        {
            GaussAlgoritm gauss = new GaussAlgoritm(system.Matrix, system.RightPart);

            return(gauss.XVector);
        }
        public static async Task <LinearEquationSystem> SolveGauss(this LinearEquationSystem system, TimeSpan delayPerStep, Action onPropertyChanged = null)
        {
            var rows = system.Matrix.RowCount;
            var cols = system.Matrix.ColumnCount;

            //Für jede Reihe, betrachte Diagonalwert
            for (int diag = 0; diag < rows; diag++)
            {
                //Suche Reihe mit höchstem Wert in der Spalte, die den Diagonalwert enthält
                int    highestValueRow = diag;
                double highestValue    = Math.Abs(system.Matrix[diag, diag]);

                double d = 0.0;

                for (int row = diag + 1; row < rows; row++)
                {
                    if (Math.Abs(system.Matrix[row, diag]) > highestValue)
                    {
                        highestValue    = Math.Abs(system.Matrix[row, diag]);
                        highestValueRow = row;
                    }
                }

                //Tausche Reihen, sodass Reihe mit höchstem Wert jetzt in der Reihe der aktuellen Diagonale ist.
                system.Matrix = system.Matrix.SwapRows(diag, highestValueRow);
                system.Vector = system.Vector.SwapRows(diag, highestValueRow);

                //*Haltepunkt* -> Reihen werden vertauscht
                await WaitAndRefreshDisplay();

                //Teile jeden Wert in der Reihe durch den Diagonalwert, sodass eine 1 entsteht
                double divider = 1 / system.Matrix[diag, diag];
                for (int col = 0; col < cols; col++)
                {
                    system.Matrix[diag, col] *= divider;
                    //*Haltepunkt* -> Jeder Wert der Reihe wird durch Diagonalwert geteilt
                    await WaitAndRefreshDisplay();
                }
                system.Vector[diag] *= divider;

                //Ziehe diese Reihe von jeder Folgenden so oft ab, dass in der Spalte eine 0 entsteht
                for (int row = 0; row < rows; row++)
                {
                    d = system.Matrix[row, diag];
                    if (row != diag)
                    {
                        for (int col = diag; col < cols; col++)
                        {
                            system.Matrix[row, col] -= d * system.Matrix[diag, col];
                        }
                        system.Vector[row] -= d * system.Vector[diag];

                        //*Haltepunkt* -> Nullen werden erzeugt
                        await WaitAndRefreshDisplay();
                    }
                }
            }

            return(system);

            //Aktualisiere Matrix in User-Oberfläche und warte kurz
            async Task WaitAndRefreshDisplay()
            {
                await Task.Delay(delayPerStep);

                onPropertyChanged?.Invoke();
            }
        }
        public static double[][] GetArrays(this LinearEquationSystem system)
        {
            var matrix = system.Matrix.InsertColumn(system.Matrix.ColumnCount, system.Vector);

            return(matrix.AsRowArrays());
        }