From b277445355d24a03a8d3f7b87ac8f733c11b78da Mon Sep 17 00:00:00 2001 From: Sakse Dalum Date: Wed, 7 Nov 2018 17:21:19 +0800 Subject: [PATCH 1/2] Add `+/-` for `Number` and `AbstractMatrix` to `LinearAlgebra` --- stdlib/LinearAlgebra/src/uniformscaling.jl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/stdlib/LinearAlgebra/src/uniformscaling.jl b/stdlib/LinearAlgebra/src/uniformscaling.jl index fc082764c6f4f..88ef64c2fe104 100644 --- a/stdlib/LinearAlgebra/src/uniformscaling.jl +++ b/stdlib/LinearAlgebra/src/uniformscaling.jl @@ -97,6 +97,12 @@ isposdef(J::UniformScaling) = isposdef(J.λ) (-)(J::UniformScaling, B::BitArray{2}) = J - Array(B) (-)(A::AbstractMatrix, J::UniformScaling) = A + (-J) +# Promotion of scalar to uniform scaling under addition/subtraction +(+)(a::Number, A::AbstractMatrix) = UniformScaling(a) + A +(+)(A::AbstractMatrix, a::Number) = A + UniformScaling(a) +(-)(a::Number, A::AbstractMatrix) = UniformScaling(a) - A +(-)(A::AbstractMatrix, a::Number) = A - UniformScaling(a) + # Unit{Lower/Upper}Triangular matrices become {Lower/Upper}Triangular under # addition with a UniformScaling for (t1, t2) in ((:UnitUpperTriangular, :UpperTriangular), From 3e27085fac8dea8201fcf0a6086f7e92937b8560 Mon Sep 17 00:00:00 2001 From: Sakse Dalum Date: Wed, 7 Nov 2018 17:25:51 +0800 Subject: [PATCH 2/2] Add tests --- stdlib/LinearAlgebra/test/uniformscaling.jl | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/stdlib/LinearAlgebra/test/uniformscaling.jl b/stdlib/LinearAlgebra/test/uniformscaling.jl index 887162d03dc20..67d7b001f2c9e 100644 --- a/stdlib/LinearAlgebra/test/uniformscaling.jl +++ b/stdlib/LinearAlgebra/test/uniformscaling.jl @@ -64,6 +64,16 @@ end @test UniformScaling(α)/α == UniformScaling(1.0) end +@testset "conversion of Number" begin + α = randn() + J = UniformScaling(α) + A = randn(2, 2) + @test α + A == J + A + @test A + α == A + J + @test α - A == J - A + @test A - α == A - J +end + @testset "det and logdet" begin @test det(I) === true @test det(1.0I) === 1.0