fortran-lang/stdlib

Make `loc` and `scale` arguments of rvs_normal optional when returning an array

Opened this issue · 6 comments

I wish the loc and scale arguments of rvs_normal were optional when generating a 1D array of variates, so that one could write

x = rvs_normal(array_size=n)

instead of

x = rvs_normal(0.0_dp, 1.0_dp, array_size=n)

That is how numpy.random.normal works.

Has this issue been triaged?

@Beliavsky, I looked into implementing optional parameters, but there's a technical challenge: without loc and scale arguments, I think we cannot determine which type/kind to generate (real(sp), real(dp), real(qp), or complex).

Proposed Solution:

Add a mold parameter to specify the desired type:

x = rvs_normal(mold=0.0_dp, array_size=100)  ! generates real(dp)
y = rvs_normal(mold=0.0_sp, array_size=50)   ! generates real(sp)

This follows Fortran's allocate(mold=...) pattern. The mold argument is only used for type determination and defaults to loc=0, scale=1.

Thoughts? I can prepare a PR if this approach seems reasonable.


cc: @jvdp1

jvdp1 commented

Thank you @gururaj1512 . I think that using mold makes sense. It is already the case in stdlib, for example with eye.
However, I wonder if it does not defeat the purpose of @Beliavsky .

@jvdp1, True, this doesn’t fully match NumPy’s minimal call, but mold reduces the verbosity while keeping under Fortran’s rules. We save one parameter while still achieving standard normal generation. What do you think?

jvdp1 commented

@gururaj1512 , I agree with you.

Thanks for confirming @jvdp1. I’ll proceed with this approach and draft a PR.