123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- #include "ceres/conjugate_gradients_solver.h"
- #include <memory>
- #include "ceres/internal/eigen.h"
- #include "ceres/linear_solver.h"
- #include "ceres/preconditioner.h"
- #include "ceres/triplet_sparse_matrix.h"
- #include "ceres/types.h"
- #include "gtest/gtest.h"
- namespace ceres::internal {
- TEST(ConjugateGradientTest, Solves3x3IdentitySystem) {
- double diagonal[] = {1.0, 1.0, 1.0};
- std::unique_ptr<TripletSparseMatrix> A(
- TripletSparseMatrix::CreateSparseDiagonalMatrix(diagonal, 3));
- Vector b(3);
- Vector x(3);
- b(0) = 1.0;
- b(1) = 2.0;
- b(2) = 3.0;
- x(0) = 1;
- x(1) = 1;
- x(2) = 1;
- ConjugateGradientsSolverOptions cg_options;
- cg_options.min_num_iterations = 1;
- cg_options.max_num_iterations = 10;
- cg_options.residual_reset_period = 20;
- cg_options.q_tolerance = 0.0;
- cg_options.r_tolerance = 1e-9;
- Vector scratch[4];
- for (int i = 0; i < 4; ++i) {
- scratch[i] = Vector::Zero(A->num_cols());
- }
- IdentityPreconditioner identity(A->num_cols());
- LinearOperatorAdapter lhs(*A);
- LinearOperatorAdapter preconditioner(identity);
- Vector* scratch_array[4] = {
- &scratch[0], &scratch[1], &scratch[2], &scratch[3]};
- auto summary = ConjugateGradientsSolver(
- cg_options, lhs, b, preconditioner, scratch_array, x);
- EXPECT_EQ(summary.termination_type, LinearSolverTerminationType::SUCCESS);
- ASSERT_EQ(summary.num_iterations, 1);
- ASSERT_DOUBLE_EQ(1, x(0));
- ASSERT_DOUBLE_EQ(2, x(1));
- ASSERT_DOUBLE_EQ(3, x(2));
- }
- TEST(ConjuateGradientTest, Solves3x3SymmetricSystem) {
- std::unique_ptr<TripletSparseMatrix> A(new TripletSparseMatrix(3, 3, 9));
- Vector b(3);
- Vector x(3);
-
-
-
- int* Ai = A->mutable_rows();
- int* Aj = A->mutable_cols();
- double* Ax = A->mutable_values();
- int counter = 0;
- for (int i = 0; i < 3; ++i) {
- for (int j = 0; j < 3; ++j) {
- Ai[counter] = i;
- Aj[counter] = j;
- ++counter;
- }
- }
- Ax[0] = 2.;
- Ax[1] = -1.;
- Ax[2] = 0;
- Ax[3] = -1.;
- Ax[4] = 2;
- Ax[5] = -1;
- Ax[6] = 0;
- Ax[7] = -1;
- Ax[8] = 2;
- A->set_num_nonzeros(9);
- b(0) = -1;
- b(1) = 0;
- b(2) = 3;
- x(0) = 1;
- x(1) = 1;
- x(2) = 1;
- ConjugateGradientsSolverOptions cg_options;
- cg_options.min_num_iterations = 1;
- cg_options.max_num_iterations = 10;
- cg_options.residual_reset_period = 20;
- cg_options.q_tolerance = 0.0;
- cg_options.r_tolerance = 1e-9;
- Vector scratch[4];
- for (int i = 0; i < 4; ++i) {
- scratch[i] = Vector::Zero(A->num_cols());
- }
- Vector* scratch_array[4] = {
- &scratch[0], &scratch[1], &scratch[2], &scratch[3]};
- IdentityPreconditioner identity(A->num_cols());
- LinearOperatorAdapter lhs(*A);
- LinearOperatorAdapter preconditioner(identity);
- auto summary = ConjugateGradientsSolver(
- cg_options, lhs, b, preconditioner, scratch_array, x);
- EXPECT_EQ(summary.termination_type, LinearSolverTerminationType::SUCCESS);
- ASSERT_DOUBLE_EQ(0, x(0));
- ASSERT_DOUBLE_EQ(1, x(1));
- ASSERT_DOUBLE_EQ(2, x(2));
- }
- }
|