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

Click For Summary
SUMMARY

The forum discussion focuses on issues related to defining user-defined operations in Fortran, specifically the multiplication operator (*) for a custom type 'vector'. The user attempts to implement scalar multiplication within a module named 'modvec' but encounters compilation errors due to an illegal recursive definition in the function 'distributive'. The solution provided indicates that Fortran's intrinsic capabilities allow for scalar and vector multiplication without needing to define a custom operator.

PREREQUISITES
  • Understanding of Fortran modules and types
  • Familiarity with operator overloading in Fortran
  • Knowledge of Fortran's intrinsic array operations
  • Basic concepts of numerical integration in programming
NEXT STEPS
  • Explore Fortran's intrinsic array operations for vector manipulation
  • Learn about operator overloading in Fortran 90 and later versions
  • Study examples of numerical integration techniques in Fortran
  • Investigate best practices for defining modules in Fortran
USEFUL FOR

This discussion is beneficial for Fortran developers, particularly those working on numerical methods, module design, and operator overloading. It is also useful for programmers seeking to optimize vector operations in their code.

gluons
Messages
15
Reaction score
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
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).
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
15K
Replies
2
Views
3K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 5 ·
Replies
5
Views
6K
Replies
2
Views
2K
Replies
13
Views
8K
  • · Replies 5 ·
Replies
5
Views
5K