Actual source code: slepcutil.c

 2:  #include slepc.h

  4: extern PetscRandom rctx;

  8: /*@
  9:    SlepcVecSetRandom - Sets all components of a vector to random numbers which
 10:    follow a uniform distribution in [-1,1].

 12:    Collective on Vec

 14:    Input/Output Parameter:
 15: .  x  - the vector

 17:    Note:
 18:    This operation is equivalent to VecSetRandom - the difference is that the
 19:    vector generated by SlepcVecSetRandom is the same irrespective of the size
 20:    of the communicator.

 22:    Level: developer

 24: .seealso: VecSetRandom()
 25: @*/
 26: int SlepcVecSetRandom(Vec x)
 27: {
 28:   int          ierr,i,n,low,high;
 29:   PetscScalar  *pv,*px;
 30:   Vec          v;

 33:   VecGetSize(x,&n);
 34:   VecGetOwnershipRange(x,&low,&high);
 35:   VecCreateSeq(PETSC_COMM_SELF,n,&v);
 36:   VecSetRandom(rctx,v);
 37:   VecGetArray(v,&pv);
 38:   VecGetArray(x,&px);
 39:   for (i=low;i<high;i++) px[i-low]=pv[i];
 40:   VecRestoreArray(v,&pv);
 41:   VecRestoreArray(x,&px);
 42:   VecDestroy(v);
 43:   return(0);
 44: }

 48: /*@
 49:    SlepcIsHermitian - Checks if a matrix is Hermitian or not.

 51:    Collective on Mat

 53:    Input parameter:
 54: .  A  - the matrix

 56:    Output parameter:
 57: .  is  - flag indicating if the matrix is Hermitian

 59:    Notes: 
 60:    The result of Ax and A^Hx (with a random x) is compared, but they 
 61:    could be equal also for some non-Hermitian matrices.

 63:    This routine will not work with BOPT=O_complex and matrix formats
 64:    MATSEQSBAIJ or MATMPISBAIJ.
 65:    
 66:    Level: developer

 68: @*/
 69: int SlepcIsHermitian(Mat A,PetscTruth *is)
 70: {
 71:   int         M,N,m,n,ierr;
 72:   Vec         x,w1,w2;
 73:   PetscScalar alpha;
 74:   MPI_Comm    comm;
 75:   PetscReal   norm;
 76:   PetscTruth  has;


 80: #if !defined(PETSC_USE_COMPLEX)
 81:   PetscTypeCompare((PetscObject)A,MATSEQSBAIJ,is);
 82:   if (*is) return(0);
 83:   PetscTypeCompare((PetscObject)A,MATMPISBAIJ,is);
 84:   if (*is) return(0);
 85: #endif

 87:   *is = PETSC_FALSE;
 88:   MatGetSize(A,&M,&N);
 89:   MatGetLocalSize(A,&m,&n);
 90:   if (M!=N) return(0);
 91:   MatHasOperation(A,MATOP_MULT,&has);
 92:   if (!has) return(0);
 93:   MatHasOperation(A,MATOP_MULT_TRANSPOSE,&has);
 94:   if (!has) return(0);

 96:   PetscObjectGetComm((PetscObject)A,&comm);
 97:   VecCreate(comm,&x);
 98:   VecSetSizes(x,n,N);
 99:   VecSetFromOptions(x);
100:   SlepcVecSetRandom(x);
101:   VecDuplicate(x,&w1);
102:   VecDuplicate(x,&w2);
103:   MatMult(A,x,w1);
104:   MatMultTranspose(A,x,w2);
105:   VecConjugate(w2);
106:   alpha = -1.0;
107:   VecAXPY(&alpha,w1,w2);
108:   VecNorm(w2,NORM_2,&norm);
109:   if (norm<1.0e-6) *is = PETSC_TRUE;
110:   VecDestroy(x);
111:   VecDestroy(w1);
112:   VecDestroy(w2);

114:   return(0);
115: }