From 797ddbb87aa4a36ce0ea00693801f605fdb88cbc Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Wed, 12 Dec 2018 11:05:56 -0800 Subject: [PATCH] Add compat annotation for NaN handling in (l|r)mul! (#30361) --- stdlib/LinearAlgebra/src/generic.jl | 28 ++++++++++++++++++++++++++-- stdlib/LinearAlgebra/test/generic.jl | 12 ++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/stdlib/LinearAlgebra/src/generic.jl b/stdlib/LinearAlgebra/src/generic.jl index 77de34bb84fb9..cdbffcf2665bc 100644 --- a/stdlib/LinearAlgebra/src/generic.jl +++ b/stdlib/LinearAlgebra/src/generic.jl @@ -31,7 +31,15 @@ mul!(C::AbstractArray, X::AbstractArray, s::Number) = generic_mul!(C, s, X) """ rmul!(A::AbstractArray, b::Number) -Scale an array `A` by a scalar `b` overwriting `A` in-place. +Scale an array `A` by a scalar `b` overwriting `A` in-place. Use +[`lmul!`](@ref) to multiply scalar from left. The scaling operation +respects the semantics of the multiplication [`*`](@ref) between an +element of `A` and `b`. In particular, this also applies to +multiplication involving non-finite numbers such as `NaN` and `±Inf`. + +!!! compat "Julia 1.1" + Prior to Julia 1.1, `NaN` and `±Inf` entries in `A` were treated + inconsistently. # Examples ```jldoctest @@ -44,6 +52,10 @@ julia> rmul!(A, 2) 2×2 Array{Int64,2}: 2 4 6 8 + +julia> rmul!([NaN], 0.0) +1-element Array{Float64,1}: + NaN ``` """ function rmul!(X::AbstractArray, s::Number) @@ -57,7 +69,15 @@ end """ lmul!(a::Number, B::AbstractArray) -Scale an array `B` by a scalar `a` overwriting `B` in-place. +Scale an array `B` by a scalar `a` overwriting `B` in-place. Use +[`rmul!`](@ref) to multiply scalar from right. The scaling operation +respects the semantics of the multiplication [`*`](@ref) between `a` +and an element of `B`. In particular, this also applies to +multiplication involving non-finite numbers such as `NaN` and `±Inf`. + +!!! compat "Julia 1.1" + Prior to Julia 1.1, `NaN` and `±Inf` entries in `B` were treated + inconsistently. # Examples ```jldoctest @@ -70,6 +90,10 @@ julia> lmul!(2, B) 2×2 Array{Int64,2}: 2 4 6 8 + +julia> lmul!(0.0, [Inf]) +1-element Array{Float64,1}: + NaN ``` """ function lmul!(s::Number, X::AbstractArray) diff --git a/stdlib/LinearAlgebra/test/generic.jl b/stdlib/LinearAlgebra/test/generic.jl index 8facc9a94142b..40f8e201ec288 100644 --- a/stdlib/LinearAlgebra/test/generic.jl +++ b/stdlib/LinearAlgebra/test/generic.jl @@ -389,4 +389,16 @@ end @test LinearAlgebra.peakflops() > 0 end +@testset "NaN handling: Issue 28972" begin + @test all(isnan, rmul!([NaN], 0.0)) + @test all(isnan, rmul!(Any[NaN], 0.0)) + @test all(isnan, lmul!(0.0, [NaN])) + @test all(isnan, lmul!(0.0, Any[NaN])) + + @test all(!isnan, rmul!([NaN], false)) + @test all(!isnan, rmul!(Any[NaN], false)) + @test all(!isnan, lmul!(false, [NaN])) + @test all(!isnan, lmul!(false, Any[NaN])) +end + end # module TestGeneric