Skip to content

Commit fec8d07

Browse files
authored
Fix vector eltype in crosscov (#805)
* Fix vector eltype in `crosscov` * Add test
1 parent 6768a16 commit fec8d07

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/signalcorr.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ function crosscov!(r::RealVector, x::RealVector, y::RealVector, lags::IntegerVec
255255
T = typeof(zero(eltype(x)) / 1)
256256
zx::Vector{T} = demean ? x .- mean(x) : x
257257
S = typeof(zero(eltype(y)) / 1)
258-
zy::Vector{T} = demean ? y .- mean(y) : y
258+
zy::Vector{S} = demean ? y .- mean(y) : y
259259
for k = 1 : m # foreach lag value
260260
r[k] = _crossdot(zx, zy, lx, lags[k]) / lx
261261
end
@@ -406,7 +406,7 @@ function crosscor!(r::RealVector, x::RealVector, y::RealVector, lags::IntegerVec
406406
T = typeof(zero(eltype(x)) / 1)
407407
zx::Vector{T} = demean ? x .- mean(x) : x
408408
S = typeof(zero(eltype(y)) / 1)
409-
zy::Vector{T} = demean ? y .- mean(y) : y
409+
zy::Vector{S} = demean ? y .- mean(y) : y
410410
sc = sqrt(dot(zx, zx) * dot(zy, zy))
411411
for k = 1 : m # foreach lag value
412412
r[k] = _crossdot(zx, zy, lx, lags[k]) / sc

test/signalcorr.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ c22 = crosscov(x2, x2)
9292
@test crosscov(x, x) cat([c11 c21], [c12 c22], dims=3)
9393
@test crosscov(realx, realx) cat([c11 c21], [c12 c22], dims=3)
9494

95+
# issue #805: avoid converting one input to the other's eltype
96+
@test crosscov([34566.5345, 3466.4566], Float16[1, 10])
97+
crosscov(Float16[1, 10], [34566.5345, 3466.4566])
98+
crosscov([34566.5345, 3466.4566], Float16[1, 10])
99+
95100
rcor0 = [0.230940107675850,
96101
-0.230940107675850,
97102
0.057735026918963,
@@ -118,6 +123,11 @@ c22 = crosscor(x2, x2)
118123
@test crosscor(x, x) cat([c11 c21], [c12 c22], dims=3)
119124
@test crosscor(realx, realx) cat([c11 c21], [c12 c22], dims=3)
120125

126+
# issue #805: avoid converting one input to the other's eltype
127+
@test crosscor([34566.5345, 3466.4566], Float16[1, 10])
128+
crosscor(Float16[1, 10], [34566.5345, 3466.4566])
129+
crosscor([34566.5345, 3466.4566], Float16[1, 10])
130+
121131

122132
## pacf
123133

0 commit comments

Comments
 (0)