CppAD: A C++ Algorithmic Differentiation Package  20130918
sparse_map2vec.cpp
Go to the documentation of this file.
00001 /* $Id$ */
00002 /* --------------------------------------------------------------------------
00003 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-14 Bradley M. Bell
00004 
00005 CppAD is distributed under multiple licenses. This distribution is under
00006 the terms of the 
00007                     Eclipse 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 // ---------------------------------------------------------------------------
00016 namespace cppad_ipopt {
00017 // ---------------------------------------------------------------------------
00018 /*!
00019 \{
00020 \file sparse_map2vec.cpp
00021 \brief Create a two vector sparsity representation from a vector of maps.
00022 */
00023 
00024 
00025 /*!
00026 Create a two vector sparsity representation from a vector of maps.
00027 
00028 \param sparse
00029 Is a vector of maps representation of sparsity as well as
00030 the index in the two vector representation. To be specific;
00031 \verbatim
00032 for(i = 0; i < sparse.size(); i++)
00033 {    for(itr = sparse[i].begin(); itr != sparse[i].end(); itr++)
00034      {    j   = itr->first;
00035           // (i, j) is a possibly non-zero entry in sparsity pattern
00036           // k == itr->second, is corresponding index in i_row and j_col
00037           k++;
00038      }
00039 }
00040 \endverbatim
00041 
00042 \param n_nz
00043 is the total number of possibly non-zero entries.
00044 
00045 \param i_row
00046 The input size and element values for \c i_row do not matter.
00047 On output, it has size \c n_nz
00048 and <tt>i_row[k]</tt> contains the row index corresponding to the
00049 \c k-th possibly non-zero entry.
00050 
00051 \param j_col
00052 The input size and element values for \c j_col do not matter.
00053 On output, it has size \c n_nz
00054 and <tt>j_col[k]</tt> contains the column index corresponding to the
00055 \c k-th possibly non-zero entry.
00056 */
00057 void sparse_map2vec(
00058      const CppAD::vector< std::map<size_t, size_t> > sparse,
00059      size_t&                                         n_nz  ,
00060      CppAD::vector<size_t>&                          i_row ,
00061      CppAD::vector<size_t>&                          j_col )
00062 {
00063      size_t i, j, k, m;
00064 
00065      // number of rows in sparse
00066      m    = sparse.size();
00067 
00068      // itererator for one row
00069      std::map<size_t, size_t>::const_iterator itr;
00070 
00071      // count the number of possibly non-zeros in sparse
00072      n_nz = 0;
00073      for(i = 0; i < m; i++)
00074           for(itr = sparse[i].begin(); itr != sparse[i].end(); itr++)
00075                ++n_nz;
00076 
00077      // resize the return vectors to accomidate n_nz entries
00078      i_row.resize(n_nz);
00079      j_col.resize(n_nz);
00080 
00081      // set the row and column indices and check assumptions on sparse
00082      k = 0;
00083      for(i = 0; i < m; i++)
00084      {    for(itr = sparse[i].begin(); itr != sparse[i].end(); itr++)
00085           {    j = itr->first;
00086                CPPAD_ASSERT_UNKNOWN( k == itr->second );
00087                i_row[k] = i;
00088                j_col[k] = j;
00089                ++k;
00090           }
00091      }
00092      return;
00093 }
00094 
00095 // ---------------------------------------------------------------------------
00096 } // end namespace cppad_ipopt
00097 // ---------------------------------------------------------------------------
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines