A version provided by Gael that avoids reallocation

This commit is contained in:
Andreas Fabri 2012-05-01 18:50:21 +00:00
parent d51a9dc356
commit c84cc28d1c
1 changed files with 18 additions and 10 deletions

View File

@ -53,14 +53,13 @@ public:
/// Create a square matrix initialized with zeros. /// Create a square matrix initialized with zeros.
Eigen_sparse_matrix(int dim, ///< Matrix dimension. Eigen_sparse_matrix(int dim, ///< Matrix dimension.
bool is_symmetric = false) ///< Symmetric/hermitian? bool is_symmetric = false) ///< Symmetric/hermitian?
: m_matrix(dim,dim) : m_matrix(dim,dim), m_is_uptodate(false)
{ {
CGAL_precondition(dim > 0); CGAL_precondition(dim > 0);
m_is_symmetric = is_symmetric; m_is_symmetric = is_symmetric;
// reserve memory for a regular 3D grid // reserve memory for a regular 3D grid
m_matrix.reserve(Eigen::VectorXi::Constant(m_matrix.outerSize(), 27)); m_triplets.reserve(dim*27);
} }
/// Create a rectangular matrix initialized with zeros. /// Create a rectangular matrix initialized with zeros.
@ -69,7 +68,7 @@ public:
Eigen_sparse_matrix(int rows, ///< Number of rows. Eigen_sparse_matrix(int rows, ///< Number of rows.
int columns, ///< Number of columns. int columns, ///< Number of columns.
bool is_symmetric = false) ///< Symmetric/hermitian? bool is_symmetric = false) ///< Symmetric/hermitian?
: m_matrix(rows,columns) : m_matrix(rows,columns), m_is_uptodate(false)
{ {
CGAL_precondition(rows > 0); CGAL_precondition(rows > 0);
CGAL_precondition(columns > 0); CGAL_precondition(columns > 0);
@ -78,9 +77,8 @@ public:
} }
m_is_symmetric = is_symmetric; m_is_symmetric = is_symmetric;
// reserve memory for a regular 3D grid // reserve memory for a regular 3D grid
m_matrix.reserve(Eigen::VectorXi::Constant(m_matrix.outerSize(), 27)); m_triplets.reserve(rows*27);
} }
/// Delete this object and the wrapped TAUCS matrix. /// Delete this object and the wrapped TAUCS matrix.
@ -113,8 +111,8 @@ public:
if (m_is_symmetric && (j > i)) if (m_is_symmetric && (j > i))
return; return;
if(new_coef) m_matrix.insert(i,j) = val; m_triplets.push_back(Triplet(i,j,val));
else m_matrix.coeffRef(i,j) = val; m_is_uptodate = false;
} }
/// Write access to a matrix coefficient: a_ij <- a_ij+val. /// Write access to a matrix coefficient: a_ij <- a_ij+val.
@ -134,13 +132,19 @@ public:
if (m_is_symmetric && (j > i)) if (m_is_symmetric && (j > i))
return; return;
m_matrix.coeffRef(i,j) += val; m_triplets.push_back(Triplet(i,j,val));
m_is_uptodate = false;
} }
const EigenType& eigen_object() const const EigenType& eigen_object() const
{ {
if(!m_is_uptodate)
{
m_matrix.setFromTriplets(m_triplets.begin(), m_triplets.end());
m_is_uptodate = true;
}
// turns the matrix into compressed mode: // turns the matrix into compressed mode:
// -> release some memory // -> release some memory
// -> required for some external solvers // -> required for some external solvers
@ -158,6 +162,10 @@ private:
// Fields // Fields
private: private:
mutable bool m_is_uptodate;
typedef Eigen::Triplet<T,int> Triplet;
mutable std::vector<Triplet> m_triplets;
mutable EigenType m_matrix; mutable EigenType m_matrix;
// Symmetric/hermitian? // Symmetric/hermitian?