本文整理汇总了C++中SparseMatrixsc::resize方法的典型用法代码示例。如果您正苦于以下问题:C++ SparseMatrixsc::resize方法的具体用法?C++ SparseMatrixsc::resize怎么用?C++ SparseMatrixsc::resize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SparseMatrixsc
的用法示例。
在下文中一共展示了SparseMatrixsc::resize方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: extractLowerTriangularMatrix
void MathUtilities::extractLowerTriangularMatrix( const SparseMatrixsc& A, SparseMatrixsc& B )
{
std::vector< Eigen::Triplet<scalar> > triplets;
for( int col = 0; col < A.outerSize(); ++col )
{
for( SparseMatrixsc::InnerIterator it( A, col ); it; ++it )
{
if( col > it.row() ) { continue; }
triplets.push_back( Eigen::Triplet<scalar>( it.row(), col, it.value() ) );
}
}
B.resize( A.rows(), A.cols() );
B.setFromTriplets( triplets.begin(), triplets.end() );
B.makeCompressed();
}
示例2: convertDenseToSparse
void MathUtilities::convertDenseToSparse( const bool filter_zeros, const MatrixXXsc& dense_matrix, SparseMatrixsc& sparse_matrix )
{
std::vector<Eigen::Triplet<scalar>> triplets;
for( int row = 0; row < dense_matrix.rows(); ++row )
{
for( int col = 0; col < dense_matrix.cols(); ++col )
{
if( dense_matrix( row, col ) != 0.0 || !filter_zeros )
{
triplets.emplace_back( Eigen::Triplet<scalar>{ row, col, dense_matrix( row, col ) } );
}
}
}
sparse_matrix.resize( dense_matrix.rows(), dense_matrix.cols() );
sparse_matrix.setFromTriplets( std::begin( triplets ), std::end( triplets ) );
sparse_matrix.makeCompressed();
}
示例3: deserialize
// TODO: Use utility class here
void MathUtilities::deserialize( SparseMatrixsc& A, std::istream& stm )
{
assert( stm.good() );
VectorXi col_ptr;
VectorXi row_ind;
VectorXs val;
// Read the number of rows in the matrix
int rows{ -1 };
stm.read((char*)&rows,sizeof(int));
assert( rows >= 0 );
// Read the number of columns in the matrix
int cols{ -1 };
stm.read((char*)&cols,sizeof(int));
assert( cols >= 0 );
// Read the column pointer array
col_ptr.resize(cols+1);
stm.read((char*)col_ptr.data(),col_ptr.size()*sizeof(int));
// Read the number of nonzeros in the array
int nnz{ -1 };
stm.read((char*)&nnz,sizeof(int));
assert( nnz >= 0 );
// Read the row-index array
row_ind.resize(nnz);
stm.read((char*)row_ind.data(),row_ind.size()*sizeof(int));
// Read the value array
val.resize(nnz);
stm.read((char*)val.data(),val.size()*sizeof(scalar));
A.resize(rows,cols);
A.reserve(nnz);
for( int col = 0; col < cols; ++col )
{
A.startVec(col);
for( int curel = col_ptr(col); curel < col_ptr(col+1); ++curel )
{
int row = row_ind(curel);
scalar curval = val(curel);
A.insertBack(row,col) = curval;
}
}
A.finalize();
}
示例4: formGeneralizedSmoothFrictionBasis
// TODO: Despecialize from smooth
void FrictionOperator::formGeneralizedSmoothFrictionBasis( const unsigned ndofs, const unsigned ncons, const VectorXs& q, const std::vector<std::unique_ptr<Constraint>>& K, const MatrixXXsc& bases, SparseMatrixsc& D )
{
assert( ncons == K.size() );
const unsigned nambientdims{ static_cast<unsigned>( bases.rows() ) };
const unsigned nsamples{ nambientdims - 1 };
D.resize( ndofs, nsamples * ncons );
auto itr = K.cbegin();
{
VectorXi column_nonzeros( D.cols() );
for( unsigned collision_number = 0; collision_number < ncons; ++collision_number )
{
for( unsigned sample_number = 0; sample_number < nsamples; ++sample_number )
{
assert( nsamples * collision_number + sample_number < column_nonzeros.size() );
column_nonzeros( nsamples * collision_number + sample_number ) = (*itr)->frictionStencilSize();
}
++itr;
}
assert( ( column_nonzeros.array() > 0 ).all() );
assert( itr == K.cend() );
D.reserve( column_nonzeros );
}
itr = K.cbegin();
for( unsigned collision_number = 0; collision_number < ncons; ++collision_number )
{
for( unsigned sample_number = 0; sample_number < nsamples; ++sample_number )
{
const unsigned current_column{ nsamples * collision_number + sample_number };
const VectorXs current_sample{ bases.col( nambientdims * collision_number + sample_number + 1 ) };
assert( fabs( current_sample.dot( bases.col( nambientdims * collision_number ) ) ) <= 1.0e-6 );
(*itr)->computeGeneralizedFrictionGivenTangentSample( q, current_sample, current_column, D );
}
++itr;
}
assert( itr == K.cend() );
D.prune( []( const Eigen::Index& row, const Eigen::Index& col, const scalar& value ) { return value != 0.0; } );
assert( D.innerNonZeroPtr() == nullptr );
}
示例5: extractColumns
// TODO: Pull the outerIndexPtr arithmetic into a helper function
void MathUtilities::extractColumns( const SparseMatrixsc& A0, const std::vector<unsigned>& cols, SparseMatrixsc& A1 )
{
const unsigned ncols_to_extract{ static_cast<unsigned>( cols.size() ) };
assert( ncols_to_extract <= static_cast<unsigned>( A0.cols() ) );
#ifndef NDEBUG
for( unsigned i = 0; i < ncols_to_extract; ++i )
{
assert( cols[i] < unsigned( A0.cols() ) );
}
#endif
// Compute the number of nonzeros in each column of the new matrix
VectorXi column_nonzeros{ ncols_to_extract };
for( unsigned i = 0; i < ncols_to_extract; ++i )
{
column_nonzeros( i ) = A0.outerIndexPtr()[cols[i]+1] - A0.outerIndexPtr()[cols[i]];
}
// Resize A1 and reserve space
A1.resize( A0.rows(), ncols_to_extract );
A1.reserve( column_nonzeros );
// Copy the data over, column by column
for( unsigned cur_col = 0; cur_col < ncols_to_extract; ++cur_col )
{
for( SparseMatrixsc::InnerIterator it( A0, cols[ cur_col ] ); it; ++it )
{
A1.insert( it.row(), cur_col ) = it.value();
}
}
A1.makeCompressed();
#ifndef NDEBUG
for( int i = 0 ; i < A1.cols(); ++i )
{
assert( ( A1.outerIndexPtr()[i+1] - A1.outerIndexPtr()[i] ) == column_nonzeros( i ) );
}
#endif
}