diff --git a/stdlib/LinearAlgebra/src/uniformscaling.jl b/stdlib/LinearAlgebra/src/uniformscaling.jl index c59871e0641ef..13acea982e7e0 100644 --- a/stdlib/LinearAlgebra/src/uniformscaling.jl +++ b/stdlib/LinearAlgebra/src/uniformscaling.jl @@ -215,8 +215,8 @@ 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 @@ -224,8 +224,8 @@ 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 diff --git a/stdlib/LinearAlgebra/test/uniformscaling.jl b/stdlib/LinearAlgebra/test/uniformscaling.jl index 27f6641657e81..0f86f0555cba7 100644 --- a/stdlib/LinearAlgebra/test/uniformscaling.jl +++ b/stdlib/LinearAlgebra/test/uniformscaling.jl @@ -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) @@ -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