Hello,
I need some help on code that I wrote a while ago The function below samples from a normal distribution. It is elemental
(and impure
due to the call to random_number
). At least for 1 dimensional arrays x
it seems to does what I expect: x
is sampled from a normal distribution with given mean and standard deviation. What I donāt understand is why real, dimension(2) :: rnd
is ābroadcastedā to something like dimension(2,N)
.
impure elemental subroutine math_normal(x,mu,sigma)
real, intent(out) :: x
real, intent(in), optional :: mu, sigma
real, dimension(2) :: rnd
call random_number(rnd)
x = misc_optional(mu,0.0) &
+ misc_optional(sigma,1.0) * sqrt(-2.0*log(1.0-rnd(1)))*cos(TAU*(1.0 - rnd(2)))
end subroutine math_normal
is this ok? Or should I use the modification below?
impure elemental subroutine math_normal(x,mu,sigma)
real, intent(out) :: x
real, intent(in), optional :: mu, sigma
real :: rnd1, rnd2
call random_number(rnd1)
call random_number(rnd2)
x = misc_optional(mu,0.0) &
+ misc_optional(sigma,1.0) * sqrt(-2.0*log(1.0-rnd1))*cos(TAU*(1.0 - rnd2))
end subroutine math_normal
The general question is then: How are array variables treated in elemental
functions/subroutines?