Skip to content

Commit 7901ee2

Browse files
committed
corr_dev: correction for vector with all elements false
1 parent 2a119c2 commit 7901ee2

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/stdlib_experimental_stats_corr.fypp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ contains
5656
logical, intent(in) :: mask(:)
5757
real(${k1}$) :: res
5858

59+
if (all(.not.mask)) then
60+
res = ieee_value(1._${k1}$, ieee_quiet_nan)
61+
return
62+
end if
63+
5964
res = 1
6065

6166
end function ${RName}$
@@ -70,6 +75,11 @@ contains
7075
logical, intent(in) :: mask(:)
7176
real(dp) :: res
7277

78+
if (all(.not.mask)) then
79+
res = ieee_value(1._dp, ieee_quiet_nan)
80+
return
81+
end if
82+
7383
res = 1
7484

7585
end function ${RName}$

src/tests/stats/test_corr.f90

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,27 +138,28 @@ subroutine test_dp(x, x2)
138138
, 'dp check 2')
139139
call check( abs(corr(x, 1, x == 1.) - 1._dp) < dptol, 'dp check 3')
140140
call check( abs(corr(x, 1, x < 5) - 1._dp) < dptol, 'dp check 4')
141+
call check( ieee_is_nan(corr(x, 1, x < -999)), 'dp check 5')
141142

142143
call check( any(ieee_is_nan(corr(x2, 1, mask = .false.)))&
143-
, 'dp check 5')
144-
call check( any(ieee_is_nan(corr(x2, 2, mask = .false.)))&
145144
, 'dp check 6')
145+
call check( any(ieee_is_nan(corr(x2, 2, mask = .false.)))&
146+
, 'dp check 7')
146147

147148
call check( all( abs( corr(x2, 1) - reshape([&
148149
1._dp, 0.9994439103600_dp, -0.870544389237152_dp, 0.99944391036_dp,&
149150
1._dp, -0.86261576629742_dp, -0.87054438923715_dp, -0.862615766297428_dp,&
150151
1._dp ]&
151152
,[ size(x2, 2), size(x2, 2)])&
152153
) < dptol)&
153-
, 'dp check 7')
154+
, 'dp check 8')
154155
call check( all( abs( corr(x2, 2) - reshape([&
155156
1._dp, 0.998742137866914_dp, 0.999846989517886_dp, -0.998337488459582_dp,&
156157
0.998742137866914_dp, 1._dp, 0.999466429486246_dp, -0.99419162560192020_dp,&
157158
0.999846989517886_dp, 0.999466429486246_dp, 1._dp, -0.99717646495273815_dp,&
158159
-0.998337488459582_dp, -0.994191625601920_dp, -0.997176464952738_dp, 1._dp]&
159160
,[ size(x2, 1), size(x2, 1)])&
160161
) < dptol)&
161-
, 'dp check 8')
162+
, 'dp check 9')
162163
!
163164
! call check( any(ieee_is_nan(cov(x2, 1, mask = x2 < 10)))&
164165
! , 'dp check 12')

0 commit comments

Comments
 (0)