Skip to content
Merged
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
8 changes: 4 additions & 4 deletions stdlib/LinearAlgebra/src/uniformscaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,17 @@ end
function (+)(A::AbstractMatrix, J::UniformScaling)
checksquare(A)
B = copy_oftype(A, Base._return_type(+, Tuple{eltype(A), typeof(J)}))
@inbounds for i in axes(A, 1)
B[i,i] += J
for i in intersect(axes(A,1), axes(A,2))
@inbounds B[i,i] += J
end
return B
end

function (-)(J::UniformScaling, A::AbstractMatrix)
checksquare(A)
B = convert(AbstractMatrix{Base._return_type(+, Tuple{eltype(A), typeof(J)})}, -A)
@inbounds for i in axes(A, 1)
B[i,i] += J
for i in intersect(axes(A,1), axes(A,2))
@inbounds B[i,i] += J
end
return B
end
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 @@ -7,6 +7,8 @@ using Test, LinearAlgebra, Random, SparseArrays
const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
isdefined(Main, :Quaternions) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "Quaternions.jl"))
using .Main.Quaternions
isdefined(Main, :OffsetArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "OffsetArrays.jl"))
using .Main.OffsetArrays

Random.seed!(123)

Expand Down Expand Up @@ -504,4 +506,12 @@ end
end
end

@testset "offset arrays" begin
A = OffsetArray(zeros(4,4), -1:2, 0:3)
@test sum(I + A) ≈ 3.0
@test sum(A + I) ≈ 3.0
@test sum(I - A) ≈ 3.0
@test sum(A - I) ≈ -3.0
end

end # module TestUniformscaling