What exactly are scalar values in Fortran, and how can I convert from and to them

Viewed 559

Some context. I have this piece of code :

function areeq(array1,array2) result(eq)
real :: array1(1:100,1:100), array2(1:100,1:100)
logical :: eq
integer :: x,y,f
do x=1,100
  do y = 1,100
    print *,array1(x:x,y:y)
    print *,array2(x:x,y:y)
    if(.not.(array1(x:x,y:y) == array2(x:x,y:y))) then
      eq = .false.
      return
    end if

    read *,f
  end do
end do
eq = .true.
return
end function

However, when I try to run it, it throws this error message:

if(.not.(array1(x:x,y:y) == array2(x:x,y:y))) then
       1
Error: IF clause at (1) requires a scalar LOGICAL expression

This is the second time that I've encountered trouble with something needing to be Scalar, and though I managed to hack together a makeshift work around for the last time, I really ought to, and need to, be able to handle them properly.

So, TL;DR: What is wrong with this piece of code, and what should I do in situations like this more generally?

2 Answers

Given

integer n
real x(5)

then, given appropriate definition of n

x(n)

is an array element of x, and

x(n:n)

is an array section of x.

The array element is a scalar whereas the array section is itself an array of size 1.

As Steve Lionel says, in the case of the question,

array1(x:x,y:y) == array2(x:x,y:y)

is an array-valued expression (albeit again of size 1) which can be reduced to a scalar expression with ALL. However

array1(x,y) == array2(x,y)

is a scalar expression, with both operands scalar array elements.


In the reference x(n) we have an array element for scalar n. With n an array we would instead have an array being a vector subscript of x.

What is wrong is, as the compiler complains, your expression has an array result, with one element for each comparison. What you want is to wrap the expression in ALL(). For example:

if(.not.(all(array1(x:x,y:y) == array2(x:x,y:y)))) then
Related