Ejemplo n.º 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());
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
0
        public void When_PartialSolve_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, 1] = 1;
            solver[1, 3] = 2;
            solver[1, 4] = 3;
            solver[1]    = 1;
            solver[2, 2] = 2;
            solver[2, 3] = 4;
            solver[2]    = 2;
            solver.Factor();

            // We should now be able to solve for multiple solutions, where the last two elements
            // will determine the result.
            var solution = new DenseVector <double>(4);

            solution[3] = 0;
            solution[4] = 0;
            solver.Solve(solution);
            Assert.AreEqual(1.0, solution[1], 1e-12);
            Assert.AreEqual(1.0, solution[2], 1e-12);
            solution[3] = 1.0;
            solution[4] = 2.0;
            solver.Solve(solution);
            Assert.AreEqual(-7.0, solution[1], 1e-12);
            Assert.AreEqual(-1.0, solution[2], 1e-12);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Reads a file for vectors.
        /// </summary>
        /// <param name="solver">The solver.</param>
        /// <param name="filename">The filename.</param>
        protected static void ReadRhs(SparseRealSolver solver, string filename)
        {
            using var sr = new StreamReader(filename);

            // The first line is a comment
            sr.ReadLine();

            // The second line tells us the dimensions
            var line  = sr.ReadLine() ?? throw new Exception("Invalid Mtx file");
            var match = Regex.Match(line, @"^(?<rows>\d+)\s+(\d+)");
            var size  = int.Parse(match.Groups["rows"].Value);

            // All subsequent lines are of the format [row] [column] [value]
            while (!sr.EndOfStream)
            {
                // Read the next line
                line = sr.ReadLine();
                if (line == null)
                {
                    break;
                }

                match = Regex.Match(line, @"^(?<row>\d+)\s+(?<value>.*)\s*$");
                if (!match.Success)
                {
                    throw new Exception("Could not recognize file");
                }
                var row   = int.Parse(match.Groups["row"].Value);
                var value = double.Parse(match.Groups["value"].Value, CultureInfo.InvariantCulture);

                // Set the value in the matrix
                solver.GetElement(row).Value = value;
            }
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
        public void When_Preorder_Expect_Reference()
        {
            var solver = new SparseRealSolver();

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

            SpiceSharp.Simulations.ModifiedNodalAnalysisHelper <double> .Magnitude = Math.Abs;
            solver.Precondition((matrix, vector) => SpiceSharp.Simulations.ModifiedNodalAnalysisHelper <double> .PreorderModifiedNodalAnalysis(matrix, matrix.Size));

            AssertInternal(solver, 1, 1, 1e-4);
            AssertInternal(solver, 1, 4, -1e-4);
            AssertInternal(solver, 1, 5, 0.0);
            AssertInternal(solver, 2, 1, 0.0);
            AssertInternal(solver, 2, 2, 1.0);
            AssertInternal(solver, 2, 5, 0.0);
            AssertInternal(solver, 3, 1, -1e-4);
            AssertInternal(solver, 3, 3, 1.0);
            AssertInternal(solver, 3, 4, 1e-4);
            AssertInternal(solver, 4, 4, 1.0);
            AssertInternal(solver, 5, 5, 1.0);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Creates solver used to solve equations.
        /// </summary>
        /// <returns>A solver that can be used to solve equations.</returns>
        public ISparsePivotingSolver <double> CreateSolver()
        {
            var solver = new SparseRealSolver();

            solver.Parameters.AbsolutePivotThreshold = AbsolutePivotThreshold;
            solver.Parameters.RelativePivotThreshold = RelativePivotThreshold;
            return(solver);
        }
Ejemplo n.º 9
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());
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Reads a matrix file generated by Spice 3f5.
        /// </summary>
        /// <param name="matFilename">The matrix filename.</param>
        /// <param name="vecFilename">The vector filename.</param>
        /// <returns></returns>
        protected static SparseRealSolver ReadSpice3f5File(string matFilename, string vecFilename)
        {
            var solver = new SparseRealSolver();

            // Read the spice file
            string line;

            using (var reader = new StreamReader(matFilename))
            {
                // The file is organized using (row) (column) (value) (imag value)
                while (!reader.EndOfStream && (line = reader.ReadLine()) != null)
                {
                    if (line == "first")
                    {
                        continue;
                    }

                    // Try to read an element
                    var match = Regex.Match(line, @"^(?<row>\d+)\s+(?<col>\d+)\s+(?<value>[^\s]+)(\s+[^\s]+)?$");
                    if (match.Success)
                    {
                        int row   = int.Parse(match.Groups["row"].Value);
                        int col   = int.Parse(match.Groups["col"].Value);
                        var value = double.Parse(match.Groups["value"].Value, CultureInfo.InvariantCulture);
                        solver.GetElement(new MatrixLocation(row, col)).Value = value;
                    }
                }
            }

            // Read the vector file
            using (var reader = new StreamReader(vecFilename))
            {
                var index = 1;
                while (!reader.EndOfStream && (line = reader.ReadLine()) != null)
                {
                    var value = double.Parse(line, CultureInfo.InvariantCulture);
                    solver.GetElement(index).Value = value;
                    index++;
                }
            }

            return(solver);
        }
Ejemplo n.º 11
0
        public void When_PartialDecompositionSingular_Expect_Reference()
        {
            var solver = new SparseRealSolver();

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

            solver.Degeneracy = 1;
            Assert.AreEqual(true, solver.Factor());

            AssertInternal(solver, 1, 1, 1);
            AssertInternal(solver, 2, 2, 1);
            AssertInternal(solver, 2, 3, 1);
            AssertInternal(solver, 3, 1, 1);
            AssertInternal(solver, 3, 2, 1);
            AssertInternal(solver, 3, 3, 0);
        }
Ejemplo n.º 12
0
        public void When_Factoring_Expect_Reference()
        {
            double[][] matrixElements =
            {
                new[] { 1.0, 1.0, 1.0 },
                new[] { 2.0, 3.0, 5.0 },
                new[] { 4.0, 6.0, 8.0 }
            };
            double[][] expected =
            {
                new[] { 1.0, 1.0,  1.0 },
                new[] { 2.0, 1.0,  3.0 },
                new[] { 4.0, 2.0, -0.5 }
            };

            // Create matrix
            var solver = new SparseRealSolver();

            for (var r = 0; r < matrixElements.Length; r++)
            {
                for (var c = 0; c < matrixElements[r].Length; c++)
                {
                    solver.GetElement(new MatrixLocation(r + 1, c + 1)).Value = matrixElements[r][c];
                }
            }

            // Factor
            solver.Factor();

            // Compare
            for (var r = 0; r < matrixElements.Length; r++)
            {
                for (var c = 0; c < matrixElements[r].Length; c++)
                {
                    Assert.AreEqual(expected[r][c], solver.GetElement(new MatrixLocation(r + 1, c + 1)).Value, 1e-12);
                }
            }
        }
Ejemplo n.º 13
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());
        }