Subversion Repositories slepc-dev

Rev

Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6 dsic.upv.es!jroman 1
 
183 dsic.upv.es!antodo 2
#include "slepc.h" /*I "slepc.h" I*/
572 dsic.upv.es!antodo 3
#include <stdlib.h>
6 dsic.upv.es!jroman 4
 
5
#undef __FUNCT__  
6
#define __FUNCT__ "SlepcVecSetRandom"
7
/*@
8
   SlepcVecSetRandom - Sets all components of a vector to random numbers which
572 dsic.upv.es!antodo 9
   follow a uniform distribution in [0,1).
6 dsic.upv.es!jroman 10
 
11
   Collective on Vec
12
 
13
   Input/Output Parameter:
14
.  x  - the vector
15
 
16
   Note:
17
   This operation is equivalent to VecSetRandom - the difference is that the
18
   vector generated by SlepcVecSetRandom is the same irrespective of the size
19
   of the communicator.
20
 
21
   Level: developer
22
 
23
.seealso: VecSetRandom()
24
@*/
476 dsic.upv.es!antodo 25
PetscErrorCode SlepcVecSetRandom(Vec x)
6 dsic.upv.es!jroman 26
{
476 dsic.upv.es!antodo 27
  PetscErrorCode ierr;
28
  int            i,n,low,high;
572 dsic.upv.es!antodo 29
  PetscScalar    *px,t;
30
  static unsigned short seed[3] = { 1, 3, 2 };
31
 
6 dsic.upv.es!jroman 32
  PetscFunctionBegin;
33
  ierr = VecGetSize(x,&n);CHKERRQ(ierr);
34
  ierr = VecGetOwnershipRange(x,&low,&high);CHKERRQ(ierr);
35
  ierr = VecGetArray(x,&px);CHKERRQ(ierr);
572 dsic.upv.es!antodo 36
  for (i=0;i<n;i++) {
37
    t = erand48(seed);
38
    if (i>=low && i<high) px[i-low] = t;
39
  }
6 dsic.upv.es!jroman 40
  ierr = VecRestoreArray(x,&px);CHKERRQ(ierr);
41
  PetscFunctionReturn(0);
42
}
43
 
44
#undef __FUNCT__  
45
#define __FUNCT__ "SlepcIsHermitian"
46
/*@
47
   SlepcIsHermitian - Checks if a matrix is Hermitian or not.
48
 
49
   Collective on Mat
50
 
51
   Input parameter:
52
.  A  - the matrix
53
 
54
   Output parameter:
55
.  is  - flag indicating if the matrix is Hermitian
56
 
57
   Notes:
58
   The result of Ax and A^Hx (with a random x) is compared, but they
59
   could be equal also for some non-Hermitian matrices.
60
 
61
   This routine will not work with BOPT=O_complex and matrix formats
62
   MATSEQSBAIJ or MATMPISBAIJ.
63
 
64
   Level: developer
65
 
66
@*/
476 dsic.upv.es!antodo 67
PetscErrorCode SlepcIsHermitian(Mat A,PetscTruth *is)
6 dsic.upv.es!jroman 68
{
476 dsic.upv.es!antodo 69
  PetscErrorCode ierr;
70
  int            M,N,m,n;
71
  Vec            x,w1,w2;
72
  PetscScalar    alpha;
73
  MPI_Comm       comm;
74
  PetscReal      norm;
75
  PetscTruth     has;
6 dsic.upv.es!jroman 76
 
77
  PetscFunctionBegin;
78
 
79
#if !defined(PETSC_USE_COMPLEX)
80
  ierr = PetscTypeCompare((PetscObject)A,MATSEQSBAIJ,is);CHKERRQ(ierr);
81
  if (*is) PetscFunctionReturn(0);
82
  ierr = PetscTypeCompare((PetscObject)A,MATMPISBAIJ,is);CHKERRQ(ierr);
83
  if (*is) PetscFunctionReturn(0);
84
#endif
85
 
86
  *is = PETSC_FALSE;
87
  ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
88
  ierr = MatGetLocalSize(A,&m,&n);CHKERRQ(ierr);
89
  if (M!=N) PetscFunctionReturn(0);
90
  ierr = MatHasOperation(A,MATOP_MULT,&has);CHKERRQ(ierr);
91
  if (!has) PetscFunctionReturn(0);
92
  ierr = MatHasOperation(A,MATOP_MULT_TRANSPOSE,&has);CHKERRQ(ierr);
93
  if (!has) PetscFunctionReturn(0);
94
 
95
  ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr);
96
  ierr = VecCreate(comm,&x);CHKERRQ(ierr);
97
  ierr = VecSetSizes(x,n,N);CHKERRQ(ierr);
98
  ierr = VecSetFromOptions(x);CHKERRQ(ierr);
99
  ierr = SlepcVecSetRandom(x);CHKERRQ(ierr);
100
  ierr = VecDuplicate(x,&w1);CHKERRQ(ierr);
101
  ierr = VecDuplicate(x,&w2);CHKERRQ(ierr);
102
  ierr = MatMult(A,x,w1);CHKERRQ(ierr);
103
  ierr = MatMultTranspose(A,x,w2);CHKERRQ(ierr);
104
  ierr = VecConjugate(w2);CHKERRQ(ierr);
105
  alpha = -1.0;
106
  ierr = VecAXPY(&alpha,w1,w2);CHKERRQ(ierr);
107
  ierr = VecNorm(w2,NORM_2,&norm);CHKERRQ(ierr);
108
  if (norm<1.0e-6) *is = PETSC_TRUE;
109
  ierr = VecDestroy(x);CHKERRQ(ierr);
110
  ierr = VecDestroy(w1);CHKERRQ(ierr);
111
  ierr = VecDestroy(w2);CHKERRQ(ierr);
112
 
113
  PetscFunctionReturn(0);
114
}
115
 
502 dsic.upv.es!antodo 116
#if !defined(PETSC_USE_COMPLEX)
491 dsic.upv.es!antodo 117
 
118
#undef __FUNCT__  
502 dsic.upv.es!antodo 119
#define __FUNCT__ "SlepcAbsEigenvalue"
547 dsic.upv.es!jroman 120
/*@
121
   SlepcAbsEigenvalue - Computes the absolute value of a complex number given
122
   its real and imaginary parts.
123
 
124
   Not collective
125
 
126
   Input parameters:
127
+  x  - the real part of the complex number
128
-  y  - the imaginary part of the complex number
129
 
130
   Return value:
131
.  the absolute value of the number
132
 
133
   Notes:
134
   This function computes sqrt(x**2+y**2), taking care not to cause unnecessary
135
   overflow. It is based on LAPACK's DLAPY2.
136
 
137
   Level: developer
138
 
139
@*/
502 dsic.upv.es!antodo 140
PetscReal SlepcAbsEigenvalue(PetscScalar x,PetscScalar y)
491 dsic.upv.es!antodo 141
{
502 dsic.upv.es!antodo 142
  PetscReal xabs,yabs,w,z,t;
491 dsic.upv.es!antodo 143
  PetscFunctionBegin;
502 dsic.upv.es!antodo 144
  xabs = PetscAbsReal(x);
145
  yabs = PetscAbsReal(y);
491 dsic.upv.es!antodo 146
  w = PetscMax(xabs,yabs);
147
  z = PetscMin(xabs,yabs);
502 dsic.upv.es!antodo 148
  if (z == 0.0) PetscFunctionReturn(w);
149
  t = z/w;
150
  PetscFunctionReturn(w*sqrt(1.0+t*t));  
491 dsic.upv.es!antodo 151
}
152
 
153
#endif