Thomas M. Ortiz wrote:
>
> Hello
>
> I’m using Octave 3.0.1, configured for “i686-pc-msdosmsvc”. I’ve 
> linked the libraries into my own program using VS 2005 SP1.
>
> I am trying to copy a TNT::Sparse_Matrix<double> into an Octave 
> SparseMatrix object using the following code:
>
> void TNTOctaveInterface::exportSparseMatrix(TNT::Sparse_Matrix<double> 
> &sparseTNTMatrix, SparseMatrix *sparseOctaveMatrix)
>
> {
>
> try
>
> {
>
> for (int i = 0; i < sparseTNTMatrix.num_rows(); i++)
>
> {
>
> for (int j = 0; j < sparseTNTMatrix.num_cols(); j++)
>
> {
>
> sparseOctaveMatrix->elem(j,i) = (sparseTNTMatrix)[i][j];
>
> }
>
> }
>
> return;
>
> }
>
> catch (...)
>
> ...
>

That is a ugly way of filling a sparse matrix.. You are having to store 
even the zeros, which probably explains the crash. One complexity of the 
TNT sparse matrix conversion to Octave's format is that TNT uses 
compressed row format and Octave uses compressed column. Another 
complexity, if the header for this class I found on the web is right, is 
that TNT stores the matrix as a set of sparse vectors, and the matrix 
doesn't let you get at the vector. If it did some like


void
TNTOctaveInterface::exportSparseMatrix (const TNT::Sparse_Matrix<double> 
&sparseTNTMatrix, SparseMatrix &sparseOctaveMatrix)
{
octave_idx_type nc = sparseTNTMatrix.num_cols();
octave_idx_type nr = sparseTNTMatrix.num_cols();
octave_idx_type nz = sparseTNTMatrix.num_nonzeros();

// TNT uses compressed row and Octave compressed column. Store the transpose
// and then transpose it yet again.
sparseOctaveMatrix = SparseMatrix (nc, nr, nz);

octave_idx_type jj = 0;
sparseOctaveMatrix.xcidx(0) = jj;
for (octave_idx_type j = 0; j <nc; j++)
{
for (TNT::Sparse_Vector<double>::const_iterator p =
TNT::Sparse_Matrix::S_[j].begin();
p <TNT::Sparse_Matrix::S_[j].end(); p++)
{
sparseOctaveMatrix.xdata(jj) = p->value();;
sparseOctaveMatrix.xridx(jj++) = p->index();;
}
sparseOctaveMatrix.xcidx(j + 1) = jj;
}

sparseOctaveMatrix = sparseOctaveMatrix.transpose ();
}

will do what you want.. Maybe it'll give you ideas for your solution..


D.


-- 
David Bateman                                dbate...@dbateman.org
35 rue Gambetta                              +33 1 46 04 02 18 (Home)
92100 Boulogne-Billancourt FRANCE            +33 6 72 01 06 33 (Mob)


------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
Octave-dev mailing list
Octave-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/octave-dev

Reply via email to