public void TestRandomA() { RandomHelper.Random = new Random(1); for (int i = 0; i < 100; i++) { // Create A. MatrixF a = new MatrixF(3, 3); RandomHelper.Random.NextMatrixF(a, 0, 1); LUDecompositionF d = new LUDecompositionF(a); if (d.IsNumericallySingular == false) { // Check solving of linear equations. MatrixF b = new MatrixF(3, 2); RandomHelper.Random.NextMatrixF(b, 0, 1); MatrixF x = d.SolveLinearEquations(b); MatrixF b2 = a * x; Assert.IsTrue(MatrixF.AreNumericallyEqual(b, b2, 0.01f)); MatrixF aPermuted = d.L * d.U; Assert.IsTrue(MatrixF.AreNumericallyEqual(aPermuted, a.GetSubmatrix(d.PivotPermutationVector, 0, 2))); } } }
public void SolveLinearEquationsException() { MatrixF a = new MatrixF(new float[, ] { { 1, 2 }, { 5, 6 }, { 0, 1 } }); LUDecompositionF d = new LUDecompositionF(a); d.SolveLinearEquations(null); }
public void TestRandomRegularA() { RandomHelper.Random = new Random(1); for (int i = 0; i < 100; i++) { VectorF column1 = new VectorF(3); RandomHelper.Random.NextVectorF(column1, 1, 2); VectorF column2 = new VectorF(3); RandomHelper.Random.NextVectorF(column2, 1, 2); // Make linearly independent. if (column1 / column1[0] == column2 / column2[0]) { column2[0]++; } // Create linearly independent third column. VectorF column3 = column1 + column2; column3[1]++; // Create A. MatrixF a = new MatrixF(3, 3); a.SetColumn(0, column1); a.SetColumn(1, column2); a.SetColumn(2, column3); LUDecompositionF d = new LUDecompositionF(a); MatrixF aPermuted = d.L * d.U; Assert.IsTrue(MatrixF.AreNumericallyEqual(aPermuted, a.GetSubmatrix(d.PivotPermutationVector, 0, 2))); aPermuted = d.L * d.U; // Repeat with to test cached values. Assert.IsTrue(MatrixF.AreNumericallyEqual(aPermuted, a.GetSubmatrix(d.PivotPermutationVector, 0, 2))); Assert.AreEqual(false, d.IsNumericallySingular); // Check solving of linear equations. MatrixF b = new MatrixF(3, 2); RandomHelper.Random.NextMatrixF(b, 0, 1); MatrixF x = d.SolveLinearEquations(b); MatrixF b2 = a * x; Assert.IsTrue(MatrixF.AreNumericallyEqual(b, b2, 0.01f)); } }
public void TestRandomRegularA() { RandomHelper.Random = new Random(1); for (int i = 0; i < 100; i++) { VectorF column1 = new VectorF(3); RandomHelper.Random.NextVectorF(column1, 1, 2); VectorF column2 = new VectorF(3); RandomHelper.Random.NextVectorF(column2, 1, 2); // Make linearly independent. if (column1 / column1[0] == column2 / column2[0]) column2[0]++; // Create linearly independent third column. VectorF column3 = column1 + column2; column3[1]++; // Create A. MatrixF a = new MatrixF(3, 3); a.SetColumn(0, column1); a.SetColumn(1, column2); a.SetColumn(2, column3); LUDecompositionF d = new LUDecompositionF(a); MatrixF aPermuted = d.L * d.U; Assert.IsTrue(MatrixF.AreNumericallyEqual(aPermuted, a.GetSubmatrix(d.PivotPermutationVector, 0, 2))); aPermuted = d.L * d.U; // Repeat with to test cached values. Assert.IsTrue(MatrixF.AreNumericallyEqual(aPermuted, a.GetSubmatrix(d.PivotPermutationVector, 0, 2))); Assert.AreEqual(false, d.IsNumericallySingular); // Check solving of linear equations. MatrixF b = new MatrixF(3, 2); RandomHelper.Random.NextMatrixF(b, 0, 1); MatrixF x = d.SolveLinearEquations(b); MatrixF b2 = a * x; Assert.IsTrue(MatrixF.AreNumericallyEqual(b, b2, 0.01f)); } }
public void SolveLinearEquationsException() { MatrixF a = new MatrixF(new float[,] { { 1, 2 }, { 5, 6 }, { 0, 1 } }); LUDecompositionF d = new LUDecompositionF(a); d.SolveLinearEquations(null); }