[petsc-users] Reshaping a vector into a matrix

Barry Smith bsmith at mcs.anl.gov
Wed Jan 5 22:38:23 CST 2011


On Jan 5, 2011, at 9:06 PM, S V N Vishwanathan wrote:

> Hi
> 
> In one of my programs I need to reshape a PETSc dense vector of
> dimensions n*k into a dense matrix of dimension n x k (similar to what
> you can do in, say, Matlab).  The vector is either  sequential or
> parallel. What is the most painless way to achieve this?
> 
> My first crude attempt was as follows:
> 
> info=MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(info);
> 
> if(size==1){
>    PetscScalar *vec_array;
>    info=VecGetArray(vec,&vec_array);CHKERRQ(info);
> info=MatCreateSeqDense(PETSC_COMM_WORLD,n,k,vec_array,&matrix);CHKERRQ(info);
>    MatView(matrix,PETSC_VIEWER_STDOUT_WORLD);
>    info=VecRestoreArray(vec,&vec_array);CHKERRQ(info);
>    // MatDestroy here?
>  }else{
> 	// Don't know how to handle this
> }
> 
> This gave me a access violation :(
> 
> If you are curious as to the application where this arises, I am using
> TAO to solve a machine learning problem. TAO expects the optimization
> parameters to be a vector, but when I evaluate the objective function I
> need to reshape them into a matrix. 
> 

   Do you mean a two dimensional array or do you mean a matrix -- that is the representation of a linear operator that applies to a vector giving a new vector?

   Barry


> vishy
> 



More information about the petsc-users mailing list