I am trying to write a subroutine (to minimize) that has two arguments:
- array of
xany length - function
fthat takes an array of this length and returns a scalar
module example:
module foo
contains
subroutine solve(x, f)
real, dimension(:), intent(inout) :: x
interface
real pure function f(y)
import x
real, dimension(size(x)), intent(in) :: y
end function
end interface
print *, x
print *, f(x)
end subroutine
end module
and test program:
use foo
real, dimension(2) :: x = [1.0, 2.0]
call solve(x, g)
contains
real pure function g(y)
real, dimension(2), intent(in) :: y
g = sum(y)
end function
end
gfortran does not work:
call solve(x, g)
1
Error: Interface mismatch in dummy procedure 'f' at (1): Shape mismatch in dimension 1 of argument 'y'
If I change size(x) => 2, then it compiles (and works) perfectly. It also works great if I change : => 2. But none of these solutions give me what I want.
Any ideas on how I can achieve this?
js947 source
share