xref: /petsc/src/mat/utils/getcolv.c (revision 29bbc08cd5461c366aba6645924e8ff42acd1de0)
1*29bbc08cSBarry Smith /*$Id: getcolv.c,v 1.17 2000/09/13 03:11:32 bsmith Exp bsmith $*/
20925cdddSBarry Smith 
3e090d566SSatish Balay #include "src/mat/matimpl.h"  /*I   "petscmat.h"  I*/
40925cdddSBarry Smith 
50925cdddSBarry Smith #undef __FUNC__
6b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"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 
1982bf6240SBarry Smith    Contributed by: Denis Vanderstraeten
200925cdddSBarry Smith 
2182bf6240SBarry Smith .keywords: matrix, column, get
220925cdddSBarry Smith 
2315091d37SBarry Smith .seealso: MatGetRow(), MatGetDiagonal()
2415091d37SBarry Smith 
250925cdddSBarry Smith @*/
2682bf6240SBarry Smith int MatGetColumnVector(Mat A,Vec yy,int col)
270925cdddSBarry Smith {
2882bf6240SBarry Smith   Scalar   *y,*v,zero = 0.0;
29b48991e4SBarry Smith   int      ierr,i,j,nz,*idx,N,Rs,Re,rs,re,size;
30011b8408SBarry Smith   MPI_Comm comm;
310925cdddSBarry Smith 
320925cdddSBarry Smith   PetscFunctionBegin;
3382bf6240SBarry Smith   PetscValidHeaderSpecific(A,MAT_COOKIE);
3482bf6240SBarry Smith   PetscValidHeaderSpecific(yy,VEC_COOKIE);
350925cdddSBarry Smith 
36*29bbc08cSBarry Smith   if (col < 0)  SETERRQ1(1,"Requested negative column: %d",col);
37011b8408SBarry Smith   ierr = MatGetSize(A,PETSC_NULL,&N);CHKERRQ(ierr);
38*29bbc08cSBarry Smith   if (col >= N)  SETERRQ2(1,"Requested column %d larger than number columns in matrix %d",col,N);
390925cdddSBarry Smith 
4082bf6240SBarry Smith   ierr = MatGetOwnershipRange(A,&Rs,&Re);CHKERRQ(ierr);
41011b8408SBarry Smith 
42011b8408SBarry Smith   ierr = PetscObjectGetComm((PetscObject)yy,&comm);CHKERRQ(ierr);
43011b8408SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
4482bf6240SBarry Smith   ierr = VecGetOwnershipRange(yy,&rs,&re);CHKERRQ(ierr);
45*29bbc08cSBarry 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);
4682bf6240SBarry Smith 
4782bf6240SBarry Smith   ierr = VecSet(&zero,yy);CHKERRQ(ierr);
4882bf6240SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
4982bf6240SBarry Smith 
5082bf6240SBarry Smith   for (i=Rs; i<Re; i++) {
5182bf6240SBarry Smith     ierr = MatGetRow(A,i,&nz,&idx,&v);CHKERRQ(ierr);
5282bf6240SBarry Smith     if (nz && idx[0] <= col) {
5382bf6240SBarry Smith       /*
5482bf6240SBarry Smith           Should use faster search here
5582bf6240SBarry Smith       */
5682bf6240SBarry Smith       for (j=0; j<nz; j++) {
5782bf6240SBarry Smith         if (idx[j] >= col) {
5882bf6240SBarry Smith           if (idx[j] == col) y[i-rs] = v[j];
5982bf6240SBarry Smith           break;
600925cdddSBarry Smith         }
610925cdddSBarry Smith       }
620925cdddSBarry Smith     }
6382bf6240SBarry Smith     ierr = MatRestoreRow(A,i,&nz,&idx,&v);CHKERRQ(ierr);
640925cdddSBarry Smith   }
650925cdddSBarry Smith 
6682bf6240SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
670925cdddSBarry Smith   PetscFunctionReturn(0);
680925cdddSBarry Smith }
69