Skip to content
This repository has been archived by the owner on Oct 26, 2018. It is now read-only.

Commit

Permalink
Added support and tests: row and column blocks for sparse matrix
Browse files Browse the repository at this point in the history
transpose.

Change-Id: Ife641b08a9e86826478521a405f21ba60667f0e8
  • Loading branch information
Richard Bowen committed Apr 5, 2014
1 parent 5ecb1c3 commit 3e60a99
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/ceres/compressed_row_sparse_matrix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,9 @@ CompressedRowSparseMatrix* CompressedRowSparseMatrix::Transpose() const {
}
transpose_rows[0] = 0;

*(transpose->mutable_row_blocks()) = col_blocks_;
*(transpose->mutable_col_blocks()) = row_blocks_;

return transpose;
}

Expand Down
22 changes: 22 additions & 0 deletions internal/ceres/compressed_row_sparse_matrix_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,22 @@ TEST(CompressedRowSparseMatrix, Transpose) {
// 13 0 14 15 9 0
// 0 16 17 0 0 0

// Block structure:
// A A A A B B
// A A A A B B
// A A A A B B
// C C C C D D
// C C C C D D
// C C C C D D

CompressedRowSparseMatrix matrix(5, 6, 30);
int* rows = matrix.mutable_rows();
int* cols = matrix.mutable_cols();
double* values = matrix.mutable_values();
matrix.mutable_row_blocks()->push_back(3);
matrix.mutable_row_blocks()->push_back(3);
matrix.mutable_col_blocks()->push_back(4);
matrix.mutable_col_blocks()->push_back(2);

rows[0] = 0;
cols[0] = 1;
Expand Down Expand Up @@ -376,6 +388,16 @@ TEST(CompressedRowSparseMatrix, Transpose) {

scoped_ptr<CompressedRowSparseMatrix> transpose(matrix.Transpose());

ASSERT_EQ(transpose->row_blocks().size(), matrix.col_blocks().size());
for (int i = 0; i < transpose->row_blocks().size(); ++i) {
EXPECT_EQ(transpose->row_blocks()[i], matrix.col_blocks()[i]);
}

ASSERT_EQ(transpose->col_blocks().size(), matrix.row_blocks().size());
for (int i = 0; i < transpose->col_blocks().size(); ++i) {
EXPECT_EQ(transpose->col_blocks()[i], matrix.row_blocks()[i]);
}

Matrix dense_matrix;
matrix.ToDenseMatrix(&dense_matrix);

Expand Down

0 comments on commit 3e60a99

Please sign in to comment.