I am stuck with an f90 module with user-defined operations

In summary: If you want to do scalar multiplication, you will need to definea type "Scalar" and add a procedure to the interface.
  • #1
gluons
15
0
I am writing some code which involves numerical integration of a function on a 3D grid of points. I am defining the type vector to refer to these points, and I am also trying to extend the intrinsic operator * to include scalar and vector multiplication. However, I cannot compile and I don't know how to fix it.

I am having trouble with and without the module, but I think it would be better to make it work as a module. I am not sure how to go about getting this to work, though. This one includes just scalar multiplication:


module modvec

type vector
real, dimension(3) :: x
end type vector

interface operator(*)
module procedure distributive
end interface

contains

function distributive(a,v) result (w)
real, intent(in) :: a
type(vector), intent(in) :: v
type(vector), intent(out) :: w
do i=1,3
w(i) = a*v(i)
enddo
end function distributive

end module modvec



This does not compile in the main or as a module. What am I doing wrong?
 
Technology news on Phys.org
  • #2
gluons said:
I am writing some code which involves numerical integration of a function on a 3D grid of points. I am defining the type vector to refer to these points, and I am also trying to extend the intrinsic operator * to include scalar and vector multiplication. However, I cannot compile and I don't know how to fix it.

I am having trouble with and without the module, but I think it would be better to make it work as a module. I am not sure how to go about getting this to work, though. This one includes just scalar multiplication:


module modvec

type vector
real, dimension(3) :: x
end type vector

interface operator(*)
module procedure distributive
end interface

contains

function distributive(a,v) result (w)
real, intent(in) :: a
type(vector), intent(in) :: v
type(vector), intent(out) :: w
do i=1,3
w(i) = a*v(i)
enddo
end function distributive

end module modvec



This does not compile in the main or as a module. What am I doing wrong?

You have created an illegal recursive definition of DISTRIBUTIVE
at the line w(i) = a*v(i).
For your (apparent) task, you don't need a type "Vector".
Nor do you need a module procedure to do multiplication.

Ordinary Fortran facilities permit you to multiply a scalar by a vector,
without the need to overlay an operator (*).

If w and v are defined as vectors, then you can write

w = a*v

and the operations are performed on w(1) thru w(3) and v(1) thru v(3).
 

Related to I am stuck with an f90 module with user-defined operations

1. What is an f90 module?

An f90 module is a Fortran 90 programming construct that allows for the organization and encapsulation of program components such as variables, functions, and subroutines. It can be used to create reusable code and enhance the structure and readability of a program.

2. What are user-defined operations?

User-defined operations refer to functions or subroutines that are created by the user to perform specific tasks. These operations can be defined within an f90 module and called upon by other parts of the program.

3. How do I use an f90 module with user-defined operations?

To use an f90 module with user-defined operations, you will first need to declare and define the module in your program. Then, you can call upon the user-defined operations within the module as needed in other parts of the program.

4. Can I modify a pre-existing f90 module with user-defined operations?

Yes, you can modify a pre-existing f90 module with user-defined operations by editing the module's declarations and definitions. However, it is important to keep in mind that any changes made to the module may affect other parts of the program that use it.

5. What are the benefits of using an f90 module with user-defined operations?

Using an f90 module with user-defined operations can lead to more organized and readable code, as well as facilitate the reusability of functions and subroutines. It can also help to avoid naming conflicts and improve the overall efficiency of the program.

Similar threads

  • Programming and Computer Science
Replies
4
Views
2K
Replies
2
Views
963
  • Programming and Computer Science
Replies
5
Views
14K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
9
Views
4K
  • Programming and Computer Science
Replies
5
Views
5K
Replies
2
Views
2K
  • Linear and Abstract Algebra
Replies
2
Views
915
  • Programming and Computer Science
Replies
13
Views
7K
Back
Top