Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29277 Discussions

Elemental Functions of Derived Types

Richard_Gordon
Beginner
737 Views
I couldn't find information on whether it was standards-conforming to have an elemental function with a derived type result. Does anyone know? For example, the following code compiles with no warnings:
Command to compile: ifort /stand:f95 test_derel
[fortran]module mymod
    implicit none
    type mytype
	real(8) :: a
	integer :: i
    end type mytype
contains
    elemental function identity(m1) result(m2)
	implicit none
	type(mytype), intent(in) :: m1
	type(mytype) :: m2
	m2 = m1
    end function identity
end module mymod

program test_derel

    use mymod
    implicit none
    type(mytype), dimension(1:2) :: m1,m2

    m1(1)%a = -1.d0
    m1(2)%a = -2.d0
    m1(1)%i = -1
    m1(2)%i = -2
    m2 = identity(m1)    

    write(*,*) m2(1)%a,m2(1)%i
    write(*,*) m2(2)%a,m2(2)%i

end program[/fortran]
And this code produces
-1.00000000000000 -1
-2.00000000000000 -2
0 Kudos
4 Replies
Steven_L_Intel1
Employee
737 Views
As far as I know, it is standard conforming. What led you to ask this?
0 Kudos
joseph_battelle
Beginner
737 Views
From 12.7.1 of F2003 standard:
C1277 All dummy arguments of an elemental procedure shall be scalar dummy data objects and shall
not have the POINTER or ALLOCATABLE attribute.
C1278 The result variable of an elemental function shall be scalar and shall not have the POINTER orALLOCATABLE attribute.
There are no other constraints in the standard that the scalar be of intrinsic type.
0 Kudos
Richard_Gordon
Beginner
737 Views
Thanks Steve and Joseph.
Steve: For the economics problem I'm working, I have to keep track of a set of statistics that evolves over time. Today's set maps into tomorrow's set, and I wanted to program the mapping somewhat independently of the set of statistics (which may change).
Joseph: Thanks for the part of the standard. I wasn't sure whether a derived type of scalars was itself "scalar". Although I guess since Fortran handles complex scalars that would make sense.
This seems like a powerful feature: you can have an elemental function that in some sense returns an array! Although I am a little nervous using it...
0 Kudos
Steven_L_Intel1
Employee
737 Views
Richard,

I asked because I didn't know why you thought this might be nonstandard.

The whole point of elemental functions is to be able to call them with either a scalar or an array and return the appropriate shape result.
0 Kudos
Reply