Fortran 90: dinamic matrix valued function as argument

In summary, the programmer tried to use functions of different dimensions in a program, but ran into problems. They tried using a subroutine to handle the task, but ran into a compiler error.
  • #1
Taff
1
0
hello, I'm trying to write a program with a subroutine that can handle array valued functions of different dimensions, I'm new to fortran 90 so i have some troubles.
Here's what i tried:

program passing
use functions
use sub_mod
implicit none
integer, dimension(2,2):: a2
integer, dimension(3,3):: a3
call prova(a2,g2)
!print*,a2
call prova(a3,g3)
!print*,a3
end program passing

module functions
implicit none
contains
function g2(x)
implicit none
!result
integer,dimension(2,2) :: g2
!dummy
integer x
g2(1,1)=x+2
g2(1,2)=x+2
g2(2,1)=x+2
g2(2,2)=x+2
end function g2

function g3(x)
implicit none
!result
integer,dimension(3,3) :: g3
!dummy
integer x
g3(1,1)=x+3
g3(1,2)=x+3
g3(2,1)=x+3
g3(2,2)=x+3
end function g3

end module functions

module sub_mod
implicit none
contains
subroutine prova(a,f)
!dummy
integer,dimension(:,:) :: a
integer,dimension(size(a,1),size(a,2)) :: f
external f
a=f(0)
end subroutine prova
end module sub_modi got 1 compiler error "can't open module file sub_mod.mod..."
 
Technology news on Phys.org
  • #2
Hi Taff. I'm a pretty infrequent Fortran user, but I think that you have three options with modules

1. Put them ahead of the code that uses them.

2. Put a "prototype" header before the code that uses them. (I think it's the "interface" keyword, but I can't remember the exact syntax).

3. Put them is a separate file.

So the easiest fix for you is to just put them ahead of the main program, Eg

Code:
module functions
implicit none
contains
function g2(x)
implicit none
!result
integer,dimension(2,2) :: g2
!dummy
integer x
g2(1,1)=x+2
g2(1,2)=x+2
g2(2,1)=x+2
g2(2,2)=x+2
end function g2

function g3(x)
implicit none
!result
integer,dimension(3,3) :: g3
!dummy
integer x
g3(1,1)=x+3
g3(1,2)=x+3
g3(2,1)=x+3
g3(2,2)=x+3
end function g3

end module functions

module sub_mod
implicit none
contains
subroutine prova(a,f)
!dummy
integer,dimension(:,:) :: a
integer,dimension(size(a,1),size(a,2)) :: f
external f
a=f(0)
end subroutine prova
end module sub_mod

program passing
use functions
use sub_mod
implicit none
integer, dimension(2,2):: a2
integer, dimension(3,3):: a3
call prova(a2,g2)
!print*,a2
call prova(a3,g3)
!print*,a3
end program passing

BTW. Please note that you can use "code" tag to preserve formatting (indentation). I didn't put any indentation into your code because I just cut and pasted it and am too lazy. But next time you post code, using the code tag will greatly increase the chance that someone will bother reading it. :)
 

Related to Fortran 90: dinamic matrix valued function as argument

1. What is a dynamic matrix valued function in Fortran 90?

A dynamic matrix valued function in Fortran 90 is a function that takes in a variable sized matrix as an argument and returns a matrix of the same size. This allows for more flexibility in the size of the matrix being passed into the function.

2. How is a dynamic matrix valued function declared in Fortran 90?

To declare a dynamic matrix valued function in Fortran 90, the function name must be followed by a pair of parentheses containing the variable matrix size as its argument. For example: "function myFunction(matrix(n))".

3. Can a dynamic matrix valued function be used with fixed sized matrices in Fortran 90?

Yes, a dynamic matrix valued function can also be used with fixed sized matrices in Fortran 90. However, the matrix size specified in the function declaration must match the size of the matrix being passed in as an argument.

4. Does Fortran 90 have built-in functions for working with dynamic matrix valued functions?

No, Fortran 90 does not have built-in functions specifically for working with dynamic matrix valued functions. However, there are various functions and subroutines that can be used for matrix operations, such as "matmul" for matrix multiplication and "transpose" for matrix transposition.

5. Are there any limitations to using dynamic matrix valued functions in Fortran 90?

One potential limitation of using dynamic matrix valued functions in Fortran 90 is that they may not perform as efficiently as fixed sized matrix functions. This is because dynamic matrices require additional memory allocation and deallocation during runtime. It is important to consider the trade-off between flexibility and performance when using dynamic matrix valued functions.

Similar threads

  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
4
Views
745
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
5
Views
7K
Back
Top