## pointer of array-valued functions

## pointer of array-valued functions

(OP)

Hello everybody,

I am starting this thread because I encounter a problem with the pointers in Fortran.

I want to use a pointer of an array-valued function, but I get a segmentation fault error during execution (compilation is ok with gfortran).

Everything works well when the pointer aims at a function returning a real. But it fails when the function returns an array of reals. I guess there is a problem in the definition of my pointer, but I don't know what to do.

Here is a small code to illustrate my problem :

Any help would be appreciated.

Thanks.

I am starting this thread because I encounter a problem with the pointers in Fortran.

I want to use a pointer of an array-valued function, but I get a segmentation fault error during execution (compilation is ok with gfortran).

Everything works well when the pointer aims at a function returning a real. But it fails when the function returns an array of reals. I guess there is a problem in the definition of my pointer, but I don't know what to do.

Here is a small code to illustrate my problem :

#### CODE --> Fortran

program main implicit none real*8, external, pointer :: p p => f1 print*,p(1.0D0) !this is ok p => f2 print*,p(1.0D0) !I get a segmentation fault contains function f1(x) implicit none real*8 :: f1 real*8, intent(in) :: x f1 = x+2 end function f1 function f2(x) implicit none real*8 :: f2(2) real*8, intent(in) :: x f2(1) = x+1 f2(2) = x-1 end function f2 end program main

Any help would be appreciated.

Thanks.

## RE: pointer of array-valued functions

First, I don't think you can use the same pointer to point to the two functions...if one of them is an array, I would suggest to create another pointer to point to f2, like this:

real*8, pointer :: ap(:)

I called it 'ap' for 'array pointer'

If that alone does not work, I wonder if you should turn your f2 into a pointer itself and THEN return it:

function f2(x)

implicit none

real*8, pointer :: f2(:)

real*8, intent(in) :: x

allocate(f2(2))

f2(1) = x+1

f2(2) = x-1

end function f2

## RE: pointer of array-valued functions

Any pointer in fortran is dereferenced at once. You decalred your pointer to point at a variable of typa real*8. A pointer to an array must be delared as array like

real*8, dimension (:), pointer :: ap

Norbert

The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.

## RE: pointer of array-valued functions

p => f1

result = compute_something(p)

p => f2

res = compute_something(p)

In this case you can rather use functions as arguments of other functions, i.e.:

result = compute_something(f1)

result = compute_something(f2)

## RE: pointer of array-valued functions

Here are my comments to all of your recommendations.

