Actual source code: getcolv.c

 2:  #include src/mat/matimpl.h

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

  9:    Not Collective

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

 16:    Level: advanced

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

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

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

 26:    Contributed by: Denis Vanderstraeten

 28: .keywords: matrix, column, get 

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

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

 46:   if (col < 0)  SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Requested negative column: %D",col);
 47:   MatGetSize(A,PETSC_NULL,&N);
 48:   if (col >= N)  SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Requested column %D larger than number columns in matrix %D",col,N);

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

 52:   PetscObjectGetComm((PetscObject)yy,&comm);
 53:   VecGetOwnershipRange(yy,&rs,&re);
 54:   if (Rs != rs || Re != re) SETERRQ4(PETSC_ERR_ARG_INCOMP,"Matrix %D %D does not have same ownership range (size) as vector %D %D",Rs,Re,rs,re);

 56:   VecSet(&zero,yy);
 57:   VecGetArray(yy,&y);

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

 75:   VecRestoreArray(yy,&y);
 76:   return(0);
 77: }