Actual source code: getcolv.c

  1: /*$Id: getcolv.c,v 1.23 2001/08/08 15:47:19 bsmith Exp $*/

 3:  #include src/mat/matimpl.h

  5: /*@
  6:    MatGetColumnVector - Gets the values from a given column of a matrix.

  8:    Not Collective

 10:    Input Parameters:
 11: +  A - the matrix
 12: .  yy - the vector
 13: -  c - the column requested (in global numbering)

 15:    Level: advanced

 17:    Notes:
 18:    Each processor for which this is called gets the values for its rows.

 20:    Since PETSc matrices are usually stored in compressed row format, this routine
 21:    will generally be slow.

 23:    The vector must have the same parallel row layout as the matrix.

 25:    Contributed by: Denis Vanderstraeten

 27: .keywords: matrix, column, get 

 29: .seealso: MatGetRow(), MatGetDiagonal()

 31: @*/
 32: int MatGetColumnVector(Mat A,Vec yy,int col)
 33: {
 34:   PetscScalar   *y,*v,zero = 0.0;
 35:   int      ierr,i,j,nz,*idx,N,Rs,Re,rs,re;
 36:   MPI_Comm comm;
 37: 

 42:   if (col < 0)  SETERRQ1(1,"Requested negative column: %d",col);
 43:   MatGetSize(A,PETSC_NULL,&N);
 44:   if (col >= N)  SETERRQ2(1,"Requested column %d larger than number columns in matrix %d",col,N);

 46:   MatGetOwnershipRange(A,&Rs,&Re);

 48:   PetscObjectGetComm((PetscObject)yy,&comm);
 49:   VecGetOwnershipRange(yy,&rs,&re);
 50:   if (Rs != rs || Re != re) SETERRQ4(1,"Matrix %d %d does not have same ownership range (size) as vector %d %d",Rs,Re,rs,re);

 52:   VecSet(&zero,yy);
 53:   VecGetArray(yy,&y);

 55:   for (i=Rs; i<Re; i++) {
 56:     MatGetRow(A,i,&nz,&idx,&v);
 57:     if (nz && idx[0] <= col) {
 58:       /*
 59:           Should use faster search here 
 60:       */
 61:       for (j=0; j<nz; j++) {
 62:         if (idx[j] >= col) {
 63:           if (idx[j] == col) y[i-rs] = v[j];
 64:           break;
 65:         }
 66:       }
 67:     }
 68:     MatRestoreRow(A,i,&nz,&idx,&v);
 69:   }

 71:   VecRestoreArray(yy,&y);
 72:   return(0);
 73: }