Actual source code: getcolv.c
petsc-3.14.6 2021-03-30
2: #include <petsc/private/matimpl.h>
4: /*@
5: MatGetColumnVector - Gets the values from a given column of a matrix.
7: Not Collective
9: Input Parameters:
10: + A - the matrix
11: . yy - the vector
12: - col - the column requested (in global numbering)
14: Level: advanced
16: Notes:
17: If a Mat type does not implement the operation, each processor for which this is called
18: gets the values for its rows using MatGetRow().
20: The vector must have the same parallel row layout as the matrix.
22: Contributed by: Denis Vanderstraeten
24: .seealso: MatGetRow(), MatGetDiagonal(), MatMult()
26: @*/
27: PetscErrorCode MatGetColumnVector(Mat A,Vec yy,PetscInt col)
28: {
29: PetscScalar *y;
30: const PetscScalar *v;
31: PetscErrorCode ierr;
32: PetscInt i,j,nz,N,Rs,Re,rs,re;
33: const PetscInt *idx;
39: if (col < 0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Requested negative column: %D",col);
40: MatGetSize(A,NULL,&N);
41: if (col >= N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Requested column %D larger than number columns in matrix %D",col,N);
42: MatGetOwnershipRange(A,&Rs,&Re);
43: VecGetOwnershipRange(yy,&rs,&re);
44: 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);
46: if (A->ops->getcolumnvector) {
47: (*A->ops->getcolumnvector)(A,yy,col);
48: } else {
49: VecSet(yy,0.0);
50: VecGetArray(yy,&y);
51: /* TODO for general matrices */
52: for (i=Rs; i<Re; i++) {
53: MatGetRow(A,i,&nz,&idx,&v);
54: if (nz && idx[0] <= col) {
55: /*
56: Should use faster search here
57: */
58: for (j=0; j<nz; j++) {
59: if (idx[j] >= col) {
60: if (idx[j] == col) y[i-rs] = v[j];
61: break;
62: }
63: }
64: }
65: MatRestoreRow(A,i,&nz,&idx,&v);
66: }
67: VecRestoreArray(yy,&y);
68: }
69: return(0);
70: }
72: /*@
73: MatGetColumnNorms - Gets the norms of each column of a sparse or dense matrix.
75: Input Parameter:
76: + A - the matrix
77: - type - NORM_2, NORM_1 or NORM_INFINITY
79: Output Parameter:
80: . norms - an array as large as the TOTAL number of columns in the matrix
82: Level: intermediate
84: Notes:
85: Each process has ALL the column norms after the call. Because of the way this is computed each process gets all the values,
86: if each process wants only some of the values it should extract the ones it wants from the array.
88: .seealso: NormType, MatNorm()
90: @*/
91: PetscErrorCode MatGetColumnNorms(Mat A,NormType type,PetscReal norms[])
92: {
97: if (A->ops->getcolumnnorms) {
98: (*A->ops->getcolumnnorms)(A,type,norms);
99: } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Not coded for this matrix type");
100: return(0);
101: }