On Tue, 14 Jul 1998, David Leal Valmana wrote:
> Hi,
>
> I have a function of the form(a simplificated example):
> function evalf(f,x)
> interface
> function f(y) result(f_out)
> real :: y
> real :: f_out
> end interface
> real :: x
> [....]
> end function
>
> How can I use this function evalf inside of a bucle for diferent
> functions f(ex.: f1,f2,f3,f4,f5)
>
> The following doesn't work:
>
> [...]
> integer :: i
> character(len=1) :: num(5)= (/'1','2','3','4','5'/)
> real :: z(5)
> do i=1,5
> z(i)=evalf('f'//num(i),x)
> enddo
>
> [...]
>
> Matlab has the function eval() to do this task of evaluation.
> Is there a similar way to do it under FORTRAN 90?
No. You have to supply the name of the function (or subroutine)
directly. The name of a procedure cannot be treated as a character
variable so the above construction can and will not work.
Fortran 2000 will have procedure pointers which will make life easier
but till then you'll have to use an extra indicator to use something
like this:
do i=1,5
select case(i)
case(1) ; z(i) = evalf(f1,x)
case(2) ; z(i) = evalf(f2,x)
case(3) ; z(i) = evalf(f3,x)
case(4) ; z(i) = evalf(f4,x)
case(5) ; z(i) = evalf(f5,x)
end select
end do
(or instead of i some other integer or character selector.)
Cheers,
WWS
-----------------------------------------------------------------------
| Werner W Schulz |
| Dept of Chemistry email: [log in to unmask] |
| University of Cambridge Phone: (+44) (0)1223 336 502 |
| Lensfield Road Secretary: 1223 336 338 |
| Cambridge CB2 1EW Fax: 1223 336 536 |
| United Kingdom WWW: |
-----------------------------------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|