From e9b73ec18c6bb5ef2b8d552461368355996bd194 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=B9=E3=83=8E=E3=83=AB?= Date: Tue, 6 Jun 2017 08:13:41 +0800 Subject: [PATCH] fix #22168, `im*I+I` should be shown as `(1+1im)*I` (#22169) * fix #22168, `im*I+I` should be shown as `(1+1im)*I` * add test for `show(UniformScaling)` * check the number of terms in `show(::UniformScaling)` * fix the misuse of `|` --- base/linalg/uniformscaling.jl | 8 +++++++- test/linalg/uniformscaling.jl | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/base/linalg/uniformscaling.jl b/base/linalg/uniformscaling.jl index 81a3ee1e6b29c..bbb1dfa46e793 100644 --- a/base/linalg/uniformscaling.jl +++ b/base/linalg/uniformscaling.jl @@ -31,7 +31,13 @@ eltype(::Type{UniformScaling{T}}) where {T} = T ndims(J::UniformScaling) = 2 getindex(J::UniformScaling, i::Integer,j::Integer) = ifelse(i==j,J.λ,zero(J.λ)) -show(io::IO, J::UniformScaling) = print(io, "$(typeof(J))\n$(J.λ)*I") +function show(io::IO, J::UniformScaling) + s = "$(J.λ)" + if ismatch(r"\w+\s*[\+\-]\s*\w+", s) + s = "($s)" + end + print(io, "$(typeof(J))\n$s*I") +end copy(J::UniformScaling) = UniformScaling(J.λ) transpose(J::UniformScaling) = J diff --git a/test/linalg/uniformscaling.jl b/test/linalg/uniformscaling.jl index 4049a44f79284..f61bdef3f4dad 100644 --- a/test/linalg/uniformscaling.jl +++ b/test/linalg/uniformscaling.jl @@ -38,6 +38,7 @@ end end @test copy(UniformScaling(one(Float64))) == UniformScaling(one(Float64)) +@test sprint(show,UniformScaling(one(Complex128))) == "UniformScaling{Complex{Float64}}\n(1.0 + 0.0im)*I" @test sprint(show,UniformScaling(one(Float32))) == "UniformScaling{Float32}\n1.0*I" λ = complex(randn(),randn())