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.

 46:    Level: developer
 47: @*/
 48: PetscErrorCode SlepcVecSetRandom(Vec x,PetscRandom rctx)
 49: {
 51:   PetscRandom    randObj = PETSC_NULL;
 52:   PetscInt       i,n,low,high;
 53:   PetscScalar    *px,t;
 54:   MPI_Comm       comm;
 55: 
 59:   else {
 60:     PetscObjectGetComm((PetscObject)x,&comm);
 61:     PetscRandomCreate(comm,&randObj);
 62:     PetscRandomSetFromOptions(randObj);
 63:     rctx = randObj;
 64:   }

 66:   VecGetSize(x,&n);
 67:   VecGetOwnershipRange(x,&low,&high);
 68:   VecGetArray(x,&px);
 69:   for (i=0;i<n;i++) {
 70:     PetscRandomGetValue(rctx,&t);
 71:     if (i>=low && i<high) px[i-low] = t;
 72:   }
 73:   VecRestoreArray(x,&px);
 74:   PetscRandomDestroy(&randObj);
 75:   PetscObjectStateIncrease((PetscObject)x);
 76:   return(0);
 77: }

 81: /*@C
 82:    SlepcVecNormalize - Normalizes a possibly complex vector by the 2-norm.

 84:    Collective on Vec

 86:    Input parameters:
 87: +  xr         - the real part of the vector (overwritten on output)
 88: .  xi         - the imaginary part of the vector (not referenced if iscomplex is false)
 89: -  iscomplex - a flag that indicating if the vector is complex

 91:    Output parameter:
 92: .  norm      - the vector norm before normalization (can be set to PETSC_NULL)

 94:    Level: developer

 96: @*/
 97: PetscErrorCode SlepcVecNormalize(Vec xr,Vec xi,PetscBool iscomplex,PetscReal *norm)
 98: {
100: #if !defined(PETSC_USE_COMPLEX)
101:   PetscReal      normr,normi,alpha;
102: #endif

106: #if !defined(PETSC_USE_COMPLEX)
107:   if (iscomplex) {
109:     VecNormBegin(xr,NORM_2,&normr);
110:     VecNormBegin(xi,NORM_2,&normi);
111:     VecNormEnd(xr,NORM_2,&normr);
112:     VecNormEnd(xi,NORM_2,&normi);
113:     alpha = SlepcAbsEigenvalue(normr,normi);
114:     if (norm) *norm = alpha;
115:     alpha = 1.0 / alpha;
116:     VecScale(xr,alpha);
117:     VecScale(xi,alpha);
118:   } else
119: #endif
120:   {
121:     VecNormalize(xr,norm);
122:   }
123:   return(0);
124: }