Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Issues with MAXLOC function

378 views
Skip to first unread message

nik@cabana

unread,
Sep 5, 2015, 10:45:27 PM9/5/15
to
Hi all,

I am beginner in Frotran. I am trying to check maxloc function on Arrays using following simple program:

program Arrays
IMPLICIT NONE

integer :: i
integer :: d(10)
d=(/5,6,8,9,4,6,7,9,10,3/)
i=maxloc(d)
write(*,*) i

end program Arrays

On compilation, it gives me an error that The shapes of the Array expressions do not conform. [I] Line 7

My aim is to just determine location of maximum value in the vector d. Can somebody help me with this?

Thanks in advance,

Nik

Tim Prince

unread,
Sep 5, 2015, 11:05:58 PM9/5/15
to
You probably want the usual maxloc(d,dim=1). Without the (f95 and
later) dim argument, the result is returned in an array:
integer i(1)

Jos Bergervoet

unread,
Sep 6, 2015, 4:07:23 AM9/6/15
to
On 9/6/2015 5:05 AM, Tim Prince wrote:
> On 9/5/2015 10:45 PM, nik@cabana wrote:
>
>>
>> I am beginner in Frotran. I am trying to check maxloc function on Arrays using following simple program:
>>
>> program Arrays
>> IMPLICIT NONE
>>
>> integer :: i
>> integer :: d(10)
>> d=(/5,6,8,9,4,6,7,9,10,3/)

It looks better if you write:
d = [5,6,8,9,4,6,7,9,10,3]
Using (/ and /) is only needed if your punch cards
cannot deal with the [ and ] characters.

>> i=maxloc(d)
>> write(*,*) i
>>
>> end program Arrays
>>
>> On compilation, it gives me an error that The shapes of the Array expressions do not conform. [I] Line 7
>>
>> My aim is to just determine location of maximum value in the vector d. Can somebody help me with this?
>>
>
>>
> You probably want the usual maxloc(d,dim=1). Without the (f95 and
> later) dim argument, the result is returned in an array:
> integer i(1)

And before f95 (and even later) we used this trick:

i = max(maxloc(d))

which looks puzzling, but is just the way to pick the
single element out of the array. Exactly the same can
be done like this: (but with even more confusion)

i = min(maxloc(d))

i = maxval(maxloc(d))

Fortran does not allow to write what is actually meant:

i = maxloc(d)(1)

(NB: I forgot why exactly this is disallowed, it must
have something to do with the possibility of ambiguous
syntax resulting.. If something is an array, why can't
we just subscript it?)

--
Jos








James Van Buskirk

unread,
Sep 6, 2015, 5:42:09 PM9/6/15
to
"Jos Bergervoet" wrote in message
news:55ebf438$0$23788$e4fe...@news.xs4all.nl...

> And before f95 (and even later) we used this trick:

> i = max(maxloc(d))

And compilers still reject this syntax. MAX is an elemental function
with two non-optional arguments. Even if you made MAX happy with
a second argument, its result would still be an array.

> i = maxval(maxloc(d))

Right. This one works. Also sum(maxloc(d)). Or transfer(maxloc(d),1).

Jos Bergervoet

unread,
Sep 7, 2015, 2:36:35 AM9/7/15
to
On 9/6/2015 11:41 PM, James Van Buskirk wrote:
> "Jos Bergervoet" wrote in message
> news:55ebf438$0$23788$e4fe...@news.xs4all.nl...
>
>> And before f95 (and even later) we used this trick:
>
>> i = max(maxloc(d))
>
> And compilers still reject this syntax.

You're right, but it's about the idea! You need some
function to select the (only) element. One could
actually overload max to accept the array and return
a scalar and then use the above syntax.

> MAX is an elemental function
> with two non-optional arguments. Even if you made MAX happy with
> a second argument, its result would still be an array.

Still, writing a user-defined max to become a duplicate
of maxval when acting on arrays is possible (in fact,
with the arrival of f90 it was a bit surprising to see
the pair maxval and maxloc, instead of keeping the well-
known max for the former).

>> i = maxval(maxloc(d))
>
> Right. This one works. Also sum(maxloc(d)). Or transfer(maxloc(d),1).

All of them slightly puzzling to the casual reader who
isn't aware of the technicalities. I still think the
clearest syntax would be maxloc(d)(1) or maxloc(d),
both of which are of course not possible.

Which shows how difficult it is to retain the "obvious
meaning" of things when writing code (or when doing
language design..)

--
Jos

Ian Harvey

unread,
Sep 7, 2015, 5:57:43 AM9/7/15
to
On 2015-09-07 4:36 PM, Jos Bergervoet wrote:
> On 9/6/2015 11:41 PM, James Van Buskirk wrote:
>> "Jos Bergervoet" wrote in message
...
>>> i = maxval(maxloc(d))
>>
>> Right. This one works. Also sum(maxloc(d)). Or transfer(maxloc(d),1).
>
> All of them slightly puzzling to the casual reader who
> isn't aware of the technicalities. I still think the
> clearest syntax would be maxloc(d)(1) or maxloc(d),
> both of which are of course not possible.

i = .GetTheFirstElementOf. maxloc(d)

0 new messages