コード例 #1
0
        public void When_SingletonPivoting_Expect_NoException()
        {
            // Build the solver with only the singleton pivoting
            var solver = new SparseRealSolver();

            solver.Parameters.Strategies.Clear();
            solver.Parameters.Strategies.Add(new MarkowitzSingleton <double>());

            // Build the matrix that should be solvable using only the singleton pivoting strategy
            double[][] matrix =
            {
                new double[] { 0, 0, 1, 0 },
                new double[] { 1, 1, 1, 1 },
                new double[] { 0, 0, 0, 1 },
                new double[] { 1, 0, 0, 0 }
            };
            double[] rhs = { 0, 1, 0, 0 };
            for (var r = 0; r < matrix.Length; r++)
            {
                for (var c = 0; c < matrix[r].Length; c++)
                {
                    if (!matrix[r][c].Equals(0.0))
                    {
                        solver.GetElement(new MatrixLocation(r + 1, c + 1)).Value = matrix[r][c];
                    }
                }
                if (!rhs[r].Equals(0.0))
                {
                    solver.GetElement(r + 1).Value = rhs[r];
                }
            }

            // This should run without throwing an exception
            Assert.AreEqual(solver.Size, solver.OrderAndFactor());
        }
コード例 #2
0
        public void When_PartialDecomposition_Expect_Reference()
        {
            var solver = new SparseRealSolver
            {
                PivotSearchReduction = 2, // Limit to only the 2 first elements
                Degeneracy           = 2  // Only perform elimination on the first two rows
            };

            solver[1, 2] = 2;
            solver[2, 1] = 1;
            solver[1, 3] = 4;
            solver[4, 2] = 4;
            solver[3, 3] = 2;
            solver[3, 4] = 4;
            solver[4, 4] = 1;

            Assert.AreEqual(2, solver.OrderAndFactor());

            // We are testing two things here:
            // - First, the solver should not have chosen a pivot in the lower-right submatrix
            // - Second, the submatrix should be equal to A_cc - A_c1 * A^-1 * A_1c with A the top-left
            //   matrix, A_cc the bottom-right submatrix, A_1c and A_c1 the off-diagonal matrices
            Assert.AreEqual(2.0, solver[3, 3], 1e-12);
            Assert.AreEqual(4.0, solver[3, 4], 1e-12);
            Assert.AreEqual(-8.0, solver[4, 3], 1e-12);
            Assert.AreEqual(1.0, solver[4, 4], 1e-12);
        }
コード例 #3
0
        public void When_OrderAndFactoring2_Expect_Reference()
        {
            var solver = new SparseRealSolver();

            solver.GetElement(new MatrixLocation(1, 1)).Value = 1.0;
            solver.GetElement(new MatrixLocation(2, 1)).Value = 0.0;
            solver.GetElement(new MatrixLocation(2, 2)).Value = 1.0;
            solver.GetElement(new MatrixLocation(2, 5)).Value = 0.0;
            solver.GetElement(new MatrixLocation(3, 3)).Value = 1.0;
            solver.GetElement(new MatrixLocation(3, 4)).Value = 1e-4;
            solver.GetElement(new MatrixLocation(3, 5)).Value = -1e-4;
            solver.GetElement(new MatrixLocation(4, 4)).Value = 1.0;
            solver.GetElement(new MatrixLocation(5, 1)).Value = 5.38e-23;
            solver.GetElement(new MatrixLocation(5, 4)).Value = -1e-4;
            solver.GetElement(new MatrixLocation(5, 5)).Value = 1e-4;

            Assert.AreEqual(5, solver.OrderAndFactor());

            AssertInternal(solver, 1, 1, 1.0);
            AssertInternal(solver, 2, 1, 0.0);
            AssertInternal(solver, 2, 2, 1.0);
            AssertInternal(solver, 2, 5, 0.0);
            AssertInternal(solver, 3, 3, 1.0);
            AssertInternal(solver, 3, 4, 1e-4);
            AssertInternal(solver, 3, 5, -1e-4);
            AssertInternal(solver, 4, 4, 1.0);
            AssertInternal(solver, 5, 1, 5.38e-23);
            AssertInternal(solver, 5, 4, -1e-4);
            AssertInternal(solver, 5, 5, 10000);
        }
