Skip to content

Commit b69fc57

Browse files
authored
Backport "Avoid concatenating LazyString in setindex! for triangular matrices #54631" to v1.11 (#54742)
1 parent d07a863 commit b69fc57

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

stdlib/LinearAlgebra/src/triangular.jl

+30-16
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,24 @@ Base.isstored(A::UpperTriangular, i::Int, j::Int) =
269269
@propagate_inbounds getindex(A::UpperTriangular, i::Integer, j::Integer) =
270270
i <= j ? A.data[i,j] : _zero(A.data,j,i)
271271

272+
_zero_triangular_half_str(::Type{<:UpperOrUnitUpperTriangular}) = "lower"
273+
_zero_triangular_half_str(::Type{<:LowerOrUnitLowerTriangular}) = "upper"
274+
275+
@noinline function throw_nonzeroerror(T, @nospecialize(x), i, j)
276+
Ts = _zero_triangular_half_str(T)
277+
Tn = nameof(T)
278+
throw(ArgumentError(
279+
lazy"cannot set index in the $Ts triangular part ($i, $j) of an $Tn matrix to a nonzero value ($x)"))
280+
end
281+
@noinline function throw_nononeerror(T, @nospecialize(x), i, j)
282+
Tn = nameof(T)
283+
throw(ArgumentError(
284+
lazy"cannot set index on the diagonal ($i, $j) of an $Tn matrix to a non-unit value ($x)"))
285+
end
286+
272287
@propagate_inbounds function setindex!(A::UpperTriangular, x, i::Integer, j::Integer)
273288
if i > j
274-
iszero(x) || throw(ArgumentError("cannot set index in the lower triangular part " *
275-
lazy"($i, $j) of an UpperTriangular matrix to a nonzero value ($x)"))
289+
iszero(x) || throw_nonzeroerror(typeof(A), x, i, j)
276290
else
277291
A.data[i,j] = x
278292
end
@@ -281,11 +295,9 @@ end
281295

282296
@propagate_inbounds function setindex!(A::UnitUpperTriangular, x, i::Integer, j::Integer)
283297
if i > j
284-
iszero(x) || throw(ArgumentError("cannot set index in the lower triangular part " *
285-
lazy"($i, $j) of a UnitUpperTriangular matrix to a nonzero value ($x)"))
298+
iszero(x) || throw_nonzeroerror(typeof(A), x, i, j)
286299
elseif i == j
287-
x == oneunit(x) || throw(ArgumentError(lazy"cannot set index on the diagonal ($i, $j) " *
288-
lazy"of a UnitUpperTriangular matrix to a non-unit value ($x)"))
300+
x == oneunit(x) || throw_nononeerror(typeof(A), x, i, j)
289301
else
290302
A.data[i,j] = x
291303
end
@@ -294,8 +306,7 @@ end
294306

295307
@propagate_inbounds function setindex!(A::LowerTriangular, x, i::Integer, j::Integer)
296308
if i < j
297-
iszero(x) || throw(ArgumentError("cannot set index in the upper triangular part " *
298-
lazy"($i, $j) of a LowerTriangular matrix to a nonzero value ($x)"))
309+
iszero(x) || throw_nonzeroerror(typeof(A), x, i, j)
299310
else
300311
A.data[i,j] = x
301312
end
@@ -304,28 +315,31 @@ end
304315

305316
@propagate_inbounds function setindex!(A::UnitLowerTriangular, x, i::Integer, j::Integer)
306317
if i < j
307-
iszero(x) || throw(ArgumentError("cannot set index in the upper triangular part " *
308-
lazy"($i, $j) of a UnitLowerTriangular matrix to a nonzero value ($x)"))
318+
iszero(x) || throw_nonzeroerror(typeof(A), x, i, j)
309319
elseif i == j
310-
x == oneunit(x) || throw(ArgumentError(lazy"cannot set index on the diagonal ($i, $j) " *
311-
lazy"of a UnitLowerTriangular matrix to a non-unit value ($x)"))
320+
x == oneunit(x) || throw_nononeerror(typeof(A), x, i, j)
312321
else
313322
A.data[i,j] = x
314323
end
315324
return A
316325
end
317326

327+
@noinline function throw_setindex_structuralzero_error(T, @nospecialize(x))
328+
Ts = _zero_triangular_half_str(T)
329+
Tn = nameof(T)
330+
throw(ArgumentError(
331+
lazy"cannot set indices in the $Ts triangular part of an $Tn matrix to a nonzero value ($x)"))
332+
end
333+
318334
@inline function fill!(A::UpperTriangular, x)
319-
iszero(x) || throw(ArgumentError("cannot set indices in the lower triangular part " *
320-
lazy"of an UpperTriangular matrix to a nonzero value ($x)"))
335+
iszero(x) || throw_setindex_structuralzero_error(typeof(A), x)
321336
for col in axes(A,2), row in firstindex(A,1):col
322337
@inbounds A.data[row, col] = x
323338
end
324339
A
325340
end
326341
@inline function fill!(A::LowerTriangular, x)
327-
iszero(x) || throw(ArgumentError("cannot set indices in the upper triangular part " *
328-
lazy"of a LowerTriangular matrix to a nonzero value ($x)"))
342+
iszero(x) || throw_setindex_structuralzero_error(typeof(A), x)
329343
for col in axes(A,2), row in col:lastindex(A,1)
330344
@inbounds A.data[row, col] = x
331345
end

0 commit comments

Comments
 (0)