xref: /petsc/src/mat/utils/getcolv.c (revision b45d2f2cb7e031d9c0de5873eca80614ca7b863b)
10925cdddSBarry Smith 
2*b45d2f2cSJed Brown #include <petsc-private/matimpl.h>  /*I   "petscmat.h"  I*/
30925cdddSBarry Smith 
44a2ae208SSatish Balay #undef __FUNCT__
54a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnVector"
60925cdddSBarry Smith /*@
782bf6240SBarry Smith    MatGetColumnVector - Gets the values from a given column of a matrix.
80925cdddSBarry Smith 
972257631SBarry Smith    Not Collective
10fee21e36SBarry Smith 
1198a79cdbSBarry Smith    Input Parameters:
125ed6d96aSBarry Smith +  A - the matrix
135ed6d96aSBarry Smith .  yy - the vector
145ed6d96aSBarry Smith -  c - the column requested (in global numbering)
1598a79cdbSBarry Smith 
1615091d37SBarry Smith    Level: advanced
1715091d37SBarry Smith 
189d006df2SBarry Smith    Notes:
199d006df2SBarry Smith    Each processor for which this is called gets the values for its rows.
209d006df2SBarry Smith 
219d006df2SBarry Smith    Since PETSc matrices are usually stored in compressed row format, this routine
229d006df2SBarry Smith    will generally be slow.
239d006df2SBarry Smith 
243d81755aSBarry Smith    The vector must have the same parallel row layout as the matrix.
253d81755aSBarry Smith 
2682bf6240SBarry Smith    Contributed by: Denis Vanderstraeten
270925cdddSBarry Smith 
2882bf6240SBarry Smith .keywords: matrix, column, get
290925cdddSBarry Smith 
3015091d37SBarry Smith .seealso: MatGetRow(), MatGetDiagonal()
3115091d37SBarry Smith 
320925cdddSBarry Smith @*/
337087cfbeSBarry Smith PetscErrorCode  MatGetColumnVector(Mat A,Vec yy,PetscInt col)
340925cdddSBarry Smith {
358aa348c1SBarry Smith   PetscScalar        *y;
36b3cc6726SBarry Smith   const PetscScalar  *v;
37dfbe8321SBarry Smith   PetscErrorCode     ierr;
3838baddfdSBarry Smith   PetscInt           i,j,nz,N,Rs,Re,rs,re;
3938baddfdSBarry Smith   const PetscInt     *idx;
400925cdddSBarry Smith 
410925cdddSBarry Smith   PetscFunctionBegin;
420700a824SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
430700a824SBarry Smith   PetscValidHeaderSpecific(yy,VEC_CLASSID,2);
44e32f2f54SBarry Smith   if (col < 0)  SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Requested negative column: %D",col);
45011b8408SBarry Smith   ierr = MatGetSize(A,PETSC_NULL,&N);CHKERRQ(ierr);
46e32f2f54SBarry Smith   if (col >= N)  SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Requested column %D larger than number columns in matrix %D",col,N);
4782bf6240SBarry Smith   ierr = MatGetOwnershipRange(A,&Rs,&Re);CHKERRQ(ierr);
4882bf6240SBarry Smith   ierr = VecGetOwnershipRange(yy,&rs,&re);CHKERRQ(ierr);
49e32f2f54SBarry Smith   if (Rs != rs || Re != re) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Matrix %D %D does not have same ownership range (size) as vector %D %D",Rs,Re,rs,re);
5082bf6240SBarry Smith 
518d0534beSBarry Smith   if (A->ops->getcolumnvector) {
528d0534beSBarry Smith     ierr = (*A->ops->getcolumnvector)(A,yy,col);CHKERRQ(ierr);
538d0534beSBarry Smith   } else {
548aa348c1SBarry Smith     ierr = VecSet(yy,0.0);CHKERRQ(ierr);
5582bf6240SBarry Smith     ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
5682bf6240SBarry Smith 
5782bf6240SBarry Smith     for (i=Rs; i<Re; i++) {
5882bf6240SBarry Smith       ierr = MatGetRow(A,i,&nz,&idx,&v);CHKERRQ(ierr);
5982bf6240SBarry Smith       if (nz && idx[0] <= col) {
6082bf6240SBarry Smith 	/*
6182bf6240SBarry Smith           Should use faster search here
6282bf6240SBarry Smith 	*/
6382bf6240SBarry Smith 	for (j=0; j<nz; j++) {
6482bf6240SBarry Smith 	  if (idx[j] >= col) {
6582bf6240SBarry Smith 	    if (idx[j] == col) y[i-rs] = v[j];
6682bf6240SBarry Smith 	    break;
670925cdddSBarry Smith 	  }
680925cdddSBarry Smith 	}
690925cdddSBarry Smith       }
7082bf6240SBarry Smith       ierr = MatRestoreRow(A,i,&nz,&idx,&v);CHKERRQ(ierr);
710925cdddSBarry Smith     }
7282bf6240SBarry Smith     ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
738d0534beSBarry Smith   }
740925cdddSBarry Smith   PetscFunctionReturn(0);
750925cdddSBarry Smith }
76242f1d38SBarry Smith 
77242f1d38SBarry Smith 
78242f1d38SBarry Smith 
79242f1d38SBarry Smith 
80242f1d38SBarry Smith #undef __FUNCT__
81242f1d38SBarry Smith #define __FUNCT__ "MatGetColumnNorms"
8286819fdcSBarry Smith /*@
830716a85fSBarry Smith     MatGetColumnNorms - Gets the norms of each column of a sparse or dense matrix.
84242f1d38SBarry Smith 
85242f1d38SBarry Smith   Input Parameter:
86242f1d38SBarry Smith +  A - the matrix
87242f1d38SBarry Smith -  type - NORM_2, NORM_1 or NORM_INFINITY
88242f1d38SBarry Smith 
89242f1d38SBarry Smith   Output Parameter:
90242f1d38SBarry Smith .  norms - an array as large as the TOTAL number of columns in the matrix
91242f1d38SBarry Smith 
92f6680f47SSatish Balay    Level: intermediate
93f6680f47SSatish Balay 
9486819fdcSBarry Smith    Notes: Each process has ALL the column norms after the call. Because of the way this is computed each process gets all the values,
9586819fdcSBarry Smith     if each process wants only some of the values it should extract the ones it wants from the array.
9686819fdcSBarry Smith 
9786819fdcSBarry Smith .seealso: MatGetColumns()
9886819fdcSBarry Smith 
9986819fdcSBarry Smith @*/
100242f1d38SBarry Smith PetscErrorCode MatGetColumnNorms(Mat A,NormType type,PetscReal *norms)
101242f1d38SBarry Smith {
102242f1d38SBarry Smith   PetscErrorCode ierr;
103242f1d38SBarry Smith 
104242f1d38SBarry Smith   PetscFunctionBegin;
1050716a85fSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
1060716a85fSBarry Smith   if (A->ops->getcolumnnorms) {
1070716a85fSBarry Smith     ierr = (*A->ops->getcolumnnorms)(A,type,norms);CHKERRQ(ierr);
10817186662SBarry Smith   } else SETERRQ(((PetscObject)A)->comm,PETSC_ERR_SUP,"Not coded for this matrix type");
109242f1d38SBarry Smith   PetscFunctionReturn(0);
110242f1d38SBarry Smith }
111242f1d38SBarry Smith 
112