Ordering.h 5.11 KB
Newer Older
Don Gagne's avatar
Don Gagne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2012  Désiré Nuentsa-Wakam <desire.nuentsa_wakam@inria.fr>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef EIGEN_ORDERING_H
#define EIGEN_ORDERING_H

namespace Eigen {
  
#include "Eigen_Colamd.h"

namespace internal {
    
/** \internal
  * \ingroup OrderingMethods_Module
22 23
  * \param[in] A the input non-symmetric matrix
  * \param[out] symmat the symmetric pattern A^T+A from the input matrix \a A.
Don Gagne's avatar
Don Gagne committed
24 25 26
  * FIXME: The values should not be considered here
  */
template<typename MatrixType> 
27
void ordering_helper_at_plus_a(const MatrixType& A, MatrixType& symmat)
Don Gagne's avatar
Don Gagne committed
28 29
{
  MatrixType C;
30
  C = A.transpose(); // NOTE: Could be  costly
Don Gagne's avatar
Don Gagne committed
31 32 33 34 35
  for (int i = 0; i < C.rows(); i++) 
  {
      for (typename MatrixType::InnerIterator it(C, i); it; ++it)
        it.valueRef() = 0.0;
  }
36
  symmat = C + A;
Don Gagne's avatar
Don Gagne committed
37 38 39 40 41 42 43 44 45 46 47
}
    
}

#ifndef EIGEN_MPL2_ONLY

/** \ingroup OrderingMethods_Module
  * \class AMDOrdering
  *
  * Functor computing the \em approximate \em minimum \em degree ordering
  * If the matrix is not structurally symmetric, an ordering of A^T+A is computed
48
  * \tparam  StorageIndex The type of indices of the matrix 
Don Gagne's avatar
Don Gagne committed
49 50
  * \sa COLAMDOrdering
  */
51
template <typename StorageIndex>
Don Gagne's avatar
Don Gagne committed
52 53 54
class AMDOrdering
{
  public:
55
    typedef PermutationMatrix<Dynamic, Dynamic, StorageIndex> PermutationType;
Don Gagne's avatar
Don Gagne committed
56 57 58 59 60 61 62 63
    
    /** Compute the permutation vector from a sparse matrix
     * This routine is much faster if the input matrix is column-major     
     */
    template <typename MatrixType>
    void operator()(const MatrixType& mat, PermutationType& perm)
    {
      // Compute the symmetric pattern
64
      SparseMatrix<typename MatrixType::Scalar, ColMajor, StorageIndex> symm;
Don Gagne's avatar
Don Gagne committed
65 66 67 68 69 70 71 72 73 74 75
      internal::ordering_helper_at_plus_a(mat,symm); 
    
      // Call the AMD routine 
      //m_mat.prune(keep_diag());
      internal::minimum_degree_ordering(symm, perm);
    }
    
    /** Compute the permutation with a selfadjoint matrix */
    template <typename SrcType, unsigned int SrcUpLo> 
    void operator()(const SparseSelfAdjointView<SrcType, SrcUpLo>& mat, PermutationType& perm)
    { 
76
      SparseMatrix<typename SrcType::Scalar, ColMajor, StorageIndex> C; C = mat;
Don Gagne's avatar
Don Gagne committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
      
      // Call the AMD routine 
      // m_mat.prune(keep_diag()); //Remove the diagonal elements 
      internal::minimum_degree_ordering(C, perm);
    }
};

#endif // EIGEN_MPL2_ONLY

/** \ingroup OrderingMethods_Module
  * \class NaturalOrdering
  *
  * Functor computing the natural ordering (identity)
  * 
  * \note Returns an empty permutation matrix
92
  * \tparam  StorageIndex The type of indices of the matrix 
Don Gagne's avatar
Don Gagne committed
93
  */
94
template <typename StorageIndex>
Don Gagne's avatar
Don Gagne committed
95 96 97
class NaturalOrdering
{
  public:
98
    typedef PermutationMatrix<Dynamic, Dynamic, StorageIndex> PermutationType;
Don Gagne's avatar
Don Gagne committed
99 100 101 102 103 104 105 106 107 108 109 110 111
    
    /** Compute the permutation vector from a column-major sparse matrix */
    template <typename MatrixType>
    void operator()(const MatrixType& /*mat*/, PermutationType& perm)
    {
      perm.resize(0); 
    }
    
};

/** \ingroup OrderingMethods_Module
  * \class COLAMDOrdering
  *
112 113
  * \tparam  StorageIndex The type of indices of the matrix 
  * 
Don Gagne's avatar
Don Gagne committed
114
  * Functor computing the \em column \em approximate \em minimum \em degree ordering 
115
  * The matrix should be in column-major and \b compressed format (see SparseMatrix::makeCompressed()).
Don Gagne's avatar
Don Gagne committed
116
  */
117
template<typename StorageIndex>
Don Gagne's avatar
Don Gagne committed
118 119 120
class COLAMDOrdering
{
  public:
121 122
    typedef PermutationMatrix<Dynamic, Dynamic, StorageIndex> PermutationType; 
    typedef Matrix<StorageIndex, Dynamic, 1> IndexVector;
Don Gagne's avatar
Don Gagne committed
123
    
124 125 126
    /** Compute the permutation vector \a perm form the sparse matrix \a mat
      * \warning The input sparse matrix \a mat must be in compressed mode (see SparseMatrix::makeCompressed()).
      */
Don Gagne's avatar
Don Gagne committed
127 128 129
    template <typename MatrixType>
    void operator() (const MatrixType& mat, PermutationType& perm)
    {
130 131
      eigen_assert(mat.isCompressed() && "COLAMDOrdering requires a sparse matrix in compressed mode. Call .makeCompressed() before passing it to COLAMDOrdering");
      
132 133 134
      StorageIndex m = StorageIndex(mat.rows());
      StorageIndex n = StorageIndex(mat.cols());
      StorageIndex nnz = StorageIndex(mat.nonZeros());
Don Gagne's avatar
Don Gagne committed
135
      // Get the recommended value of Alen to be used by colamd
136
      StorageIndex Alen = internal::colamd_recommended(nnz, m, n); 
Don Gagne's avatar
Don Gagne committed
137 138
      // Set the default parameters
      double knobs [COLAMD_KNOBS]; 
139
      StorageIndex stats [COLAMD_STATS];
Don Gagne's avatar
Don Gagne committed
140 141 142
      internal::colamd_set_defaults(knobs);
      
      IndexVector p(n+1), A(Alen); 
143 144
      for(StorageIndex i=0; i <= n; i++)   p(i) = mat.outerIndexPtr()[i];
      for(StorageIndex i=0; i < nnz; i++)  A(i) = mat.innerIndexPtr()[i];
Don Gagne's avatar
Don Gagne committed
145
      // Call Colamd routine to compute the ordering 
146
      StorageIndex info = internal::colamd(m, n, Alen, A.data(), p.data(), knobs, stats); 
147
      EIGEN_UNUSED_VARIABLE(info);
Don Gagne's avatar
Don Gagne committed
148 149 150
      eigen_assert( info && "COLAMD failed " );
      
      perm.resize(n);
151
      for (StorageIndex i = 0; i < n; i++) perm.indices()(p(i)) = i;
Don Gagne's avatar
Don Gagne committed
152 153 154 155 156 157
    }
};

} // end namespace Eigen

#endif