Skip to content

Commit 04fe63a

Browse files
committed
refactor: update implementation as suggested
1 parent bb56acb commit 04fe63a

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

doc/specs/stdlib_stats_distribution_normal.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ With two arguments, the function returns a normal distributed random variate \(N
2323

2424
With three arguments, the function returns a rank-1 array of normal distributed random variates.
2525

26-
With two arguments `array_size` and `mold`, the function returns a rank-1 array of standard normal distributed random variates \(N(0,1)\), where the `mold` argument is used only to determine the output type and kind.
26+
With one or two arguments where the first is `array_size`, the function returns a rank-1 array of standard normal distributed random variates \(N(0,1)\). The `mold` argument determines the output type and kind; it is optional only for `real(dp)` (and defaults to `real(dp)` when omitted), but required for all other types.
2727

2828
@note
2929
The algorithm used for generating exponential random variates is fundamentally limited to double precision.[^1]
@@ -32,7 +32,7 @@ The algorithm used for generating exponential random variates is fundamentally l
3232

3333
`result = ` [[stdlib_stats_distribution_normal(module):rvs_normal(interface)]] `([loc, scale] [[, array_size]])`
3434

35-
`result = ` [[stdlib_stats_distribution_normal(module):rvs_normal(interface)]] `(array_size, mold)`
35+
`result = ` [[stdlib_stats_distribution_normal(module):rvs_normal(interface)]] `(array_size [, mold])`
3636

3737
### Class
3838

@@ -44,15 +44,15 @@ Elemental function (passing both `loc` and `scale`).
4444

4545
`scale`: optional argument has `intent(in)` and is a positive scalar of type `real` or `complex`.
4646

47-
`array_size`: optional argument has `intent(in)` and is a scalar of type `integer`. When used with `loc` and `scale`, specifies the size of the output array. When used with `mold`, must be provided as the first argument.
47+
`array_size`: optional argument has `intent(in)` and is a scalar of type `integer`. When used with `loc` and `scale`, specifies the size of the output array. When used alone or with `mold`, must be provided as the first argument.
4848

49-
`mold`: optional argument has `intent(in)` and is a scalar of type `real` or `complex`. Used only to determine the type and kind of the output; its value is not referenced. When provided, generates standard normal variates \(N(0,1)\).
49+
`mold`: optional argument (only for `real(dp)`; required for other types) has `intent(in)` and is a scalar of type `real` or `complex`. Used only to determine the type and kind of the output; its value is not referenced. When omitted (only allowed for `real(dp)`), defaults to `real(dp)`. When provided, generates standard normal variates \(N(0,1)\) of the specified type and kind.
5050

5151
`loc` and `scale` arguments must be of the same type.
5252

5353
### Return value
5454

55-
The result is a scalar or rank-1 array, with a size of `array_size`, and the same type as `scale` and `loc` (or same type and kind as `mold` when using the `(array_size, mold)` form). If `scale` is non-positive, the result is `NaN`.
55+
The result is a scalar or rank-1 array, with a size of `array_size`, and the same type as `scale` and `loc` (or same type and kind as `mold` when using the `array_size [, mold]` form; defaults to `real(dp)` when `mold` is omitted). If `scale` is non-positive, the result is `NaN`.
5656

5757
### Example
5858

src/stdlib_stats_distribution_normal.fypp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module stdlib_stats_distribution_normal
3333

3434
#:for k1, t1 in RC_KINDS_TYPES
3535
module procedure rvs_norm_array_${t1[0]}$${k1}$ !3 dummy variables
36-
module procedure rvs_norm_array_default_${t1[0]}$${k1}$ !2 dummy variables (mold, array_size)
36+
module procedure rvs_norm_array_default_${t1[0]}$${k1}$ !array_size, mold (mold optional for real(dp) only)
3737
#:endfor
3838
end interface rvs_normal
3939

@@ -246,7 +246,7 @@ contains
246246
! The mold argument is used only to determine the type and is not referenced
247247
!
248248
integer, intent(in) :: array_size
249-
${t1}$, intent(in) :: mold
249+
${t1}$, intent(in) #{if t1 == 'real(dp)'}#, optional #{endif}#:: mold
250250
${t1}$ :: res(array_size)
251251

252252
res = rvs_norm_array_${t1[0]}$${k1}$ (0.0_${k1}$, 1.0_${k1}$, array_size)

test/stats/test_distribution_normal.fypp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,19 @@ contains
164164
call random_seed(seed, get)
165165

166166
! default mold form: mold used only to disambiguate kind
167-
#:if t1[0] == "r"
168-
mold = 0.0_${k1}$
167+
! For real(dp), mold is optional; for other types (including complex), it's required
168+
#:if t1[0] == "r" and k1 == "dp"
169+
a2 = nor_rvs(10) ! mold optional for rdp only, defaults to real(dp)
169170
#:else
170-
mold = (0.0_${k1}$, 0.0_${k1}$)
171+
#! mold required for all other types including complex and non-dp kinds
172+
#:if t1[0] == "r"
173+
mold = 0.0_${k1}$
174+
#:else
175+
mold = (0.0_${k1}$, 0.0_${k1}$)
176+
#:endif
177+
a2 = nor_rvs(10, mold)
171178
#:endif
172179

173-
a2 = nor_rvs(10, mold)
174-
175180
call check(all(a1 == a2), msg="normal_distribution_rvs_default_${t1[0]}$${k1}$ failed", warn=warn)
176181
end subroutine test_nor_rvs_default_${t1[0]}$${k1}$
177182

0 commit comments

Comments
 (0)