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

Scalar arguments in elemental subprograms. Why not?

Sergio
Beginner
606 Views

Hi,

Is it possible to create a function with the same interface as the elemental intrinsic bessel_jn(n, x), where n is always a scalar and x may be an array? It looks like it is not possible. Why not?

In general that would be nice, but it introduces one serious limitation: elemental type-bound procedures. I would like to write something like

type :: Foo
    contains
         procedure :: func => Foo_func
end type

type, extends(Foo) :: Bar
    real :: param
   contains
         procedure :: func => Bar_func
end type

elemental function Foo_func(self, x)
   class(Foo) :: self
   real :: x, Foo_func
   Foo_func=x+1.d0
end function

elemental function Bar_func(self, x)
   class(Bar) :: self
   real :: x, Bar_func
   Bar_func=x*self%param
end function

 But this would not work, because I would need to provide an array of self's.

Did I misunderstood something? Why shouldn't this be allowed?

0 Kudos
1 Solution
IanH
Honored Contributor III
606 Views

You forgot to add the INTENT(IN) attribute to the arguments.

Apart from that, I don't think there's an issue.  The arguments to an elemental function just need to be conformable.  A scalar is conformable with an array.

(Similarly your description of the intrinsic bessel_jn isn't quite right - n doesn't always have to be scalar - it can be an array with the same shape as the other argument.)

View solution in original post

0 Kudos
2 Replies
IanH
Honored Contributor III
607 Views

You forgot to add the INTENT(IN) attribute to the arguments.

Apart from that, I don't think there's an issue.  The arguments to an elemental function just need to be conformable.  A scalar is conformable with an array.

(Similarly your description of the intrinsic bessel_jn isn't quite right - n doesn't always have to be scalar - it can be an array with the same shape as the other argument.)

0 Kudos
Sergio
Beginner
606 Views

Uhm, now I feel extremely stupid. Why didn't I test the code before posting here!!!??? I had some stupid preconceived idea and went for it.

It works like a charm :) Thank you!

0 Kudos
Reply