Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add +/- for Number and AbstractMatrix to LinearAlgebra #29951

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions stdlib/LinearAlgebra/src/uniformscaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this could/should be done via the promotion mechanism. Interestingly, that would address the behavior of scaling a matrix by a scalar as well.


# Unit{Lower/Upper}Triangular matrices become {Lower/Upper}Triangular under
# addition with a UniformScaling
for (t1, t2) in ((:UnitUpperTriangular, :UpperTriangular),
Expand Down
10 changes: 10 additions & 0 deletions stdlib/LinearAlgebra/test/uniformscaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down