Skip to content

Commit e69ce0f

Browse files
tkfKristofferC
authored andcommitted
Normalize index to CartesianIndex in _modify! (#33187)
(cherry picked from commit 6a20ad7)
1 parent 965fc11 commit e69ce0f

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

stdlib/LinearAlgebra/src/generic.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,15 @@ julia> C
6161
"""
6262
@inline @propagate_inbounds function _modify!(p::MulAddMul{ais1, bis0},
6363
x, C, idx′) where {ais1, bis0}
64-
# Workaround for performance penalty of splatting a number (#29114):
65-
idx = idx′ isa Integer ? (idx′,) : idx′
64+
# `idx′` may be an integer, a tuple of integer, or a `CartesianIndex`.
65+
# Let `CartesianIndex` constructor normalize them so that it can be
66+
# used uniformly. It also acts as a workaround for performance penalty
67+
# of splatting a number (#29114):
68+
idx = CartesianIndex(idx′)
6669
if bis0
67-
C[idx...] = p(x)
70+
C[idx] = p(x)
6871
else
69-
C[idx...] = p(x, C[idx...])
72+
C[idx] = p(x, C[idx])
7073
end
7174
return
7275
end

stdlib/LinearAlgebra/test/matmul.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,4 +575,10 @@ end
575575
end
576576
end
577577

578+
@testset "CartesianIndex handling in _modify!" begin
579+
C = rand(10, 10)
580+
A = rand(10, 10)
581+
@test mul!(view(C, 1:10, 1:10), A, 0.5) == A * 0.5
582+
end
583+
578584
end # module TestMatmul

0 commit comments

Comments
 (0)