Imported Upstream version ceres 1.13.0
[platform/upstream/ceres-solver.git] / internal / ceres / triplet_sparse_matrix.h
1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2015 Google Inc. All rights reserved.
3 // http://ceres-solver.org/
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 <vector>
35 #include "ceres/sparse_matrix.h"
36 #include "ceres/internal/eigen.h"
37 #include "ceres/internal/scoped_ptr.h"
38 #include "ceres/types.h"
39
40 namespace ceres {
41 namespace internal {
42
43 // An implementation of the SparseMatrix interface to store and
44 // manipulate sparse matrices in triplet (i,j,s) form.  This object is
45 // inspired by the design of the cholmod_triplet struct used in the
46 // SuiteSparse package and is memory layout compatible with it.
47 class TripletSparseMatrix : public SparseMatrix {
48  public:
49   TripletSparseMatrix();
50   TripletSparseMatrix(int num_rows, int num_cols, int max_num_nonzeros);
51   TripletSparseMatrix(int num_rows,
52                       int num_cols,
53                       const std::vector<int>& rows,
54                       const std::vector<int>& cols,
55                       const std::vector<double>& values);
56
57   explicit TripletSparseMatrix(const TripletSparseMatrix& orig);
58
59   TripletSparseMatrix& operator=(const TripletSparseMatrix& rhs);
60
61   ~TripletSparseMatrix();
62
63   // Implementation of the SparseMatrix interface.
64   virtual void SetZero();
65   virtual void RightMultiply(const double* x, double* y) const;
66   virtual void LeftMultiply(const double* x, double* y) const;
67   virtual void SquaredColumnNorm(double* x) const;
68   virtual void ScaleColumns(const double* scale);
69   virtual void ToDenseMatrix(Matrix* dense_matrix) const;
70   virtual void ToTextFile(FILE* file) const;
71   virtual int num_rows()        const { return num_rows_;     }
72   virtual int num_cols()        const { return num_cols_;     }
73   virtual int num_nonzeros()    const { return num_nonzeros_; }
74   virtual const double* values()  const { return values_.get(); }
75   virtual double* mutable_values()      { return values_.get(); }
76   virtual void set_num_nonzeros(int num_nonzeros);
77
78   // Increase max_num_nonzeros and correspondingly increase the size
79   // of rows_, cols_ and values_. If new_max_num_nonzeros is smaller
80   // than max_num_nonzeros_, then num_non_zeros should be less than or
81   // equal to new_max_num_nonzeros, otherwise data loss is possible
82   // and the method crashes.
83   void Reserve(int new_max_num_nonzeros);
84
85   // Append the matrix B at the bottom of this matrix. B should have
86   // the same number of columns as num_cols_.
87   void AppendRows(const TripletSparseMatrix& B);
88
89   // Append the matrix B at the right of this matrix. B should have
90   // the same number of rows as num_rows_;
91   void AppendCols(const TripletSparseMatrix& B);
92
93   // Resize the matrix. Entries which fall outside the new matrix
94   // bounds are dropped and the num_non_zeros changed accordingly.
95   void Resize(int new_num_rows, int new_num_cols);
96
97   int max_num_nonzeros() const { return max_num_nonzeros_; }
98   const int* rows()      const { return rows_.get();       }
99   const int* cols()      const { return cols_.get();       }
100   int* mutable_rows()          { return rows_.get();       }
101   int* mutable_cols()          { return cols_.get();       }
102
103   // Returns true if the entries of the matrix obey the row, column,
104   // and column size bounds and false otherwise.
105   bool AllTripletsWithinBounds() const;
106
107   bool IsValid() const { return AllTripletsWithinBounds(); }
108
109   // Build a sparse diagonal matrix of size num_rows x num_rows from
110   // the array values. Entries of the values array are copied into the
111   // sparse matrix.
112   static TripletSparseMatrix* CreateSparseDiagonalMatrix(const double* values,
113                                                          int num_rows);
114
115   // Options struct to control the generation of random
116   // TripletSparseMatrix objects.
117   struct RandomMatrixOptions {
118     int num_rows;
119     int num_cols;
120     // 0 < density <= 1 is the probability of an entry being
121     // structurally non-zero. A given random matrix will not have
122     // precisely this density.
123     double density;
124   };
125
126   // Create a random CompressedRowSparseMatrix whose entries are
127   // normally distributed and whose structure is determined by
128   // RandomMatrixOptions.
129   //
130   // Caller owns the result.
131   static TripletSparseMatrix* CreateRandomMatrix(
132       const TripletSparseMatrix::RandomMatrixOptions& options);
133
134  private:
135   void AllocateMemory();
136   void CopyData(const TripletSparseMatrix& orig);
137
138   int num_rows_;
139   int num_cols_;
140   int max_num_nonzeros_;
141   int num_nonzeros_;
142
143   // The data is stored as three arrays. For each i, values_[i] is
144   // stored at the location (rows_[i], cols_[i]). If the there are
145   // multiple entries with the same (rows_[i], cols_[i]), the values_
146   // entries corresponding to them are summed up.
147   scoped_array<int> rows_;
148   scoped_array<int> cols_;
149   scoped_array<double> values_;
150 };
151
152 }  // namespace internal
153 }  // namespace ceres
154
155 #endif  // CERES_INTERNAL_TRIPLET_SPARSE_MATRIX_H__