januka
januka

Reputation: 47

gfortran compiling issue with double complex variable

I've been trying to compile a programme and have run into a simple-looking problem. However, I haven't been able to solve it yet. Any ideas are very much appreciated. The programme looks like the following,

Code:

  subroutine rescal(zy,nvar)

  integer nvar,i, nexponent
  double complex zy(nvar)
  double precision maxabs, scalfac

  maxabs = 0.d0
  do i = 1, nvar
    maxabs = max(zabs(zy),maxabs)
  enddo

maxabs((rank 0)) should store the max of zy and maxabs. Simple, right?

I try to compile this programme with gfortran and getting the following error. I don't understand as to why the compiler is complaining that maxabs should be rank 1 and not rank 0. To my understanding, zabs is the right function to use with a double precision complex number.

gfortran -e -Ofast -ffixed-line-length-none -std=legacy -c -o rescal.o rescal.f
rescal.f:13.8:

maxabs = max(zabs(zy),maxabs)
1
Error: Incompatible ranks 0 and 1 in assignment at (1)
make: *** [rescal.o] Error 1

Upvotes: 1

Views: 232

Answers (1)

francescalus
francescalus

Reputation: 32406

max is an elemental function which, given an array argument returns an array result. Your problem here, then, is that zabs(zy) is an array.

Simply, you are missing selecting an element of zy as you loop through:

do i = 1, nvar
  maxabs = max(zabs(zy(i)),maxabs)  ! Select the i-th element of zy
enddo

That said, a couple of points:

  • you can avoid non-standard specific zabs by using the generic abs;
  • the maxval function is available.

You can instead have

maxabs = MAXVAL(ABS(zy))

Upvotes: 1

Related Questions