Esempio 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());
        }
Esempio n. 2
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;
            }
        }
Esempio n. 3
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);
        }
        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);
                }
            }
        }
Esempio n. 5
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());
        }
        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);
        }
        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);
        }
        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);
        }