Prev Next sparse_cppad2eigen

@(@\newcommand{\B}[1]{{\bf #1}} \newcommand{\R}[1]{{\rm #1}}@)@
Convert A CppAD Sparse Matrix to an Eigen Sparse Matrix

Syntax
#include "utility.hpp"
sparse_cppad2eigen(sparse_cppadsparse_eigen) .

Prototype

template <class Scalar> void
     sparse_cppad2eigen(
     const CPPAD_SPARSE(Scalar)& sparse_cppad  ,
     SparseMatrix<Scalar>&       sparse_eigen  )

sparse_cppad
Is the CppAD sparse matrix that we are converting to an Eigen sparse matrix.

sparse_eigen
On input, if sparse_eigen.rows() or sparse_eigen.cols() is zero, a new sparsity patter is allocated in sparse_eigen . Otherwise, it is assumed that the sparsity pattern in sparse_eigen corresponds to a previous call to sparse_cppad2eigen with the same sparsity pattern in cppad_sparse.

Example

bool test_sparse_cppad2eigen(void)
{    bool ok = true;
     //
     // create sparse_cppad
     size_t nr  = 6;
     size_t nc  = nr;
     size_t nnz = nr  + nr - 1;
     CppAD::sparse_rc<VECTOR(size_t)> pattern_in(nr, nc, nnz);
     for(size_t k = 0; k < nr; k++)
          pattern_in.set(k, k, k);
     for(size_t k = 0; k < nr - 1; k++)
          pattern_in.set(nr + k, k+1, k);
     CPPAD_SPARSE(double) sparse_in(pattern_in);
     for(size_t k = 0; k < nnz; k++)
          sparse_in.set(k, double(k+1));
     //
     SparseMatrix<double> sparse_out;
     sparse_cppad2eigen(sparse_in, sparse_out);
     //
     // check the result
     const VECTOR(size_t) row_in( sparse_in.row() );
     const VECTOR(size_t) col_in( sparse_in.col() );
     const VECTOR(double) val_in( sparse_in.val() );
     //
     typedef typename SparseMatrix<double>::InnerIterator iterator;
     VECTOR(size_t) col_major = sparse_in.col_major();
     size_t k = 0;
     for(int c = 0; c < sparse_out.outerSize(); ++c)
     {    for(iterator itr(sparse_out, c); itr; ++itr)
          {    size_t ell = col_major[k];
               ok &= size_t(itr.row()) == row_in[ell];
               ok &= size_t(itr.col()) == col_in[ell];
               ok &= itr.value() == val_in[ell];
               ++k;
          }
     }
     //
     return ok;
}

Input File: src/utility.hpp