@@ -269,10 +269,24 @@ Base.isstored(A::UpperTriangular, i::Int, j::Int) =
269
269
@propagate_inbounds getindex (A:: UpperTriangular , i:: Integer , j:: Integer ) =
270
270
i <= j ? A. data[i,j] : _zero (A. data,j,i)
271
271
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
+
272
287
@propagate_inbounds function setindex! (A:: UpperTriangular , x, i:: Integer , j:: Integer )
273
288
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)
276
290
else
277
291
A. data[i,j] = x
278
292
end
281
295
282
296
@propagate_inbounds function setindex! (A:: UnitUpperTriangular , x, i:: Integer , j:: Integer )
283
297
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)
286
299
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)
289
301
else
290
302
A. data[i,j] = x
291
303
end
294
306
295
307
@propagate_inbounds function setindex! (A:: LowerTriangular , x, i:: Integer , j:: Integer )
296
308
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)
299
310
else
300
311
A. data[i,j] = x
301
312
end
@@ -304,28 +315,31 @@ end
304
315
305
316
@propagate_inbounds function setindex! (A:: UnitLowerTriangular , x, i:: Integer , j:: Integer )
306
317
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)
309
319
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)
312
321
else
313
322
A. data[i,j] = x
314
323
end
315
324
return A
316
325
end
317
326
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
+
318
334
@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)
321
336
for col in axes (A,2 ), row in firstindex (A,1 ): col
322
337
@inbounds A. data[row, col] = x
323
338
end
324
339
A
325
340
end
326
341
@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)
329
343
for col in axes (A,2 ), row in col: lastindex (A,1 )
330
344
@inbounds A. data[row, col] = x
331
345
end
0 commit comments