public void TestWarmStarting() { MatrixF A = new MatrixF(new float[, ] { { -21, 2, -4, 0 }, { 2, 3, 0.1f, -1 }, { 2, 10, 111.1f, -11 }, { 23, 112, 111.1f, -143 } }); VectorF b = new VectorF(new float[] { 20, 28, -12, 0.1f }); SorMethodF solver = new SorMethodF(); VectorF x = solver.Solve(A, null, b); int fullIterationCount = solver.NumberOfIterations; VectorF solution = MatrixF.SolveLinearEquations(A, b); Assert.IsTrue(VectorF.AreNumericallyEqual(solution, x)); // Now test make separate solve calls with warm-starting solver.MaxNumberOfIterations = 3; x = solver.Solve(A, null, b); Assert.AreEqual(3, solver.NumberOfIterations); solver.MaxNumberOfIterations = 100; x = solver.Solve(A, x, b); Assert.AreEqual(fullIterationCount, solver.NumberOfIterations + 3); }
public void Test6() { MatrixF A = new MatrixF(new float[, ] { { -21, 2, -4, 0 }, { 2, 3, 0.1f, -1 }, { 2, 10, 111.1f, -11 }, { 23, 112, 111.1f, -143 } }); VectorF b = new VectorF(new float[] { 20, 28, -12, 0.1f }); SorMethodF solver = new SorMethodF(); solver.MaxNumberOfIterations = 4; VectorF x = solver.Solve(A, null, b); VectorF solution = MatrixF.SolveLinearEquations(A, b); Assert.IsFalse(VectorF.AreNumericallyEqual(solution, x)); Assert.AreEqual(4, solver.NumberOfIterations); // Compare with Gauss-Seidel. Must be equal. GaussSeidelMethodF gsSolver = new GaussSeidelMethodF(); gsSolver.MaxNumberOfIterations = 4; VectorF gsSolution = gsSolver.Solve(A, null, b); Assert.IsTrue(VectorF.AreNumericallyEqual(gsSolution, x)); }
public void Test() { // Make a random list. RandomHelper.Random = new Random(77); List <VectorF> points = new List <VectorF>(); for (int i = 0; i < 10; i++) { var vector = new VectorF(4); RandomHelper.Random.NextVectorF(vector, -1, 10); points.Add(vector); } PrincipalComponentAnalysisF pca = new PrincipalComponentAnalysisF(points); Assert.Greater(pca.Variances[0], pca.Variances[1]); Assert.Greater(pca.Variances[1], pca.Variances[2]); Assert.Greater(pca.Variances[2], pca.Variances[3]); Assert.Greater(pca.Variances[3], 0); Assert.IsTrue(pca.V.GetColumn(0).IsNumericallyNormalized); Assert.IsTrue(pca.V.GetColumn(1).IsNumericallyNormalized); Assert.IsTrue(pca.V.GetColumn(2).IsNumericallyNormalized); Assert.IsTrue(pca.V.GetColumn(3).IsNumericallyNormalized); // Compute covariance matrix and check if it is diagonal in the transformed space. MatrixF cov = StatisticsHelper.ComputeCovarianceMatrix(points); MatrixF transformedCov = pca.V.Transposed * cov * pca.V; for (int row = 0; row < transformedCov.NumberOfRows; row++) { for (int column = 0; column < transformedCov.NumberOfColumns; column++) { if (row != column) { Assert.IsTrue(Numeric.IsZero(transformedCov[row, column])); } } } // The principal components must be Eigenvectors which means that multiplying with the covariance // matrix does only change the length! VectorF v0 = pca.V.GetColumn(0); VectorF v0Result = cov * v0; Assert.IsTrue(VectorF.AreNumericallyEqual(v0.Normalized, v0Result.Normalized)); VectorF v1 = pca.V.GetColumn(1); VectorF v1Result = cov * v1; Assert.IsTrue(VectorF.AreNumericallyEqual(v1.Normalized, v1Result.Normalized)); VectorF v2 = pca.V.GetColumn(2); VectorF v2Result = cov * v2; Assert.IsTrue(VectorF.AreNumericallyEqual(v2.Normalized, v2Result.Normalized)); VectorF v3 = pca.V.GetColumn(3); VectorF v3Result = cov * v3; Assert.IsTrue(VectorF.AreNumericallyEqual(v3.Normalized, v3Result.Normalized)); }
/// <summary> /// Solves the specified linear system of equations <i>Ax=b</i>. /// </summary> /// <param name="matrixA">The matrix A.</param> /// <param name="initialX"> /// The initial guess for x. If this value is <see langword="null"/>, a zero vector will be used /// as initial guess. /// </param> /// <param name="vectorB">The vector b.</param> /// <returns>The solution vector x.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="matrixA"/> is <see langword="null"/>. /// </exception> /// <exception cref="ArgumentNullException"> /// <paramref name="vectorB"/> is <see langword="null"/>. /// </exception> /// <exception cref="ArgumentException"> /// <paramref name="matrixA"/> is not a square matrix. /// </exception> /// <exception cref="ArgumentException"> /// The number of elements of <paramref name="initialX"/> does not match. /// </exception> public override VectorF Solve(MatrixF matrixA, VectorF initialX, VectorF vectorB) { NumberOfIterations = 0; if (matrixA == null) { throw new ArgumentNullException("matrixA"); } if (vectorB == null) { throw new ArgumentNullException("vectorB"); } if (matrixA.IsSquare == false) { throw new ArgumentException("Matrix A must be a square matrix.", "matrixA"); } if (matrixA.NumberOfRows != vectorB.NumberOfElements) { throw new ArgumentException("The number of rows of A and b do not match."); } if (initialX != null && initialX.NumberOfElements != vectorB.NumberOfElements) { throw new ArgumentException("The number of elements of the initial guess for x and b do not match."); } VectorF xOld = initialX ?? new VectorF(vectorB.NumberOfElements); VectorF xNew = new VectorF(vectorB.NumberOfElements); bool isConverged = false; // Make iterations until max iteration count or the result has converged. for (int i = 0; i < MaxNumberOfIterations && !isConverged; i++) { for (int j = 0; j < vectorB.NumberOfElements; j++) { float delta = 0; for (int k = 0; k < j; k++) { delta += matrixA[j, k] * xNew[k]; } for (int k = j + 1; k < vectorB.NumberOfElements; k++) { delta += matrixA[j, k] * xOld[k]; } delta = (vectorB[j] - delta) / matrixA[j, j]; xNew[j] = xOld[j] + RelaxationFactor * (delta - xOld[j]); } // Test convergence isConverged = VectorF.AreNumericallyEqual(xOld, xNew, Epsilon); xOld = xNew.Clone(); NumberOfIterations = i + 1; } return(xNew); }
public void Test1() { MatrixF A = new MatrixF(new float[, ] { { 4 } }); VectorF b = new VectorF(new float[] { 20 }); SorMethodF solver = new SorMethodF(); VectorF x = solver.Solve(A, null, b); Assert.IsTrue(VectorF.AreNumericallyEqual(new VectorF(1, 5), x)); Assert.AreEqual(2, solver.NumberOfIterations); }
public void SolveWithDefaultInitialGuess() { MatrixF A = new MatrixF(new float[, ] { { 4 } }); VectorF b = new VectorF(new float[] { 20 }); JacobiMethodF solver = new JacobiMethodF(); VectorF x = solver.Solve(A, b); Assert.IsTrue(VectorF.AreNumericallyEqual(new VectorF(1, 5), x)); Assert.AreEqual(2, solver.NumberOfIterations); }
public void Test3() { MatrixF A = new MatrixF(new float[, ] { { 2, 0 }, { 0, 2 } }); VectorF b = new VectorF(new float[] { 20, 28 }); JacobiMethodF solver = new JacobiMethodF(); VectorF x = solver.Solve(A, null, b); Assert.IsTrue(VectorF.AreNumericallyEqual(b / 2, x)); Assert.AreEqual(2, solver.NumberOfIterations); }
public void Test2() { MatrixF A = new MatrixF(new float[, ] { { 1, 0 }, { 0, 1 } }); VectorF b = new VectorF(new float[] { 20, 28 }); GaussSeidelMethodF solver = new GaussSeidelMethodF(); VectorF x = solver.Solve(A, null, b); Assert.IsTrue(VectorF.AreNumericallyEqual(b, x)); Assert.AreEqual(2, solver.NumberOfIterations); }
public void Test4() { MatrixF A = new MatrixF(new float[, ] { { -12, 2 }, { 2, 3 } }); VectorF b = new VectorF(new float[] { 20, 28 }); SorMethodF solver = new SorMethodF(); VectorF x = solver.Solve(A, null, b); VectorF solution = MatrixF.SolveLinearEquations(A, b); Assert.IsTrue(VectorF.AreNumericallyEqual(solution, x)); }
public void Test5() { MatrixF A = new MatrixF(new float[, ] { { -21, 2, -4, 0 }, { 2, 3, 0.1f, -1 }, { 2, 10, 111.1f, -11 }, { 23, 112, 111.1f, -143 } }); VectorF b = new VectorF(new float[] { 20, 28, -12, 0.1f }); SorMethodF solver = new SorMethodF(); VectorF x = solver.Solve(A, null, b); VectorF solution = MatrixF.SolveLinearEquations(A, b); Assert.IsTrue(VectorF.AreNumericallyEqual(solution, x)); }
public void Test6() { MatrixF A = new MatrixF(new float[, ] { { -21, 2, -4, 0 }, { 2, 3, 0.1f, -1 }, { 2, 10, 111.1f, -11 }, { 23, 112, 111.1f, -143 } }); VectorF b = new VectorF(new float[] { 20, 28, -12, 0.1f }); JacobiMethodF solver = new JacobiMethodF(); solver.MaxNumberOfIterations = 10; VectorF x = solver.Solve(A, null, b); VectorF solution = MatrixF.SolveLinearEquations(A, b); Assert.IsFalse(VectorF.AreNumericallyEqual(solution, x)); Assert.AreEqual(10, solver.NumberOfIterations); }
public void Test7() { MatrixF A = new MatrixF(new float[, ] { { -21, 2, -4, 0 }, { 2, 3, 0.1f, -1 }, { 2, 10, 111.1f, -11 }, { 23, 112, 111.1f, -143 } }); VectorF b = new VectorF(new float[] { 20, 28, -12, 0.1f }); SorMethodF solver = new SorMethodF(); solver.Epsilon = 0.1f; VectorF x = solver.Solve(A, null, b); VectorF solution = MatrixF.SolveLinearEquations(A, b); Assert.IsTrue(VectorF.AreNumericallyEqual(solution, x, 0.1f)); Assert.IsFalse(VectorF.AreNumericallyEqual(solution, x)); Assert.Greater(12, solver.NumberOfIterations); // For normal accuracy (EpsilonF) we need 12 iterations. }
/// <summary> /// Solves the specified linear system of equations <i>Ax=b</i>. /// </summary> /// <param name="matrixA">The matrix A.</param> /// <param name="initialX"> /// The initial guess for x. If this value is <see langword="null"/>, a zero vector will be used /// as initial guess. /// </param> /// <param name="vectorB">The vector b.</param> /// <returns>The solution vector x.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="matrixA"/> is <see langword="null"/>. /// </exception> /// <exception cref="ArgumentNullException"> /// <paramref name="vectorB"/> is <see langword="null"/>. /// </exception> /// <exception cref="ArgumentException"> /// <paramref name="matrixA"/> is not a square matrix. /// </exception> /// <exception cref="ArgumentException"> /// The number of elements of <paramref name="initialX"/> does not match. /// </exception> public override VectorF Solve(MatrixF matrixA, VectorF initialX, VectorF vectorB) { // TODO: We can possible improve the method by reordering after each step. // This can be done randomly or we sort by the "convergence" of the elements. // See book Physics-Based Animation. NumberOfIterations = 0; if (matrixA == null) { throw new ArgumentNullException("matrixA"); } if (vectorB == null) { throw new ArgumentNullException("vectorB"); } if (matrixA.IsSquare == false) { throw new ArgumentException("Matrix A must be a square matrix.", "matrixA"); } if (matrixA.NumberOfRows != vectorB.NumberOfElements) { throw new ArgumentException("The number of rows of A and b do not match."); } if (initialX != null && initialX.NumberOfElements != vectorB.NumberOfElements) { throw new ArgumentException("The number of elements of the initial guess for x and b do not match."); } VectorF xOld = initialX ?? new VectorF(vectorB.NumberOfElements); VectorF xNew = new VectorF(vectorB.NumberOfElements); bool isConverged = false; // Make iterations until max iteration count or the result has converged. for (int i = 0; i < MaxNumberOfIterations && !isConverged; i++) { for (int j = 0; j < vectorB.NumberOfElements; j++) { float delta = 0; for (int k = 0; k < j; k++) { delta += matrixA[j, k] * xNew[k]; } for (int k = j + 1; k < vectorB.NumberOfElements; k++) { delta += matrixA[j, k] * xOld[k]; } xNew[j] = (vectorB[j] - delta) / matrixA[j, j]; } // Test convergence isConverged = VectorF.AreNumericallyEqual(xOld, xNew, Epsilon); xOld = xNew.Clone(); NumberOfIterations = i + 1; } return(xNew); }