|
|
 |
 |
 |
 |
Fortran Programming Language
|
 |
 |
 |
 |
 |
 |
 |
 |
array of functions...
Hi, I looked at the FAQ, but did not see anything in concern with my issue. I want to use an array which is of functions. How can I do this ? I would like to use something like this: program test integer :: i real :: x real :: funcs(6) funcs(1) = a funcs(2) = b funcs(3) = c ... i = 1 x = 0.2 print *, (funcs(i))(x) <-- returns a(x) end program real function a(x) ... real function b(x) ... real function c(x) ... Any suggestion welcome (or a better idea than an array of functions ;-) Thanks in advance. Cheers, -- http://scipy.org/FredericPetit
fred wrote: > I want to use an array which is of functions.
I think you want an array of function pointers, or, in Fortran terms (as of 2003) procedure pointers. I think it is also possible to do through the C interoperability feature as C function pointers. > I would like to use something like this: > program test > integer :: i > real :: x > real :: funcs(6) > funcs(1) = a > funcs(2) = b > funcs(3) = c > i = 1 > x = 0.2 > print *, (funcs(i))(x) <-- returns a(x)
As close as I can tell, reading the F2003 draft: PROCEDURE, POINTER :: funcs(3), func PROCEDURE :: a, b, c funcs(1) => a funcs(2) => b funcs(3) => c i=1 x=0.2 func => funcs(i) print *,func(x) There aren't many examples, especially using arrays. I don't see that you can do the more obvious funcs(i)(x). (Function call isn't an operator, so you can't do the C style (funcs(i))(x) form.) Maybe, like data pointers, you can't have arrays of pointers, but only arrays of structures containing procedure pointers, I believe like arrays of pointers, arrays of procedure pointers would be useful. -- glen
glen herrmannsfeldt <g @ugcs.caltech.edu> wrote: > fred wrote: > > I want to use an array which is of functions. > I think you want an array of function pointers, or, in Fortran > terms (as of 2003) procedure pointers. I think it is also > possible to do through the C interoperability feature as > C function pointers.
Correct on both counts. You can't do anything like that in f95. To the OP, note that in f95 or earlier, you can't have a function variable/pointer/whatever at all (except for the special case of a dummy argument), not even a scalar one. You can't have arrays without first having scalars, insomuch as each element of the array is a scalar. Some people might note that some implementations of so-called Cray pointers allow it. But note that this isn't even portable among Cray pointer implementations. For example, it wasn't in the ones actually done by Cray, so one might consider Cray pointers to functions to be an extension of an extension. Expect portability problems if you use it. It is also possible to hack it via C. I've done so. But it isn't standard. On the whole, however, it is probably more portable than using Cray pointers. It can be made to work on most systems; you just might need to do some usually minor tweaking. There is a perfectly standard way to get the basic functionality, but it is long-winded, awkward, and a maintenance headache. It does work, though. You assign an integer index value to each possible function. Then use a CASE construct to branch to a reference to the appropriate function. You can bury the CASE construct in a wrapper function so that you only hav eto write it once. > Maybe, like data pointers, you can't have arrays of pointers, > but only arrays of structures containing procedure pointers, > I believe like arrays of pointers, arrays of procedure > pointers would be useful.
Correct that you can't have arrays of pointers. Period. It doesn't matter whether they are data or function pointers. I don't think there is any debate about the utility of such a thing as an array of pointers. "Everyone" agrees they would be useful; there just happen to be syntactic problems mentioned many times before. In fact, when procedure pointers were added to the language, there was a camp (which I was in) that wanted them to be described as procedure variables instead of as procedure pointers. One of the specifically cited reasons was that making them variables would have allowed arrays of them. Yes, you can do the usual array of structures trick. It is a bit long-winded, but it does at least have the functionality. -- Richard Maine | Good judgement comes from experience; email: last name at domain . net | experience comes from bad judgement. domain: summertriangle | -- Mark Twain
glen herrmannsfeldt <g@ugcs.caltech.edu> a crit : > As close as I can tell, reading the F2003 draft:
Uh, I use gfortran 4.1.1 ifort 9.1.037 but none seems to be F2003 compliant :-( Or maybe I'm wrong ? I think I have to use C function calls in my subroutine. Thanks to all two. Cheers, -- http://scipy.org/FredericPetit
On 11 mei, 12:25, fred <fredantis@free.fr> wrote: > glen herrmannsfeldt <g @ugcs.caltech.edu> a crit : > > As close as I can tell, reading the F2003 draft: > Uh, I use gfortran 4.1.1 ifort 9.1.037 but none seems to be F2003 > compliant :-( > Or maybe I'm wrong ? > I think I have to use C function calls in my subroutine. > Thanks to all two. > Cheers, > --http://scipy.org/FredericPetit
Just read in the Fortran Forum that g95 _does_ have function pointers (there is a nice overview of a number of compilers versus F2003 features) Regards, Arjen
"Arjen Markus" <arjen.mar @wldelft.nl> wrote in message news:1178880229.301392.65340@w5g2000hsg.googlegroups.com... On 11 mei, 12:25, fred <fredantis@free.fr> wrote: Just read in the Fortran Forum that g95 _does_ have function pointers (there is a nice overview of a number of compilers versus F2003 features) ... and Cray too. Regards, Mike Metcalf
Arjen Markus <arjen.mar@wldelft.nl> a crit : > Just read in the Fortran Forum that g95 _does_ have function pointers > (there is a nice overview of a number of compilers versus F2003 > features)
Oops ! Ok, thanks. -- http://scipy.org/FredericPetit
Richard Maine wrote: >>fred wrote: >>>I want to use an array which is of functions.
(I wrote) >>Maybe, like data pointers, you can't have arrays of pointers, >>but only arrays of structures containing procedure pointers, >>I believe like arrays of pointers, arrays of procedure >>pointers would be useful. > Correct that you can't have arrays of pointers. Period. It doesn't > matter whether they are data or function pointers. > I don't think there is any debate about the utility of such a thing as > an array of pointers. "Everyone" agrees they would be useful; there just > happen to be syntactic problems mentioned many times before.
I suppose I was hoping that the syntactic problems didn't come in this case. In my example I assigned to a scalar pointer to attempt to get around some of the problems... > In fact, when procedure pointers were added to the language, there was a > camp (which I was in) that wanted them to be described as procedure > variables instead of as procedure pointers. One of the specifically > cited reasons was that making them variables would have allowed arrays > of them.
They are considered pointers in C, and variables in PL/I. In C parentheses are used to distinguish a function returning a pointer from a function pointer, with the precedence going to the function returning a pointer. This works in C because function call is an operator. int *x(); /* function returning a pointer to int */ int (*y)(); /* pointer to function returning an int */ int (*(*z)())(); /* pointer to function returning a pointer to a function returning an int */ > Yes, you can do the usual array of structures trick. It is a bit > long-winded, but it does at least have the functionality.
As far as I know, the biggest use of function pointers if the unix I/O system which has an array of struct, one for each type of I/O device. A call to read or write becomes a system call which then calls the appropriate function in the struct for that type of device. -- glen
fred <fredantis @free.fr> wrote: > Arjen Markus <arjen.mar @wldelft.nl> a crit : > > Just read in the Fortran Forum that g95 _does_ have function pointers > > (there is a nice overview of a number of compilers versus F2003 > > features) > Oops ! > Ok, thanks.
The "oops" makes me think that you might have a slight misunderstanding. Although GFortran and g95 are both free compilers, and have some heritage in common, they are different. Details of how this came about have been discussed here before; no need for me to repeat them or the related laments. Anyway, both you are Arjen are probaby right - you that GFortran doesn't have procedure pointers, and Arjen that g95 does have them. (I didn't check to make sure, but it sounds right and is at least plausible). -- Richard Maine | Good judgement comes from experience; email: last name at domain . net | experience comes from bad judgement. domain: summertriangle | -- Mark Twain
glen herrmannsfeldt wrote:
... > As far as I know, the biggest use of function pointers if the > unix I/O system which has an array of struct, one for each > type of I/O device. A call to read or write becomes a system > call which then calls the appropriate function in the struct > for that type of device.
From that description, it sounds like they didn't need function pointers there. A branch table would suffice. You compute an offset into the table and each location in the table is a procedure call to the right device driver. -- J. Giles "I conclude that there are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies." -- C. A. R. Hoare
James Giles wrote: > glen herrmannsfeldt wrote: >>As far as I know, the biggest use of function pointers if the >>unix I/O system which has an array of struct, one for each >>type of I/O device. A call to read or write becomes a system >>call which then calls the appropriate function in the struct >>for that type of device. > From that description, it sounds like they didn't need function > pointers there. A branch table would suffice. You compute > an offset into the table and each location in the table is a > procedure call to the right device driver.
I think it is big enough, and machine memory was small enough, that function pointers were significantly smaller. Written in a high level language, a branch table to function calls generates all the argument passing code for each call, in addition to an extra branch back to the common code. -- glen
nos@see.signature (Richard Maine) a crit : > fred <fredantis @free.fr> wrote: >> Arjen Markus <arjen.mar@wldelft.nl> a crit : >> > Just read in the Fortran Forum that g95 _does_ have function pointers >> > (there is a nice overview of a number of compilers versus F2003 >> > features) >> Oops ! >> Ok, thanks. > The "oops" makes me think that you might have a slight misunderstanding.
I guess you're right. ;-) I was thinking that the answer that Arjen gave me was obvious... By the way, I got a fix that works like a charm: I did it in C (which I know better), and call my C function from my fortran code. Thanks anyway. Cheers, -- http://scipy.org/FredericPetit
In article <87ejlonxu9. @free.fr>, fred <fredantis @free.fr> wrote: >I want to use an array which is of functions. >Any suggestion welcome (or a better idea than an array of functions ;-)
Instead of an array of functions f95 lets you use a function whose value is an array. Problem: if it's called f you can't refer to f(x)(3) or f(3)(x) because of the obvious ambiguity. Solution: put the function value in an array (e.g. temp below) and refer to an element of temp: PROGRAM testarrayfunc ! how to do array-valued function reference? INTEGER temp(5) temp = f(17) PRINT *,'temp(3) = element 3 of f(17) =',temp(3) CONTAINS FUNCTION f(n) INTEGER,INTENT(IN) :: n INTEGER,DIMENSION(5) :: f f = n*(/1,2,3,4,5/) END FUNCTION f END PROGRAM testarrayfunc -- John Harper, School of Mathematics, Statistics and Computer Science, Victoria University, PO Box 600, Wellington 6140, New Zealand e-mail john.har@vuw.ac.nz phone (+64)(4)463 5341 fax (+64)(4)463 5045
har@mcs.vuw.ac.nz (John Harper) a crit : > In article <87ejlonxu9. @free.fr>, fred <fredantis @free.fr> wrote: >>I want to use an array which is of functions. >>Any suggestion welcome (or a better idea than an array of functions ;-) > Instead of an array of functions f95 lets you use a function whose value > is an array. Problem: if it's called f you can't refer to f(x)(3) or > f(3)(x) because of the obvious ambiguity. Solution: put the function > value in an array (e.g. temp below) and refer to an element of temp:
Hi John, I don't think that fits my needs. By the way, I got a fix (and already posted it here) to my problem : call C functions from my fortran code. Works fine. Thanks anyway. Cheers, -- http://scipy.org/FredericPetit
|
 |
 |
 |
 |
|