1 /*$Id: getcolv.c,v 1.21 2001/08/07 03:03:20 balay Exp bsmith $*/ 2 3 #include "src/mat/matimpl.h" /*I "petscmat.h" I*/ 4 5 #undef __FUNCT__ 6 #define __FUNCT__ "MatGetColumnVector" 7 /*@ 8 MatGetColumnVector - Gets the values from a given column of a matrix. 9 10 Not Collective 11 12 Input Parameters: 13 + A - the matrix 14 . yy - the vector 15 - c - the column requested (in global numbering) 16 17 Level: advanced 18 19 Notes: 20 Each processor for which this is called gets the values for its rows. 21 22 Since PETSc matrices are usually stored in compressed row format, this routine 23 will generally be slow. 24 25 Contributed by: Denis Vanderstraeten 26 27 .keywords: matrix, column, get 28 29 .seealso: MatGetRow(), MatGetDiagonal() 30 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 38 PetscFunctionBegin; 39 PetscValidHeaderSpecific(A,MAT_COOKIE); 40 PetscValidHeaderSpecific(yy,VEC_COOKIE); 41 42 if (col < 0) SETERRQ1(1,"Requested negative column: %d",col); 43 ierr = MatGetSize(A,PETSC_NULL,&N);CHKERRQ(ierr); 44 if (col >= N) SETERRQ2(1,"Requested column %d larger than number columns in matrix %d",col,N); 45 46 ierr = MatGetOwnershipRange(A,&Rs,&Re);CHKERRQ(ierr); 47 48 ierr = PetscObjectGetComm((PetscObject)yy,&comm);CHKERRQ(ierr); 49 ierr = VecGetOwnershipRange(yy,&rs,&re);CHKERRQ(ierr); 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); 51 52 ierr = VecSet(&zero,yy);CHKERRQ(ierr); 53 ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 54 55 for (i=Rs; i<Re; i++) { 56 ierr = MatGetRow(A,i,&nz,&idx,&v);CHKERRQ(ierr); 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 ierr = MatRestoreRow(A,i,&nz,&idx,&v);CHKERRQ(ierr); 69 } 70 71 ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 72 PetscFunctionReturn(0); 73 } 74