Matrix generation cleanup

1. Convert a CompressedRowSparseMatrix constructor which
takes a TripletSparseMatrix as input into a factory method
which allows the input to be transposed.

2. Move the random matrix creation routine for CompressedRowSparseMatrix
from being a standalone function to a static method.

3. Add a corresponding random matrix generation static method to
TripletSparseMatrix.

4. Add a new constructor to TripletSparseMatrix, which takes as input
the row, col and values arrays.

Change-Id: Iec7b184646818f432a5e6822bea3b2f3128a82aa
This commit is contained in:
Sameer Agarwal
2017-05-01 17:24:22 -07:00
parent d72e19d985
commit 086ff01aca
8 changed files with 371 additions and 130 deletions
+26
View File
@@ -31,6 +31,7 @@
#ifndef CERES_INTERNAL_TRIPLET_SPARSE_MATRIX_H_
#define CERES_INTERNAL_TRIPLET_SPARSE_MATRIX_H_
#include <vector>
#include "ceres/sparse_matrix.h"
#include "ceres/internal/eigen.h"
#include "ceres/internal/scoped_ptr.h"
@@ -47,6 +48,12 @@ class TripletSparseMatrix : public SparseMatrix {
public:
TripletSparseMatrix();
TripletSparseMatrix(int num_rows, int num_cols, int max_num_nonzeros);
TripletSparseMatrix(int num_rows,
int num_cols,
const std::vector<int>& rows,
const std::vector<int>& cols,
const std::vector<double>& values);
explicit TripletSparseMatrix(const TripletSparseMatrix& orig);
TripletSparseMatrix& operator=(const TripletSparseMatrix& rhs);
@@ -105,6 +112,25 @@ class TripletSparseMatrix : public SparseMatrix {
static TripletSparseMatrix* CreateSparseDiagonalMatrix(const double* values,
int num_rows);
// Options struct to control the generation of random
// TripletSparseMatrix objects.
struct RandomMatrixOptions {
int num_rows;
int num_cols;
// 0 < density <= 1 is the probability of an entry being
// structurally non-zero. A given random matrix will not have
// precisely this density.
double density;
};
// Create a random CompressedRowSparseMatrix whose entries are
// normally distributed and whose structure is determined by
// RandomMatrixOptions.
//
// Caller owns the result.
static TripletSparseMatrix* CreateRandomMatrix(
const TripletSparseMatrix::RandomMatrixOptions& options);
private:
void AllocateMemory();
void CopyData(const TripletSparseMatrix& orig);