-
Notifications
You must be signed in to change notification settings - Fork 198
Add default (mold, array_size) overloads for rvs_normal #1056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add default (mold, array_size) overloads for rvs_normal #1056
Conversation
|
@jvdp1, Can you have a look. Needs review...! |
jvdp1
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @gururaj1512 . I have some suggestions below.
| #:for k1, t1 in RC_KINDS_TYPES | ||
| module procedure rvs_norm_array_default_${t1[0]}$${k1}$ !2 dummy variables (mold, array_size) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be included in the previous fypp loop (Line 35).
| #:endfor | ||
| #:for k1, t1 in REAL_KINDS_TYPES | ||
| impure function rvs_norm_array_default_${t1[0]}$${k1}$ (mold, array_size) result(res) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| impure function rvs_norm_array_default_${t1[0]}$${k1}$ (mold, array_size) result(res) | |
| impure function rvs_norm_array_default_${t1[0]}$${k1}$ (array_size, mold) result(res) |
mold is usually the last argument.
| ${t1}$, parameter :: r = 3.442619855899_${k1}$, rr = 1.0_${k1}$/r | ||
| ${t1}$ :: x, y, re | ||
| integer :: hz, iz, i | ||
| if (.not. zig_norm_initialized) call zigset | ||
| do i = 1, array_size | ||
| iz = 0 | ||
| hz = dist_rand(1_int32) | ||
| iz = iand(hz, 127) | ||
| if (abs(hz) < kn(iz)) then | ||
| re = hz*wn(iz) | ||
| else | ||
| L1: do | ||
| L2: if (iz == 0) then | ||
| do | ||
| x = -log(uni(1.0_${k1}$))*rr | ||
| y = -log(uni(1.0_${k1}$)) | ||
| if (y + y >= x*x) exit | ||
| end do | ||
| re = r + x | ||
| if (hz <= 0) re = -re | ||
| exit L1 | ||
| end if L2 | ||
| x = hz*wn(iz) | ||
| if (fn(iz) + uni(1.0_${k1}$)*(fn(iz - 1) - fn(iz)) < & | ||
| exp(-HALF*x*x)) then | ||
| re = x | ||
| exit L1 | ||
| end if | ||
| hz = dist_rand(1_int32) | ||
| iz = iand(hz, 127) | ||
| if (abs(hz) < kn(iz)) then | ||
| re = hz*wn(iz) | ||
| exit L1 | ||
| end if | ||
| end do L1 | ||
| end if | ||
| res(i) = re ! Default: loc=0, scale=1, so re*1 + 0 = re | ||
| end do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| ${t1}$, parameter :: r = 3.442619855899_${k1}$, rr = 1.0_${k1}$/r | |
| ${t1}$ :: x, y, re | |
| integer :: hz, iz, i | |
| if (.not. zig_norm_initialized) call zigset | |
| do i = 1, array_size | |
| iz = 0 | |
| hz = dist_rand(1_int32) | |
| iz = iand(hz, 127) | |
| if (abs(hz) < kn(iz)) then | |
| re = hz*wn(iz) | |
| else | |
| L1: do | |
| L2: if (iz == 0) then | |
| do | |
| x = -log(uni(1.0_${k1}$))*rr | |
| y = -log(uni(1.0_${k1}$)) | |
| if (y + y >= x*x) exit | |
| end do | |
| re = r + x | |
| if (hz <= 0) re = -re | |
| exit L1 | |
| end if L2 | |
| x = hz*wn(iz) | |
| if (fn(iz) + uni(1.0_${k1}$)*(fn(iz - 1) - fn(iz)) < & | |
| exp(-HALF*x*x)) then | |
| re = x | |
| exit L1 | |
| end if | |
| hz = dist_rand(1_int32) | |
| iz = iand(hz, 127) | |
| if (abs(hz) < kn(iz)) then | |
| re = hz*wn(iz) | |
| exit L1 | |
| end if | |
| end do L1 | |
| end if | |
| res(i) = re ! Default: loc=0, scale=1, so re*1 + 0 = re | |
| end do | |
| res = rvs_norm_array_default_${t1[0]}$${k1}$(0._${k1}, 1._${k1}$, array_size) |
I propose to call the procedure, instead of repeating the code, except if there is a good reason to repeat it (I didn't look carefully to the code; if the code needs to be (partly) repeated, the reason should be provided in the comments)..
Resolves #963