to Salgerman (post #1):

The problem is that I do not to point at a variable, but at a function. Therefore, I cannot declare my pointer with

## CODE --> fortran

## CODE --> fortran

## CODE --> fortran

As a consequence, your second option does not work neither.

to gummibaer :

pointer of functions have been introduced in Fortran 2003. It works well when the functions are returning a single value, but I have some problems when an array is returned.

I finally found how to do this, using 2 different pointers (p and ap) and an explicit interface. Here is my code :

## CODE --> fortran

to Salgerman (post #2):

You are right, this is exactly what I want to do, but I don't know how to declare the type of the function f passed as argument in the function 'compute_something', that returns a real*8 for f1 but an array of size 2 of real*8 for f2. I initially thought it would be easier using pointers of functions, but it appears it is not.

Here is an illustration of what I want to do, and the problem I get when calling the subroutine 'compute_something' with 2 different functions types as argument (f1 returns a real*8, f2 returns an array of real*8) :

## CODE --> fortran

the compilation error for the line "call compute_something(f2)" is :

## CODE --> fortran

ps : I used two modules, m1 and m2, because I get a compilation error when including m1 in the contains part of the main program. I don't undertand why, anyway...

## RE: pointer of array-valued functions

http://www.tek-tips.com/viewthread.cfm?qid=1570169

If you want to use as function argument an function which returns an array, you need to use

interface. For an working example seefuncargs.f95in the last posting with in this thread:http://www.tek-tips.com/viewthread.cfm?qid=1572869

## RE: pointer of array-valued functions

I will look at them with great attention and come back later to tell if it works for my case.

Thank you !

## RE: pointer of array-valued functions

Simply said, I want only one function

my_functionwith 2 arguments and I want to apply the function on scalar arguments (real function and real number) and on vector arguments (vector function and vector vector). This is possible in fortran too - we can implement function overloading suing generic interface.Here is an example

overloading.f95## CODE

You see, I need write the function

my_function_with_scal_argswith scalar arguments and the functionmy_function_with_vec_argswith vector arguments.Then implement the overloading mechanism via generic interface

my_function.Now it compiles and runs:

## CODE

## RE: pointer of array-valued functions

One last question : to avoid the use of the function overloading with the general interface, I was wondering if it was possible to define, in the interface for the function passed as argument, the arguments as assumed-shape arrays. In that way, I define only once my function, that combines both 'my_function_with_scal_args' and 'my_function_with_vec_args', by writing the interface in a way like this one :

## CODE --> fortran

With my (simplified) previous code, it writes :

## CODE --> fortran

unfortunately, if it compiles without problem, I got a segmentation fault error when the line 'print*,f(1.0D0)' in the subroutine 'compute_something' is executed.

Is it possible to do such a thing ? It would be very interesting for me because, with such an approach, I can write a single function that computes, for instance, the Jacobian matrix (size m*n) of a vectorial function for any values of m and n. By using the function overloading and the general interface, I would have to write the m*n functions !

## RE: pointer of array-valued functions

For example look at this thread

http://www.tek-tips.com/viewthread.cfm?qid=1546213

The function

multiply_permutationshas 2 arguments, i.e. arrays without defined dimension and returns allocatable array...## RE: pointer of array-valued functions

http://www.tek-tips.com/viewthread.cfm?qid=1516435

Look at the function

MatrixVector(M, x)in modulevector_functions. It has 2 input argumentsreal, dimension(:,:), intent(in) :: M

real, dimension(:), intent(in) :: x

and returns a vector

real, dimension(n) :: MatrixVector

which size depends on input argument / n=SIZE(x) /

## RE: pointer of array-valued functions

## RE: pointer of array-valued functions

Let me sum up my problem, for the readers of the forum.

I have the code given below. In this code, the function f returns an array (dimension 2) of reals. This function is passed as argument to the subroutine mysub.

What I would like to do is to be able to pass any function that returns an array of dimension n >= 1 to mysub. I tried modifying the line

## CODE --> fortran

## CODE --> fortran

Does anyone know the solution of my problem ? I searched for a long time on the Internet but didn't find any similar thing.

## CODE --> fortran

## RE: pointer of array-valued functions

The code below works for me and uses mysub to take on 2 one-dimensional functions of different lengths.

## CODE

## RE: pointer of array-valued functions

## RE: pointer of array-valued functions

## RE: pointer of array-valued functions

I modified a little bit the program:

Now the function

vf1()takes a vector argument of any dimebnsion and returns the vector of the same dimesion as the input argument.The function

my_function_with_vec_args()accepts now arguments like vf1()overloading2.f95## CODE

The program a vector of length 2 and 5, the function vf1() returns in that cases a vector of length 2 or 5.

## CODE

But I must say you, that what I have done is compiler dependent. It compiles with

gfortran, but it doesn't compile withg95, which doesn't like this declaration:## CODE

## CODE

## RE: pointer of array-valued functions

salgerman, you understood my problem welle, I wanted to take on any one-dimensional function. I am just surprised I get a segmentation fault if I do not declare f2 and f5 as allocatables. Anyway, by declaring them as allocatable it works fine and that is what I will do in the future.

mikrom, I am very impressed by your solution, where it not not necessary do declare the functions with allocatables. I need a bit more time to understand clearly what it does exactly. I tried to reproduce my example with your solution but encountered some errors. I guess I need to reread your code carefully to fully understand it.

anyway, thanks very much to both of you for your help. It is greatly appreciated.

Best regards.