xref: /petsc/src/mat/utils/getcolv.c (revision 9d006df29267b69e91f0a72a1164c736258e6c11)
1*9d006df2SBarry Smith /*$Id: getcolv.c,v 1.21 2001/08/07 03:03:20 balay Exp bsmith $*/
20925cdddSBarry Smith 
3e090d566SSatish Balay #include "src/mat/matimpl.h"  /*I   "petscmat.h"  I*/
40925cdddSBarry Smith 
54a2ae208SSatish Balay #undef __FUNCT__
64a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnVector"
70925cdddSBarry Smith /*@
882bf6240SBarry Smith    MatGetColumnVector - Gets the values from a given column of a matrix.
90925cdddSBarry Smith 
1072257631SBarry Smith    Not Collective
11fee21e36SBarry Smith 
1298a79cdbSBarry Smith    Input Parameters:
135ed6d96aSBarry Smith +  A - the matrix
145ed6d96aSBarry Smith .  yy - the vector
155ed6d96aSBarry Smith -  c - the column requested (in global numbering)
1698a79cdbSBarry Smith 
1715091d37SBarry Smith    Level: advanced
1815091d37SBarry Smith 
19*9d006df2SBarry Smith    Notes:
20*9d006df2SBarry Smith    Each processor for which this is called gets the values for its rows.
21*9d006df2SBarry Smith 
22*9d006df2SBarry Smith    Since PETSc matrices are usually stored in compressed row format, this routine
23*9d006df2SBarry Smith    will generally be slow.
24*9d006df2SBarry Smith 
2582bf6240SBarry Smith    Contributed by: Denis Vanderstraeten
260925cdddSBarry Smith 
2782bf6240SBarry Smith .keywords: matrix, column, get
280925cdddSBarry Smith 
2915091d37SBarry Smith .seealso: MatGetRow(), MatGetDiagonal()
3015091d37SBarry Smith 
310925cdddSBarry Smith @*/
3282bf6240SBarry Smith int MatGetColumnVector(Mat A,Vec yy,int col)
330925cdddSBarry Smith {
34ea709b57SSatish Balay   PetscScalar   *y,*v,zero = 0.0;
354a2ae208SSatish Balay   int      ierr,i,j,nz,*idx,N,Rs,Re,rs,re;
36011b8408SBarry Smith   MPI_Comm comm;
370925cdddSBarry Smith 
380925cdddSBarry Smith   PetscFunctionBegin;
3982bf6240SBarry Smith   PetscValidHeaderSpecific(A,MAT_COOKIE);
4082bf6240SBarry Smith   PetscValidHeaderSpecific(yy,VEC_COOKIE);
410925cdddSBarry Smith 
4229bbc08cSBarry Smith   if (col < 0)  SETERRQ1(1,"Requested negative column: %d",col);
43011b8408SBarry Smith   ierr = MatGetSize(A,PETSC_NULL,&N);CHKERRQ(ierr);
4429bbc08cSBarry Smith   if (col >= N)  SETERRQ2(1,"Requested column %d larger than number columns in matrix %d",col,N);
450925cdddSBarry Smith 
4682bf6240SBarry Smith   ierr = MatGetOwnershipRange(A,&Rs,&Re);CHKERRQ(ierr);
47011b8408SBarry Smith 
48011b8408SBarry Smith   ierr = PetscObjectGetComm((PetscObject)yy,&comm);CHKERRQ(ierr);
4982bf6240SBarry Smith   ierr = VecGetOwnershipRange(yy,&rs,&re);CHKERRQ(ierr);
5029bbc08cSBarry Smith   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);
5182bf6240SBarry Smith 
5282bf6240SBarry Smith   ierr = VecSet(&zero,yy);CHKERRQ(ierr);
5382bf6240SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
5482bf6240SBarry Smith 
5582bf6240SBarry Smith   for (i=Rs; i<Re; i++) {
5682bf6240SBarry Smith     ierr = MatGetRow(A,i,&nz,&idx,&v);CHKERRQ(ierr);
5782bf6240SBarry Smith     if (nz && idx[0] <= col) {
5882bf6240SBarry Smith       /*
5982bf6240SBarry Smith           Should use faster search here
6082bf6240SBarry Smith       */
6182bf6240SBarry Smith       for (j=0; j<nz; j++) {
6282bf6240SBarry Smith         if (idx[j] >= col) {
6382bf6240SBarry Smith           if (idx[j] == col) y[i-rs] = v[j];
6482bf6240SBarry Smith           break;
650925cdddSBarry Smith         }
660925cdddSBarry Smith       }
670925cdddSBarry Smith     }
6882bf6240SBarry Smith     ierr = MatRestoreRow(A,i,&nz,&idx,&v);CHKERRQ(ierr);
690925cdddSBarry Smith   }
700925cdddSBarry Smith 
7182bf6240SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
720925cdddSBarry Smith   PetscFunctionReturn(0);
730925cdddSBarry Smith }
74