# [petsc-users] sqrt for a PetscScalar

Matthew Knepley knepley at gmail.com
Wed Apr 7 06:32:38 CDT 2010

```Yes, but do you understand what you mean by

1/sqrt(c)

where c is complex? If so, cast 1 to complex.

Matt

On Wed, Apr 7, 2010 at 1:03 PM, Umut Tabak <u.tabak at tudelft.nl> wrote:

> Matthew Knepley wrote:
>
>>
>>
>> My guess is that you want
>>
>> PetscReal xTMx;
>>
>>
>>
> Dear Matthew,
>
> Thanks for the quick reply, Not sure, here is the code for the related
> part, I used a VecTDot before that so I need PetscScalar I guess.
>
>  PetscErrorCode ierr;
>  Vec x, Bx;
>  int rSz, cSz;
>  PetscScalar xTMx;
>  ierr = MatGetSize(modalMat.getMatrix(), &rSz, &cSz);    CHKERRQ(ierr);
>  // create the vectors to be used
>  ierr = VecCreate(MPI_COMM_SELF, &x);         CHKERRQ(ierr);
>  ierr = VecCreate(MPI_COMM_SELF, &Bx);        CHKERRQ(ierr);
>  ierr = VecSetSizes(x, rSz, PETSC_DECIDE);    CHKERRQ(ierr);
>  ierr = VecSetSizes(Bx, rSz, PETSC_DECIDE);   CHKERRQ(ierr);
>  VecSetFromOptions(x);
>  VecSetFromOptions(Bx);
>  //
>  for(int k=0; k<nev; k++)
>  {
>   // retrive the mode vector
>   MatGetColumnVector(modalMat.getMatrix(), x, k);
>   MatMult(B, x, Bx);
>   VecTDot(Bx, x, &xTMx);
>
>   VecScale(x, 1/sqrt(xTMx));
>
> Best regards,
> Umut
>

--
What most experimenters take for granted before they begin their experiments
is infinitely more interesting than any results to which their experiments