Skip to content

Commit 797ddbb

Browse files
tkffredrikekre
authored andcommitted
Add compat annotation for NaN handling in (l|r)mul! (#30361)
1 parent 8965a81 commit 797ddbb

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

stdlib/LinearAlgebra/src/generic.jl

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@ mul!(C::AbstractArray, X::AbstractArray, s::Number) = generic_mul!(C, s, X)
3131
"""
3232
rmul!(A::AbstractArray, b::Number)
3333
34-
Scale an array `A` by a scalar `b` overwriting `A` in-place.
34+
Scale an array `A` by a scalar `b` overwriting `A` in-place. Use
35+
[`lmul!`](@ref) to multiply scalar from left. The scaling operation
36+
respects the semantics of the multiplication [`*`](@ref) between an
37+
element of `A` and `b`. In particular, this also applies to
38+
multiplication involving non-finite numbers such as `NaN` and `±Inf`.
39+
40+
!!! compat "Julia 1.1"
41+
Prior to Julia 1.1, `NaN` and `±Inf` entries in `A` were treated
42+
inconsistently.
3543
3644
# Examples
3745
```jldoctest
@@ -44,6 +52,10 @@ julia> rmul!(A, 2)
4452
2×2 Array{Int64,2}:
4553
2 4
4654
6 8
55+
56+
julia> rmul!([NaN], 0.0)
57+
1-element Array{Float64,1}:
58+
NaN
4759
```
4860
"""
4961
function rmul!(X::AbstractArray, s::Number)
@@ -57,7 +69,15 @@ end
5769
"""
5870
lmul!(a::Number, B::AbstractArray)
5971
60-
Scale an array `B` by a scalar `a` overwriting `B` in-place.
72+
Scale an array `B` by a scalar `a` overwriting `B` in-place. Use
73+
[`rmul!`](@ref) to multiply scalar from right. The scaling operation
74+
respects the semantics of the multiplication [`*`](@ref) between `a`
75+
and an element of `B`. In particular, this also applies to
76+
multiplication involving non-finite numbers such as `NaN` and `±Inf`.
77+
78+
!!! compat "Julia 1.1"
79+
Prior to Julia 1.1, `NaN` and `±Inf` entries in `B` were treated
80+
inconsistently.
6181
6282
# Examples
6383
```jldoctest
@@ -70,6 +90,10 @@ julia> lmul!(2, B)
7090
2×2 Array{Int64,2}:
7191
2 4
7292
6 8
93+
94+
julia> lmul!(0.0, [Inf])
95+
1-element Array{Float64,1}:
96+
NaN
7397
```
7498
"""
7599
function lmul!(s::Number, X::AbstractArray)

stdlib/LinearAlgebra/test/generic.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,4 +389,16 @@ end
389389
@test LinearAlgebra.peakflops() > 0
390390
end
391391

392+
@testset "NaN handling: Issue 28972" begin
393+
@test all(isnan, rmul!([NaN], 0.0))
394+
@test all(isnan, rmul!(Any[NaN], 0.0))
395+
@test all(isnan, lmul!(0.0, [NaN]))
396+
@test all(isnan, lmul!(0.0, Any[NaN]))
397+
398+
@test all(!isnan, rmul!([NaN], false))
399+
@test all(!isnan, rmul!(Any[NaN], false))
400+
@test all(!isnan, lmul!(false, [NaN]))
401+
@test all(!isnan, lmul!(false, Any[NaN]))
402+
end
403+
392404
end # module TestGeneric

0 commit comments

Comments
 (0)