Boost logo

Ublas :

Subject: [ublas] Matrix in Preallocatd Chunk of Memory, Slices?
From: Jonas Huckestein (huckeste_at_[hidden])
Date: 2009-02-04 10:56:52


Hello there,

I just started using ublas (a great library, congratulations!) and
have a couple of questions :)

i have preallocated a matrix field of doubles like

double* array = new double[r][r];

Now I want to use that field of memory in an incremental algorithm,
which starts with a small 1x1 matrix which will grow but never exceed
rxr. is something like the following possible:

matrix<double> m (1,1, array); // I made this up :)
// then in each step of the algorithm
while(...) {
   if( increase_dimension ) {
     m.resize(i+1, i+1); // resize, without allocating or moving memory
   }
   m = something*m // do stuff
}

In fact, I do not know how the data of a dense matrix is stored. Is it
stored like a two-dimensional array? Or perhaps as an array
double[rows*cols].

Is it possible to use slices instead? Do slices have a large
performance overhead? Could/Should I do the following:

matrix<double> m(r,r); // allocate huge matrix
// then in each step of the algorithm
while(...) {
   if( increase_dimension ) {
     dim ++;
   }
   subslice(m,0,1,dim,0,1,dim) = something*subslice(m,0,1,dim,
0,1,dim) // does this work? is it fast?
}

Can I copy a matrix into a matric_slice, thus copying the elements of
the matrix into the slice, like follows?

subslice(m,0,1,dim,m,0,1,dim) = some other matrix;

What is the fastest way to copy a column of a sparse matrix in
compressed row_major format into the column of a dense matrix in
column major format?

Thanks in advance and kind regards,

Jonas