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

AI Thread Summary
The discussion centers on issues with compiling Fortran code that involves defining a vector type and extending the intrinsic multiplication operator for scalar and vector multiplication. The user is attempting to create a module for this purpose but encounters compilation errors. Key points include the identification of an illegal recursive definition in the function "distributive," specifically the line where the multiplication is attempted. It is suggested that the user may not need to define a separate vector type or create a module procedure for multiplication, as Fortran's built-in capabilities allow for direct scalar and vector multiplication without needing to overload the operator. Instead, the user can simply assign the result of the multiplication directly, which would automatically handle the operations on the vector elements.
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).
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top