• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
3 // http://code.google.com/p/ceres-solver/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 //   this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 //   this list of conditions and the following disclaimer in the documentation
12 //   and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors may be
14 //   used to endorse or promote products derived from this software without
15 //   specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Author: sameeragarwal@google.com (Sameer Agarwal)
30 
31 #ifndef CERES_INTERNAL_TRIPLET_SPARSE_MATRIX_H_
32 #define CERES_INTERNAL_TRIPLET_SPARSE_MATRIX_H_
33 
34 #include "ceres/sparse_matrix.h"
35 #include "ceres/internal/eigen.h"
36 #include "ceres/internal/scoped_ptr.h"
37 #include "ceres/types.h"
38 
39 namespace ceres {
40 namespace internal {
41 
42 // An implementation of the SparseMatrix interface to store and
43 // manipulate sparse matrices in triplet (i,j,s) form.  This object is
44 // inspired by the design of the cholmod_triplet struct used in the
45 // SuiteSparse package and is memory layout compatible with it.
46 class TripletSparseMatrix : public SparseMatrix {
47  public:
48   TripletSparseMatrix();
49   TripletSparseMatrix(int num_rows, int num_cols, int max_num_nonzeros);
50   explicit TripletSparseMatrix(const TripletSparseMatrix& orig);
51 
52   TripletSparseMatrix& operator=(const TripletSparseMatrix& rhs);
53 
54   ~TripletSparseMatrix();
55 
56   // Implementation of the SparseMatrix interface.
57   virtual void SetZero();
58   virtual void RightMultiply(const double* x, double* y) const;
59   virtual void LeftMultiply(const double* x, double* y) const;
60   virtual void SquaredColumnNorm(double* x) const;
61   virtual void ScaleColumns(const double* scale);
62   virtual void ToDenseMatrix(Matrix* dense_matrix) const;
63   virtual void ToTextFile(FILE* file) const;
num_rows()64   virtual int num_rows()        const { return num_rows_;     }
num_cols()65   virtual int num_cols()        const { return num_cols_;     }
num_nonzeros()66   virtual int num_nonzeros()    const { return num_nonzeros_; }
values()67   virtual const double* values()  const { return values_.get(); }
mutable_values()68   virtual double* mutable_values()      { return values_.get(); }
69   virtual void set_num_nonzeros(int num_nonzeros);
70 
71   // Increase max_num_nonzeros and correspondingly increase the size
72   // of rows_, cols_ and values_. If new_max_num_nonzeros is smaller
73   // than max_num_nonzeros_, then num_non_zeros should be less than or
74   // equal to new_max_num_nonzeros, otherwise data loss is possible
75   // and the method crashes.
76   void Reserve(int new_max_num_nonzeros);
77 
78   // Append the matrix B at the bottom of this matrix. B should have
79   // the same number of columns as num_cols_.
80   void AppendRows(const TripletSparseMatrix& B);
81 
82   // Append the matrix B at the right of this matrix. B should have
83   // the same number of rows as num_rows_;
84   void AppendCols(const TripletSparseMatrix& B);
85 
86   // Resize the matrix. Entries which fall outside the new matrix
87   // bounds are dropped and the num_non_zeros changed accordingly.
88   void Resize(int new_num_rows, int new_num_cols);
89 
max_num_nonzeros()90   int max_num_nonzeros() const { return max_num_nonzeros_; }
rows()91   const int* rows()      const { return rows_.get();       }
cols()92   const int* cols()      const { return cols_.get();       }
mutable_rows()93   int* mutable_rows()          { return rows_.get();       }
mutable_cols()94   int* mutable_cols()          { return cols_.get();       }
95 
96   // Returns true if the entries of the matrix obey the row, column,
97   // and column size bounds and false otherwise.
98   bool AllTripletsWithinBounds() const;
99 
IsValid()100   bool IsValid() const { return AllTripletsWithinBounds(); }
101 
102   // Build a sparse diagonal matrix of size num_rows x num_rows from
103   // the array values. Entries of the values array are copied into the
104   // sparse matrix.
105   static TripletSparseMatrix* CreateSparseDiagonalMatrix(const double* values,
106                                                          int num_rows);
107 
108  private:
109   void AllocateMemory();
110   void CopyData(const TripletSparseMatrix& orig);
111 
112   int num_rows_;
113   int num_cols_;
114   int max_num_nonzeros_;
115   int num_nonzeros_;
116 
117   // The data is stored as three arrays. For each i, values_[i] is
118   // stored at the location (rows_[i], cols_[i]). If the there are
119   // multiple entries with the same (rows_[i], cols_[i]), the values_
120   // entries corresponding to them are summed up.
121   scoped_array<int> rows_;
122   scoped_array<int> cols_;
123   scoped_array<double> values_;
124 };
125 
126 }  // namespace internal
127 }  // namespace ceres
128 
129 #endif  // CERES_INTERNAL_TRIPLET_SPARSE_MATRIX_H__
130