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