コード例 #4
0
        public void When_OrderAndFactoring_Expect_Reference()
        {
            var solver = new SparseRealSolver();

            solver.GetElement(new MatrixLocation(1, 1)).Value = 0.0001;
            solver.GetElement(new MatrixLocation(1, 4)).Value = -0.0001;
            solver.GetElement(new MatrixLocation(1, 5)).Value = 0.0;
            solver.GetElement(new MatrixLocation(2, 1)).Value = 0.0;
            solver.GetElement(new MatrixLocation(2, 2)).Value = 1.0;
            solver.GetElement(new MatrixLocation(2, 5)).Value = 0.0;
            solver.GetElement(new MatrixLocation(3, 1)).Value = -0.0001;
            solver.GetElement(new MatrixLocation(3, 3)).Value = 1.0;
            solver.GetElement(new MatrixLocation(3, 4)).Value = 0.0001;
            solver.GetElement(new MatrixLocation(4, 4)).Value = 1.0;
            solver.GetElement(new MatrixLocation(5, 5)).Value = 1.0;

            // Order and factor
            Assert.AreEqual(5, solver.OrderAndFactor());

            // Compare
            Assert.AreEqual(solver.GetElement(new MatrixLocation(1, 1)).Value, 1.0e4);
            Assert.AreEqual(solver.GetElement(new MatrixLocation(1, 4)).Value, -0.0001);
            Assert.AreEqual(solver.GetElement(new MatrixLocation(1, 5)).Value, 0.0);
            Assert.AreEqual(solver.GetElement(new MatrixLocation(2, 1)).Value, 0.0);
            Assert.AreEqual(solver.GetElement(new MatrixLocation(2, 2)).Value, 1.0);
            Assert.AreEqual(solver.GetElement(new MatrixLocation(2, 5)).Value, 0.0);
            Assert.AreEqual(solver.GetElement(new MatrixLocation(3, 1)).Value, -0.0001);
            Assert.AreEqual(solver.GetElement(new MatrixLocation(3, 3)).Value, 1.0);
            Assert.AreEqual(solver.GetElement(new MatrixLocation(3, 4)).Value, 0.0001);
            Assert.AreEqual(solver.GetElement(new MatrixLocation(4, 4)).Value, 1.0);
            Assert.AreEqual(solver.GetElement(new MatrixLocation(5, 5)).Value, 1.0);
        }
コード例 #5
0
        public void When_BigMatrix_Expect_NoException()
        {
            // Test factoring a big matrix
            var solver = new SparseRealSolver();

            ReadMatrix(solver, Path.Combine(TestContext.CurrentContext.TestDirectory, Path.Combine("Algebra", "Matrices", "fidapm05")));

            // Order and factor this larger matrix
            Assert.AreEqual(solver.Size, solver.OrderAndFactor());
        }
コード例 #6
0
        public void When_EntireMatrixPivoting_Expect_NoException()
        {
            // Build the solver with only the quick diagonal pivoting
            var solver   = new SparseRealSolver();
            var strategy = solver.Parameters;

            strategy.Strategies.Clear();
            strategy.Strategies.Add(new MarkowitzEntireMatrix <double>());

            // Build the matrix that should be solvable using only the singleton pivoting strategy
            double[][] matrix =
            {
                new[]        { 1, 0.5,     0, 2 },
                new double[] { 2,   5,     4, 3 },
                new double[] { 0,   3,     2, 0 },
                new[]        { 4, 1.8, -0.01, 8 }
            };
            double[] rhs = { 1, 2, 3, 4 };
            for (var r = 0; r < matrix.Length; r++)
            {
                for (var c = 0; c < matrix[r].Length; c++)
                {
                    if (!matrix[r][c].Equals(0.0))
                    {
                        solver.GetElement(new MatrixLocation(r + 1, c + 1)).Value = matrix[r][c];
                    }
                }
                if (!rhs[r].Equals(0.0))
                {
                    solver.GetElement(r + 1).Value = rhs[r];
                }
            }

            // This should run without throwing an exception
            Assert.AreEqual(solver.Size, solver.OrderAndFactor());
        }