Thanks Alvaro,
I tried your code, but unfortunately it won't compile. I fixed the
typos, but it seems that perhaps the sum intrinsic can't be
overloaded. I had previously tried overloading the (+) operator, but I
also tried that again, using the vec3 class/module approach you
suggested, but no luck there either. I also tried adding the elemental
keyword to the "methods"; alas again.
I receive error messages such as
Error: Generic function 'sum' at (1) is not consistent with a
specific intrinsic interface
when overloading the "sum" intrinsic; and
Error: 'array' argument of 'sum' intrinsic at (1) must be a
numeric type
Error: Function 'sum' at (1) has no implicit type
when trying the (+) operator.
Here is the current code, with commenting configured for the "sum"
approach:
------------------------------------------------------------------------------------
module vec3_class
implicit none ! Always. :)
private ! default all contents are private
public :: vec3 ! except the type "wrap" and the interface to the
method
public :: sum
!public :: plus, operator(+)
integer, parameter :: k = 8
type :: vec3
private ! Makes the contents of this type private;
! the name is public, cf above.
real(kind=k), dimension(3) :: es
end type vec3
interface sum ! Name for this method. You can add more
module procedure sum_ ! here as long as they can be disambiguated
end interface ! good old compile-time polymorphism.
!interface operator(+)
! module procedure plus
!end interface
contains
function sum_(this,dim) result(fval)
type(vec3),intent(in) :: this
integer,intent(in) :: dim
real(kind=k) :: fval
! Pass in the contents of the object to the intrinsic.
fval = sum(this%es,dim=dim)
end function sum_
function plus(a,b) result(r)
type(vec3), intent(in) :: a, b
type(vec3) :: r
r%es = a%es + b%es
end function plus
end module vec3_class
program test
use vec3_class ! Bring in the interface and type definition.
implicit none
integer, parameter :: n = 4
type(vec3), dimension(n,n) :: A
type(vec3), dimension(n) :: B
! type(vec3) :: X, Y, Z
! Z = plus(X,Y)
! Z = X + Y