[ADMB Users] Interfacing with software which expects a matrix objects values to be contiguous

dave fournier davef at otter-rsch.com
Sun Oct 2 11:29:12 PDT 2011


A lot of software  expects the values of a matrix to be in a vector of 
contiguous values.
This is inconvenient for ADMB users because the usual constructor  uses 
an array of vectors.
This comes up in GPU processing as well as using BLAS.  It is however a 
simple matter to
change the behaviour of the software so that a matrix consists of a 
vector of
contiguous values (they are stored by row and not by column, however.)
Here is an example of how to do it.

#include <admodel.h>

dmatrix unstack_dvector(int lr,int ur,int lc ,int uc,dvector& v)
{
   // should ckeck size
   int rowsize=ur-lr+1;
   int colsize=uc-lc+1;
   int ssize=rowsize*colsize;
   int vmin=v.indexmin();
   if (ssize != v.indexmax()-vmin+1)
   {
     cerr << "size error in unstack_dvector" << endl;
     ad_exit(1);
   }
   dmatrix M(lr,ur);
   //lc+offset=vmin;
   int offset=vmin-lc;
   for (int i=lr;i<=ur;i++)
   {
     M(i)=v(lc+offset,uc+offset).shift(lc);
     offset+=uc-lc+1;
   }
   return M;
}

   void printout(double * v,int n)
{
   for (int i=0;i<n;i++)
   {
     cout << v[i] << " ";
   }
   cout << endl;
}

int main(int argc,char * argv[])
{
   dvector v(1,40);
   v.fill_seqadd(1,1);
   dmatrix M=unstack_dvector(1,10,2,5,v);
   cout << M << endl;
   // interface with a C like zero offset pointer function
   printout(&v(1),40);
}





More information about the Users mailing list