Skip to content

Commit

Permalink
Fixed segfault when adding OffsetArray and UniformScaling (JuliaLang#…
Browse files Browse the repository at this point in the history
  • Loading branch information
fp4code authored and johanmon committed Jul 5, 2021
1 parent ee74358 commit 25aae62
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
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

0 comments on commit 25aae62

Please sign in to comment.