@@ -278,43 +278,38 @@ Base.@constprop :aggressive @propagate_inbounds function getindex(A::Union{Lower
278
278
end
279
279
end
280
280
281
- _zero_triangular_half_str (:: Type{<:UpperOrUnitUpperTriangular} ) = " lower"
282
- _zero_triangular_half_str (:: Type{<:LowerOrUnitLowerTriangular} ) = " upper"
283
-
284
- @noinline function throw_nonzeroerror (T, @nospecialize (x), i, j)
285
- Ts = _zero_triangular_half_str (T)
286
- Tn = nameof (T)
281
+ @noinline function throw_nonzeroerror (Tn, @nospecialize (x), i, j)
282
+ Ts = Tn in (:UpperTriangular , :UnitUpperTriangular ) ? " lower" : " upper"
287
283
throw (ArgumentError (
288
- lazy " cannot set index in the $Ts triangular part ($i, $j) of an $Tn matrix to a nonzero value ($x)" ))
284
+ lazy " cannot set index in the $Ts triangular part ($i, $j) of a $Tn matrix to a nonzero value ($x)" ))
289
285
end
290
- @noinline function throw_nonuniterror (T, @nospecialize (x), i, j)
291
- check_compatible_type (T, x)
292
- Tn = nameof (T)
286
+ @noinline function throw_nonuniterror (Tn, @nospecialize (x), i, j)
293
287
throw (ArgumentError (
294
- lazy " cannot set index on the diagonal ($i, $j) of an $Tn matrix to a non-unit value ($x)" ))
295
- end
296
- function check_compatible_type (T, @nospecialize (x))
297
- ET = eltype (T)
298
- convert (ET, x) # check that the types are compatible with setindex!
288
+ lazy " cannot set index ($i, $j) on the diagonal of a $Tn matrix to a non-unit value ($x)" ))
299
289
end
300
290
301
291
@propagate_inbounds function setindex! (A:: UpperTriangular , x, i:: Integer , j:: Integer )
302
292
if i > j
303
293
@boundscheck checkbounds (A, i, j)
304
- iszero (x) || throw_nonzeroerror (typeof (A), x, i, j)
294
+ # the value must be convertible to the eltype for setindex! to be meaningful
295
+ xT = convert (eltype (A), x)
296
+ iszero (xT) || throw_nonzeroerror (:UpperTriangular , x, i, j)
305
297
else
306
298
A. data[i,j] = x
307
299
end
308
300
return A
309
301
end
310
302
311
303
@propagate_inbounds function setindex! (A:: UnitUpperTriangular , x, i:: Integer , j:: Integer )
312
- if i > j
313
- @boundscheck checkbounds (A, i, j)
314
- iszero (x) || throw_nonzeroerror (typeof (A), x, i, j)
315
- elseif i == j
304
+ if i >= j
316
305
@boundscheck checkbounds (A, i, j)
317
- x == oneunit (eltype (A)) || throw_nonuniterror (typeof (A), x, i, j)
306
+ # the value must be convertible to the eltype for setindex! to be meaningful
307
+ xT = convert (eltype (A), x)
308
+ if i > j
309
+ iszero (xT) || throw_nonzeroerror (:UnitUpperTriangular , x, i, j)
310
+ else
311
+ xT == oneunit (eltype (A)) || throw_nonuniterror (:UnitUpperTriangular , x, i, j)
312
+ end
318
313
else
319
314
A. data[i,j] = x
320
315
end
@@ -324,20 +319,25 @@ end
324
319
@propagate_inbounds function setindex! (A:: LowerTriangular , x, i:: Integer , j:: Integer )
325
320
if i < j
326
321
@boundscheck checkbounds (A, i, j)
327
- iszero (x) || throw_nonzeroerror (typeof (A), x, i, j)
322
+ # the value must be convertible to the eltype for setindex! to be meaningful
323
+ xT = convert (eltype (A), x)
324
+ iszero (xT) || throw_nonzeroerror (:LowerTriangular , x, i, j)
328
325
else
329
326
A. data[i,j] = x
330
327
end
331
328
return A
332
329
end
333
330
334
331
@propagate_inbounds function setindex! (A:: UnitLowerTriangular , x, i:: Integer , j:: Integer )
335
- if i < j
332
+ if i <= j
336
333
@boundscheck checkbounds (A, i, j)
337
- iszero (x) || throw_nonzeroerror (typeof (A), x, i, j)
338
- elseif i == j
339
- @boundscheck checkbounds (A, i, j)
340
- x == oneunit (eltype (A)) || throw_nonuniterror (typeof (A), x, i, j)
334
+ # the value must be convertible to the eltype for setindex! to be meaningful
335
+ xT = convert (eltype (A), x)
336
+ if i < j
337
+ iszero (xT) || throw_nonzeroerror (:UnitLowerTriangular , x, i, j)
338
+ else
339
+ xT == oneunit (eltype (A)) || throw_nonuniterror (:UnitLowerTriangular , x, i, j)
340
+ end
341
341
else
342
342
A. data[i,j] = x
343
343
end
@@ -593,7 +593,7 @@ for (T, UT) in ((:UpperTriangular, :UnitUpperTriangular), (:LowerTriangular, :Un
593
593
@eval @inline function _copy! (A:: $UT , B:: $T )
594
594
for dind in diagind (A, IndexStyle (A))
595
595
if A[dind] != B[dind]
596
- throw_nonuniterror (typeof (A), B[dind], Tuple (dind)... )
596
+ throw_nonuniterror (nameof ( typeof (A) ), B[dind], Tuple (dind)... )
597
597
end
598
598
end
599
599
_copy! ($ T (parent (A)), B)
0 commit comments