#ifndef IGL_SUM_H #define IGL_SUM_H #include namespace igl { // Note: If your looking for dense matrix matlab like sum for eigen matrics // just use: // M.colwise().sum() or M.rowwise().sum() // // Sum the columns or rows of a sparse matrix // Templates: // T should be a eigen sparse matrix primitive type like int or double // Inputs: // X m by n sparse matrix // dim dimension along which to sum (1 or 2) // Output: // S n-long sparse vector (if dim == 1) // or // S m-long sparse vector (if dim == 2) template inline void sum( const Eigen::SparseMatrix& X, const int dim, Eigen::SparseVector& S); } // Implementation template inline void igl::sum( const Eigen::SparseMatrix& X, const int dim, Eigen::SparseVector& S) { // dim must be 2 or 1 assert(dim == 1 || dim == 2); // Get size of input int m = X.rows(); int n = X.cols(); // resize output if(dim==1) { S = Eigen::SparseVector(n); }else { S = Eigen::SparseVector(m); } // Iterate over outside for(int k=0; k::InnerIterator it (X,k); it; ++it) { if(dim == 1) { S.coeffRef(it.col()) += it.value(); }else { S.coeffRef(it.row()) += it.value(); } } } } #endif