Fortran 中 maxloc 的类型冲突

Type conflict of maxloc in Fortran

我有以下 Fortran 程序

PROGRAM main

  IMPLICIT NONE

  INTEGER                          :: i
  INTEGER,dimension(:),allocatable :: x0

  allocate(x0(1:25))
  DO i=1,25
     x0(i)=i
  END DO

  print*,"maxloc de x0 est 25, en effet",maxloc(x0)

  print*,"Cinq fois maxloc(x0)",INT(maxloc(x0))

  print*,"f applique a la fonction",f(maxloc(x0))

  print*,"f1 applique a la fonction",f1(INT(maxloc(x0)))

  CONTAINS

  FUNCTION f(maxlocec)
    IMPLICIT NONE
    !Entrées
    INTEGER,dimension(1) :: maxlocec
    !Sorties
    INTEGER,dimension(1) :: f

    f=maxlocec**2

  END FUNCTION f

  FUNCTION f1(maxlocec)
    IMPLICIT NONE
    !Entrées
    INTEGER :: maxlocec
    !Sorties
    INTEGER :: f1

    f1=maxlocec**2

  END FUNCTION f1

END PROGRAM

当我执行它时,我收到以下错误消息:

print*,"f1 applique a la fonction",f1(INT(maxloc(x0)))
                                        1
Error: Rank mismatch in argument 'maxlocec' at (1) (0 and 1)

我试过 f1(maxloc(x0)) 但它不起作用,所以我认为 f1(INT(maxloc(x0))) 会起作用,但它不起作用。

maxloc 的输出似乎是一个整数,但实际上不是。有什么办法解决?

maxloc 例程不是 return 标量整数,而是在本例中为大小为 1 的一维整数数组。 来自标准:

MAXLOC (ARRAY, DIM [, MASK, KIND, BACK]) or MAXLOC (ARRAY [, MASK, KIND, BACK])

...

Result Characteristics. Integer. If KIND is present, the kind type parameter is that specified by the value of KIND; otherwise the kind type parameter is that of default integer type. If DIM does not appear, the result is an array of rank one and of size equal to the rank of ARRAY; otherwise, the result is of rank n − 1 and shape [d1, d2, . . . , dDIM−1, dDIM+1, . . . , dn], where [d1, d2, . . . , dn] is the shape of ARRAY.

所以在你的情况下你可能需要:

print*,"f1 applique a la fonction",f1(maxloc(x0,1))