Elemental 関数 :Fortran

エレメンタル関数(Elemental function) について述べる。

Elemental functionは、 スカラー値を引数にして、スカラー値を返却するスカラー演算子として定義されるが 配列も実際の引数のように扱うことができる関数のことをいう。 すなわち、以下のようないわゆる"Elemental-wise" operation を可能にする

x = [1 、2 、3]

y = [5、 6、 2] w = x+y

result :: w =6 8 5

このような関数は必ずside effect のないpure functionでなくてはならない。 例として以下のコードをあげる

module test
  implicit none
contains
  elemental real function power(x,n)
    real, intent(in) :: x
    integer ,intent(in) ::n 
    power = x**n
  end function
end module

program test_prog
  use test
  implicit none

  real, dimension(3) :: x = (/ 1.0, 2.0, 3.0 /)
  integer::n=3
  print *, power(x,n)
end program