Home | History | Annotate | Download | only in ceres
      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: keir (at) google.com (Keir Mierle)
     30 
     31 #include "ceres/dense_sparse_matrix.h"
     32 
     33 #include <algorithm>
     34 #include "ceres/matrix_proto.h"
     35 #include "ceres/triplet_sparse_matrix.h"
     36 #include "ceres/internal/eigen.h"
     37 #include "ceres/internal/port.h"
     38 
     39 namespace ceres {
     40 namespace internal {
     41 
     42 DenseSparseMatrix::DenseSparseMatrix(int num_rows, int num_cols)
     43     : has_diagonal_appended_(false),
     44       has_diagonal_reserved_(false) {
     45   // Allocate enough space for the diagonal.
     46   m_.resize(num_rows, num_cols);
     47   m_.setZero();
     48 }
     49 
     50 DenseSparseMatrix::DenseSparseMatrix(int num_rows, int num_cols, bool reserve_diagonal)
     51     : has_diagonal_appended_(false),
     52       has_diagonal_reserved_(reserve_diagonal) {
     53   // Allocate enough space for the diagonal.
     54   if (reserve_diagonal) {
     55     m_.resize(num_rows +  num_cols, num_cols);
     56   } else {
     57     m_.resize(num_rows, num_cols);
     58   }
     59   m_.setZero();
     60 }
     61 
     62 DenseSparseMatrix::DenseSparseMatrix(const TripletSparseMatrix& m)
     63     : m_(Eigen::MatrixXd::Zero(m.num_rows(), m.num_cols())),
     64       has_diagonal_appended_(false),
     65       has_diagonal_reserved_(false) {
     66   const double *values = m.values();
     67   const int *rows = m.rows();
     68   const int *cols = m.cols();
     69   int num_nonzeros = m.num_nonzeros();
     70 
     71   for (int i = 0; i < num_nonzeros; ++i) {
     72     m_(rows[i], cols[i]) += values[i];
     73   }
     74 }
     75 
     76 DenseSparseMatrix::DenseSparseMatrix(const Matrix& m)
     77     : m_(m),
     78       has_diagonal_appended_(false),
     79       has_diagonal_reserved_(false) {
     80 }
     81 
     82 #ifndef CERES_NO_PROTOCOL_BUFFERS
     83 DenseSparseMatrix::DenseSparseMatrix(const SparseMatrixProto& outer_proto)
     84     : m_(Eigen::MatrixXd::Zero(
     85         outer_proto.dense_matrix().num_rows(),
     86         outer_proto.dense_matrix().num_cols())),
     87       has_diagonal_appended_(false),
     88       has_diagonal_reserved_(false) {
     89   const DenseSparseMatrixProto& proto = outer_proto.dense_matrix();
     90   for (int i = 0; i < m_.rows(); ++i) {
     91     for (int j = 0; j < m_.cols(); ++j) {
     92       m_(i, j) = proto.values(m_.cols() * i + j);
     93     }
     94   }
     95 }
     96 #endif
     97 
     98 void DenseSparseMatrix::SetZero() {
     99   m_.setZero();
    100 }
    101 
    102 void DenseSparseMatrix::RightMultiply(const double* x, double* y) const {
    103   VectorRef(y, num_rows()) += matrix() * ConstVectorRef(x, num_cols());
    104 }
    105 
    106 void DenseSparseMatrix::LeftMultiply(const double* x, double* y) const {
    107   VectorRef(y, num_cols()) +=
    108       matrix().transpose() * ConstVectorRef(x, num_rows());
    109 }
    110 
    111 void DenseSparseMatrix::SquaredColumnNorm(double* x) const {
    112   VectorRef(x, num_cols()) = m_.colwise().squaredNorm();
    113 }
    114 
    115 void DenseSparseMatrix::ScaleColumns(const double* scale) {
    116   m_ *= ConstVectorRef(scale, num_cols()).asDiagonal();
    117 }
    118 
    119 void DenseSparseMatrix::ToDenseMatrix(Matrix* dense_matrix) const {
    120   *dense_matrix = m_;
    121 }
    122 
    123 #ifndef CERES_NO_PROTOCOL_BUFFERS
    124 void DenseSparseMatrix::ToProto(SparseMatrixProto* outer_proto) const {
    125   CHECK(!has_diagonal_appended_) << "Not supported.";
    126   outer_proto->Clear();
    127   DenseSparseMatrixProto* proto = outer_proto->mutable_dense_matrix();
    128 
    129   proto->set_num_rows(num_rows());
    130   proto->set_num_cols(num_cols());
    131 
    132   int num_nnz = num_nonzeros();
    133   for (int i = 0; i < num_nnz; ++i) {
    134     proto->add_values(m_.data()[i]);
    135   }
    136 }
    137 #endif
    138 
    139 void DenseSparseMatrix::AppendDiagonal(double *d) {
    140   CHECK(!has_diagonal_appended_);
    141   if (!has_diagonal_reserved_) {
    142     Matrix tmp = m_;
    143     m_.resize(m_.rows() + m_.cols(), m_.cols());
    144     m_.setZero();
    145     m_.block(0, 0, tmp.rows(), tmp.cols()) = tmp;
    146     has_diagonal_reserved_ = true;
    147   }
    148 
    149   m_.bottomLeftCorner(m_.cols(), m_.cols()) =
    150       ConstVectorRef(d, m_.cols()).asDiagonal();
    151   has_diagonal_appended_ = true;
    152 }
    153 
    154 void DenseSparseMatrix::RemoveDiagonal() {
    155   CHECK(has_diagonal_appended_);
    156   has_diagonal_appended_ = false;
    157   // Leave the diagonal reserved.
    158 }
    159 
    160 int DenseSparseMatrix::num_rows() const {
    161   if (has_diagonal_reserved_ && !has_diagonal_appended_) {
    162     return m_.rows() - m_.cols();
    163   }
    164   return m_.rows();
    165 }
    166 
    167 int DenseSparseMatrix::num_cols() const {
    168   return m_.cols();
    169 }
    170 
    171 int DenseSparseMatrix::num_nonzeros() const {
    172   if (has_diagonal_reserved_ && !has_diagonal_appended_) {
    173     return (m_.rows() - m_.cols()) * m_.cols();
    174   }
    175   return m_.rows() * m_.cols();
    176 }
    177 
    178 ConstAlignedMatrixRef DenseSparseMatrix::matrix() const {
    179   if (has_diagonal_reserved_ && !has_diagonal_appended_) {
    180     return ConstAlignedMatrixRef(
    181         m_.data(), m_.rows() - m_.cols(), m_.cols());
    182   }
    183   return ConstAlignedMatrixRef(m_.data(), m_.rows(), m_.cols());
    184 }
    185 
    186 AlignedMatrixRef DenseSparseMatrix::mutable_matrix() {
    187   if (has_diagonal_reserved_ && !has_diagonal_appended_) {
    188     return AlignedMatrixRef(
    189         m_.data(), m_.rows() - m_.cols(), m_.cols());
    190   }
    191   return AlignedMatrixRef(m_.data(), m_.rows(), m_.cols());
    192 }
    193 
    194 void DenseSparseMatrix::ToTextFile(FILE* file) const {
    195   CHECK_NOTNULL(file);
    196   const int active_rows =
    197       (has_diagonal_reserved_ && !has_diagonal_appended_)
    198       ? (m_.rows() - m_.cols())
    199       : m_.rows();
    200 
    201   for (int r = 0; r < active_rows; ++r) {
    202     for (int c = 0; c < m_.cols(); ++c) {
    203       fprintf(file,  "% 10d % 10d %17f\n", r, c, m_(r, c));
    204     }
    205   }
    206 }
    207 
    208 }  // namespace internal
    209 }  // namespace ceres
    210