public void When_OrderAndFactoring2_Expect_Reference() { var solver = new RealSolver(5); solver.GetMatrixElement(1, 1).Value = 1.0; solver.GetMatrixElement(2, 1).Value = 0.0; solver.GetMatrixElement(2, 2).Value = 1.0; solver.GetMatrixElement(2, 5).Value = 0.0; solver.GetMatrixElement(3, 3).Value = 1.0; solver.GetMatrixElement(3, 4).Value = 1e-4; solver.GetMatrixElement(3, 5).Value = -1e-4; solver.GetMatrixElement(4, 4).Value = 1.0; solver.GetMatrixElement(5, 1).Value = 5.38e-23; solver.GetMatrixElement(5, 4).Value = -1e-4; solver.GetMatrixElement(5, 5).Value = 1e-4; 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 RealSolver(); solver.GetMatrixElement(1, 1).Value = 0.0001; solver.GetMatrixElement(1, 4).Value = -0.0001; solver.GetMatrixElement(1, 5).Value = 0.0; solver.GetMatrixElement(2, 1).Value = 0.0; solver.GetMatrixElement(2, 2).Value = 1.0; solver.GetMatrixElement(2, 5).Value = 0.0; solver.GetMatrixElement(3, 1).Value = -0.0001; solver.GetMatrixElement(3, 3).Value = 1.0; solver.GetMatrixElement(3, 4).Value = 0.0001; solver.GetMatrixElement(4, 4).Value = 1.0; solver.GetMatrixElement(5, 5).Value = 1.0; // Order and factor solver.OrderAndFactor(); // Compare Assert.AreEqual(solver.GetMatrixElement(1, 1).Value, 1.0e4); Assert.AreEqual(solver.GetMatrixElement(1, 4).Value, -0.0001); Assert.AreEqual(solver.GetMatrixElement(1, 5).Value, 0.0); Assert.AreEqual(solver.GetMatrixElement(2, 1).Value, 0.0); Assert.AreEqual(solver.GetMatrixElement(2, 2).Value, 1.0); Assert.AreEqual(solver.GetMatrixElement(2, 5).Value, 0.0); Assert.AreEqual(solver.GetMatrixElement(3, 1).Value, -0.0001); Assert.AreEqual(solver.GetMatrixElement(3, 3).Value, 1.0); Assert.AreEqual(solver.GetMatrixElement(3, 4).Value, 0.0001); Assert.AreEqual(solver.GetMatrixElement(4, 4).Value, 1.0); Assert.AreEqual(solver.GetMatrixElement(5, 5).Value, 1.0); }
/// <summary> /// Find a voltage driver that closes a voltage drive loop. /// </summary> /// <returns> /// The component that closes the loop. /// </returns> private Component FindVoltageDriveLoop() { // Remove the ground node and make a map for reducing the matrix complexity var index = 1; var map = new Dictionary <int, int> { { 0, 0 } }; foreach (var vd in _voltageDriven) { if (vd.Node1 != 0) { if (!map.ContainsKey(vd.Node1)) { map.Add(vd.Node1, index++); } } if (vd.Node2 != 0) { if (!map.ContainsKey(vd.Node2)) { map.Add(vd.Node2, index++); } } } // Determine the rank of the matrix var solver = new RealSolver(Math.Max(_voltageDriven.Count, map.Count)); for (var i = 0; i < _voltageDriven.Count; i++) { var pins = _voltageDriven[i]; solver.GetMatrixElement(i + 1, map[pins.Node1]).Value += 1.0; solver.GetMatrixElement(i + 1, map[pins.Node2]).Value += 1.0; } try { // Try refactoring the matrix solver.OrderAndFactor(); } catch (SingularException exception) { /* * If the rank of the matrix is lower than the number of driven nodes, then * the matrix is not solvable for those nodes. This means that there are * voltage sources driving nodes in such a way that they cannot be solved. */ if (exception.Index <= _voltageDriven.Count) { var indices = new LinearSystemIndices(exception.Index); solver.InternalToExternal(indices); return(_voltageDriven[indices.Row - 1].Source); } } return(null); }
public void When_QuickDiagonalPivoting_Expect_NoException() { // Build the solver with only the quick diagonal pivoting var solver = new RealSolver(); var strategy = (Markowitz <double>)solver.Strategy; strategy.Strategies.Clear(); strategy.Strategies.Add(new MarkowitzQuickDiagonal <double>()); // Build the matrix that should be solvable using only the singleton pivoting strategy double[][] matrix = { new[] { 1, 0.5, 0, 0 }, new[] { -0.5, 5, 4, 0 }, new[] { 0, 3, 2, 0.1 }, new[] { 0, 0, -0.01, 3 } }; double[] rhs = { 0, 0, 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.GetMatrixElement(r + 1, c + 1).Value = matrix[r][c]; } } if (!rhs[r].Equals(0.0)) { solver.GetRhsElement(r + 1).Value = rhs[r]; } } // This should run without throwing an exception solver.OrderAndFactor(); }