Actual source code: vecutil.c
1: /*
2: Miscellaneous Vec-related functions.
4: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
5: SLEPc - Scalable Library for Eigenvalue Problem Computations
6: Copyright (c) 2002-2011, Universitat Politecnica de Valencia, Spain
8: This file is part of SLEPc.
9:
10: SLEPc is free software: you can redistribute it and/or modify it under the
11: terms of version 3 of the GNU Lesser General Public License as published by
12: the Free Software Foundation.
14: SLEPc is distributed in the hope that it will be useful, but WITHOUT ANY
15: WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16: FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
17: more details.
19: You should have received a copy of the GNU Lesser General Public License
20: along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
21: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
22: */
24: #include <private/vecimplslepc.h> /*I "slepcvec.h" I*/
25: #include <slepcsys.h>
29: /*@
30: SlepcVecSetRandom - Sets all components of a vector to random numbers.
32: Logically Collective on Vec
34: Input/Output Parameter:
35: . x - the vector
37: Input Parameter:
38: - rctx - the random number context, formed by PetscRandomCreate(), or PETSC_NULL and
39: it will create one internally.
41: Note:
42: This operation is equivalent to VecSetRandom - the difference is that the
43: vector generated by SlepcVecSetRandom is the same irrespective of the size
44: of the communicator (if all processes pass a PetscRandom context initialized
45: with the same seed).
47: Level: developer
48: @*/
49: PetscErrorCode SlepcVecSetRandom(Vec x,PetscRandom rctx)
50: {
52: PetscRandom randObj = PETSC_NULL;
53: PetscInt i,n,low,high;
54: PetscScalar *px,t;
55: MPI_Comm comm;
56:
60: else {
61: PetscObjectGetComm((PetscObject)x,&comm);
62: PetscRandomCreate(comm,&randObj);
63: PetscRandomSetSeed(randObj,0x12345678);
64: PetscRandomSetFromOptions(randObj);
65: rctx = randObj;
66: }
68: VecGetSize(x,&n);
69: VecGetOwnershipRange(x,&low,&high);
70: VecGetArray(x,&px);
71: for (i=0;i<n;i++) {
72: PetscRandomGetValue(rctx,&t);
73: if (i>=low && i<high) px[i-low] = t;
74: }
75: VecRestoreArray(x,&px);
76: PetscRandomDestroy(&randObj);
77: PetscObjectStateIncrease((PetscObject)x);
78: return(0);
79: }
83: /*@C
84: SlepcVecNormalize - Normalizes a possibly complex vector by the 2-norm.
86: Collective on Vec
88: Input parameters:
89: + xr - the real part of the vector (overwritten on output)
90: . xi - the imaginary part of the vector (not referenced if iscomplex is false)
91: - iscomplex - a flag that indicating if the vector is complex
93: Output parameter:
94: . norm - the vector norm before normalization (can be set to PETSC_NULL)
96: Level: developer
98: @*/
99: PetscErrorCode SlepcVecNormalize(Vec xr,Vec xi,PetscBool iscomplex,PetscReal *norm)
100: {
102: #if !defined(PETSC_USE_COMPLEX)
103: PetscReal normr,normi,alpha;
104: #endif
108: #if !defined(PETSC_USE_COMPLEX)
109: if (iscomplex) {
111: VecNormBegin(xr,NORM_2,&normr);
112: VecNormBegin(xi,NORM_2,&normi);
113: VecNormEnd(xr,NORM_2,&normr);
114: VecNormEnd(xi,NORM_2,&normi);
115: alpha = SlepcAbsEigenvalue(normr,normi);
116: if (norm) *norm = alpha;
117: alpha = 1.0 / alpha;
118: VecScale(xr,alpha);
119: VecScale(xi,alpha);
120: } else
121: #endif
122: {
123: VecNormalize(xr,norm);
124: }
125: return(0);
126: }