fix implicit conversion of range to array#197
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #197 +/- ##
==========================================
- Coverage 96.66% 96.43% -0.24%
==========================================
Files 2 2
Lines 450 449 -1
==========================================
- Hits 435 433 -2
- Misses 15 16 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@nalimilan would you mind reviewing this? |
Co-authored-by: Milan Bouchet-Valat <nalimilan@club.fr>
Co-authored-by: Milan Bouchet-Valat <nalimilan@club.fr>
I just looked this up and actually julia> LinRange(0.0im, 1.0im + 1.0, 10)
10-element LinRange{ComplexF64, Int64}:
0.0+0.0im, 0.111111+0.111111im, 0.222222+0.222222im, …, 0.888889+0.888889im, 1.0+1.0im
julia> typeof(LinRange(0.0im, 1.0im + 1.0, 10)) <: AbstractRange
trueI'll switch back to |
|
Let me know if you'd prefer this: function _var(A::AbstractArray, corrected::Bool, mean, dims::Dims) where Dims
if mean === nothing
mean = dims === (:) ? Statistics.mean(A) : Statistics.mean(A, dims=dims)
end
v = if dims === (:)
varm(A, mean; corrected=corrected)
else
varm(A, mean; corrected=corrected, dims=dims)
end
return dims === (:) ? real(v) : v
endor keep it split as-is |
|
What I would do is this: function _var(A::AbstractArray, corrected::Bool, mean, dims::Colon)
if mean === nothing
mean = Statistics.mean(A)
end
return varm(A, mean; corrected=corrected)
end
function _var(A::AbstractArray, corrected::Bool, mean, dims::Dims) where Dims
if mean === nothing
mean = Statistics.mean(A, dims=dims)
end
return real(varm(A, mean; corrected=corrected, dims=dims))
endEven if that's a bit verbose, the advantage is that it's clear that it calls |
src/Statistics.jl
Outdated
| l = length(v) | ||
| vv = f^2 * l / (l - 1) + f * s * l + s^2 * l * (2 * l - 1) / 6 | ||
| if l == 0 || l == 1 | ||
| return typeof(vv)(NaN) | ||
| T = typeof((abs2(f) + abs2(s) + realXcY(f, s)) / 2) | ||
| return T(NaN) | ||
| end |
There was a problem hiding this comment.
Why change this? The current method has the advantage that we always return a value of the type of vv, and it's simpler.
There was a problem hiding this comment.
It doesn't change the behavior; just avoids redundant calculations
There was a problem hiding this comment.
I mean, why not keep return typeof(vv)(NaN)? We don't care about redundant calculations in that case. Also, I suspect the compiler is able to eliminate any of these.
There was a problem hiding this comment.
vv was moved to underneath this branch now to avoid the redundant calculation, since all we want here is the return type. I can revert it if you want though
There was a problem hiding this comment.
Yeah we don't care about performance in this corner case, and the compiler will probably be able to eliminate all of these.
|
Mmm but maybe we don't even need these functions, thanks to the already existing |
|
Yeah we've gone full circle; this is the current code, untouched by this PR function _var(A::AbstractArray, corrected::Bool, mean, dims)
if mean === nothing
mean = Statistics.mean(A, dims=dims)
end
return varm(A, mean; corrected=corrected, dims=dims)
end
function _var(A::AbstractArray, corrected::Bool, mean, ::Colon)
if mean === nothing
mean = Statistics.mean(A)
end
return real(varm(A, mean; corrected=corrected))
endAdding |
|
Sorry at first I thought the methods you proposed used |
|
I am not sure I understand |
|
I mean just remove |
|
Maybe this refactoring could be done in the future? I only wanted to fix the one issue I happened upon, I hadn't planned to spend this much time on it. |
|
But that's not a refactoring, it's just removing a function. Or do you mean that doesn't work? What you've done on |
|
Well if we fully remove the |
|
But we can still use if |
Co-authored-by: Milan Bouchet-Valat <nalimilan@club.fr>
|
Fixed! |
Co-authored-by: Milan Bouchet-Valat <nalimilan@club.fr>
|
Implemented |
Co-authored-by: Milan Bouchet-Valat <nalimilan@club.fr>
Co-authored-by: Milan Bouchet-Valat <nalimilan@club.fr>
Co-authored-by: Milan Bouchet-Valat <nalimilan@club.fr>
|
Ok thanks updated |
|
Thanks! So now we get more features almost without more code! |
stdis inadvertently reroutingstd(::AbstractRange)throughvar(::AbstractArray)rather thanvar(::AbstractRange), since the latter does not support keywords, and the bug JuliaLang/julia#9498 exists (patched in JuliaLang/julia#60499, which led me to spot this downstream effect)For example:
This PR fixes this so that
std(::AbstractRange)now usesvar(::AbstractRange)