CppAD: A C++ Algorithmic Differentiation Package 20110419
sparse_map2vec.cpp
Go to the documentation of this file.
00001 /* $Id$ */
00002 /* --------------------------------------------------------------------------
00003 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-10 Bradley M. Bell
00004 
00005 CppAD is distributed under multiple licenses. This distribution is under
00006 the terms of the 
00007                     Common Public License Version 1.0.
00008 
00009 A copy of this license is included in the COPYING file of this distribution.
00010 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
00011 -------------------------------------------------------------------------- */
00012 # include "cppad_ipopt_nlp.hpp"
00013 # include "sparse_map2vec.hpp"
00014 
00015 CPPAD_BEGIN_NAMESPACE
00016 /*!
00017 \file sparse_map2vec.cpp
00018 \brief Create a two vector sparsity representation from a vector of maps.
00019 */
00020 
00021 
00022 /*!
00023 Create a two vector sparsity representation from a vector of maps.
00024 
00025 \param sparse
00026 Is a vector of maps representation of sparsity as well as
00027 the index in the two vector representation. To be specific;
00028 \verbatim
00029 for(i = 0; i < sparse.size(); i++)
00030 {       for(itr = sparse[i].begin(); itr != sparse[i].end(); itr++)
00031         {       j   = itr->first;
00032                 // (i, j) is a possibly non-zero entry in sparsity pattern
00033                 // k == itr->second, is corresponding index in i_row and j_col
00034                 k++;
00035         }
00036 }
00037 \endverbatim
00038 
00039 \param n_nz
00040 is the total number of possibly non-zero entries.
00041 
00042 \param i_row
00043 The input size and element values for \c i_row do not matter.
00044 On output, it has size \c n_nz
00045 and <tt>i_row[k]</tt> contains the row index corresponding to the
00046 \c k-th possibly non-zero entry.
00047 
00048 \param j_col
00049 The input size and element values for \c j_col do not matter.
00050 On output, it has size \c n_nz
00051 and <tt>j_col[k]</tt> contains the column index corresponding to the
00052 \c k-th possibly non-zero entry.
00053 */
00054 void sparse_map2vec(
00055         const CppAD::vector< std::map<size_t, size_t> > sparse,
00056         size_t&                                         n_nz  ,
00057         CppAD::vector<size_t>&                          i_row ,
00058         CppAD::vector<size_t>&                          j_col )
00059 {
00060         size_t i, j, k, m;
00061 
00062         // number of rows in sparse
00063         m    = sparse.size();
00064 
00065         // itererator for one row
00066         std::map<size_t, size_t>::const_iterator itr;
00067 
00068         // count the number of possibly non-zeros in sparse
00069         n_nz = 0;
00070         for(i = 0; i < m; i++)
00071                 for(itr = sparse[i].begin(); itr != sparse[i].end(); itr++)
00072                         ++n_nz;
00073 
00074         // resize the return vectors to accomidate n_nz entries
00075         i_row.resize(n_nz);
00076         j_col.resize(n_nz);
00077 
00078         // set the row and column indices and check assumptions on sparse
00079         k = 0;
00080         for(i = 0; i < m; i++)
00081         {       for(itr = sparse[i].begin(); itr != sparse[i].end(); itr++)
00082                 {       j = itr->first;
00083                         CPPAD_ASSERT_UNKNOWN( k == itr->second );
00084                         i_row[k] = i;
00085                         j_col[k] = j;
00086                         ++k;
00087                 }
00088         }
00089         return;
00090 }
00091 
00092 CPPAD_END_NAMESPACE