Skip to content

Commit ce13a94

Browse files
committed
Better error messages & coverage metrics for triangular
1 parent 22a1418 commit ce13a94

File tree

1 file changed

+52
-28
lines changed

1 file changed

+52
-28
lines changed

base/linalg/triangular.jl

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,34 @@ getindex{T,S}(A::LowerTriangular{T,S}, i::Integer, j::Integer) = i >= j ? A.data
112112
getindex{T,S}(A::UnitUpperTriangular{T,S}, i::Integer, j::Integer) = i == j ? one(T) : (i < j ? A.data[i,j] : zero(A.data[i,j]))
113113
getindex{T,S}(A::UpperTriangular{T,S}, i::Integer, j::Integer) = i <= j ? A.data[i,j] : zero(A.data[i,j])
114114

115-
setindex!(A::UpperTriangular, x, i::Integer, j::Integer) = i <= j ? (A.data[i,j] = x; A) : throw(BoundsError())
116-
setindex!(A::UnitUpperTriangular, x, i::Integer, j::Integer) = i < j ? (A.data[i,j] = x; A) : throw(BoundsError())
117-
setindex!(A::LowerTriangular, x, i::Integer, j::Integer) = i >= j ? (A.data[i,j] = x; A) : throw(BoundsError())
118-
setindex!(A::UnitLowerTriangular, x, i::Integer, j::Integer) = i > j ? (A.data[i,j] = x; A) : throw(BoundsError())
115+
function setindex!(A::UpperTriangular, x, i::Integer, j::Integer)
116+
if i <= j
117+
A.data[i,j] = x; A
118+
else
119+
throw(BoundsError())
120+
end
121+
end
122+
function setindex!(A::UnitUpperTriangular, x, i::Integer, j::Integer)
123+
if i < j
124+
A.data[i,j] = x; A
125+
else
126+
throw(BoundsError())
127+
end
128+
end
129+
function setindex!(A::LowerTriangular, x, i::Integer, j::Integer)
130+
if i >= j
131+
A.data[i,j] = x; A
132+
else
133+
throw(BoundsError())
134+
end
135+
end
136+
function setindex!(A::UnitLowerTriangular, x, i::Integer, j::Integer)
137+
if i > j
138+
A.data[i,j] = x; A
139+
else
140+
throw(BoundsError())
141+
end
142+
end
119143

120144
istril(A::LowerTriangular) = true
121145
istril(A::UnitLowerTriangular) = true
@@ -319,7 +343,7 @@ end
319343
function A_mul_B!(A::UpperTriangular, B::StridedVecOrMat)
320344
m, n = size(B, 1), size(B, 2)
321345
if m != size(A, 1)
322-
throw(DimensionMismatch("left and right hand side does not fit"))
346+
throw(DimensionMismatch("B needs first dimension of size $(size(A,1)), has size $m"))
323347
end
324348
for j = 1:n
325349
for i = 1:m
@@ -335,7 +359,7 @@ end
335359
function A_mul_B!(A::UnitUpperTriangular, B::StridedVecOrMat)
336360
m, n = size(B, 1), size(B, 2)
337361
if m != size(A, 1)
338-
throw(DimensionMismatch("left and right hand side does not fit"))
362+
throw(DimensionMismatch("B needs first dimension of size $(size(A,1)), has size $m"))
339363
end
340364
for j = 1:n
341365
for i = 1:m
@@ -352,7 +376,7 @@ end
352376
function A_mul_B!(A::LowerTriangular, B::StridedVecOrMat)
353377
m, n = size(B, 1), size(B, 2)
354378
if m != size(A, 1)
355-
throw(DimensionMismatch("left and right hand side does not fit"))
379+
throw(DimensionMismatch("B needs first dimension of size $(size(A,1)), has size $m"))
356380
end
357381
for j = 1:n
358382
for i = m:-1:1
@@ -368,7 +392,7 @@ end
368392
function A_mul_B!(A::UnitLowerTriangular, B::StridedVecOrMat)
369393
m, n = size(B, 1), size(B, 2)
370394
if m != size(A, 1)
371-
throw(DimensionMismatch("left and right hand side does not fit"))
395+
throw(DimensionMismatch("B needs first dimension of size $(size(A,1)), has size $m"))
372396
end
373397
for j = 1:n
374398
for i = m:-1:1
@@ -385,7 +409,7 @@ end
385409
function Ac_mul_B!(A::UpperTriangular, B::StridedVecOrMat)
386410
m, n = size(B, 1), size(B, 2)
387411
if m != size(A, 1)
388-
throw(DimensionMismatch("left and right hand side does not fit"))
412+
throw(DimensionMismatch("B needs first dimension of size $(size(A,1)), has size $m"))
389413
end
390414
for j = 1:n
391415
for i = m:-1:1
@@ -401,7 +425,7 @@ end
401425
function Ac_mul_B!(A::UnitUpperTriangular, B::StridedVecOrMat)
402426
m, n = size(B, 1), size(B, 2)
403427
if m != size(A, 1)
404-
throw(DimensionMismatch("left and right hand side does not fit"))
428+
throw(DimensionMismatch("B needs first dimension of size $(size(A,1)), has size $m"))
405429
end
406430
for j = 1:n
407431
for i = m:-1:1
@@ -418,7 +442,7 @@ end
418442
function Ac_mul_B!(A::LowerTriangular, B::StridedVecOrMat)
419443
m, n = size(B, 1), size(B, 2)
420444
if m != size(A, 1)
421-
throw(DimensionMismatch("left and right hand side does not fit"))
445+
throw(DimensionMismatch("B needs first dimension of size $(size(A,1)), has size $m"))
422446
end
423447
for j = 1:n
424448
for i = 1:m
@@ -434,7 +458,7 @@ end
434458
function Ac_mul_B!(A::UnitLowerTriangular, B::StridedVecOrMat)
435459
m, n = size(B, 1), size(B, 2)
436460
if m != size(A, 1)
437-
throw(DimensionMismatch("left and right hand side does not fit"))
461+
throw(DimensionMismatch("B needs first dimension of size $(size(A,1)), has size $m"))
438462
end
439463
for j = 1:n
440464
for i = 1:m
@@ -451,7 +475,7 @@ end
451475
function A_mul_B!(A::StridedMatrix, B::UpperTriangular)
452476
m, n = size(A)
453477
if size(B, 1) != n
454-
throw(DimensionMismatch("left and right hand side does not fit"))
478+
throw(DimensionMismatch("B needs first dimension of size $n, has size $(size(B,1))"))
455479
end
456480
for i = 1:m
457481
for j = n:-1:1
@@ -467,7 +491,7 @@ end
467491
function A_mul_B!(A::StridedMatrix, B::UnitUpperTriangular)
468492
m, n = size(A)
469493
if size(B, 1) != n
470-
throw(DimensionMismatch("left and right hand side does not fit"))
494+
throw(DimensionMismatch("B needs first dimension of size $n, has size $(size(B,1))"))
471495
end
472496
for i = 1:m
473497
for j = n:-1:1
@@ -484,7 +508,7 @@ end
484508
function A_mul_B!(A::StridedMatrix, B::LowerTriangular)
485509
m, n = size(A)
486510
if size(B, 1) != n
487-
throw(DimensionMismatch("left and right hand side does not fit"))
511+
throw(DimensionMismatch("B needs first dimension of size $n, has size $(size(B,1))"))
488512
end
489513
for i = 1:m
490514
for j = 1:n
@@ -500,7 +524,7 @@ end
500524
function A_mul_B!(A::StridedMatrix, B::UnitLowerTriangular)
501525
m, n = size(A)
502526
if size(B, 1) != n
503-
throw(DimensionMismatch("left and right hand side does not fit"))
527+
throw(DimensionMismatch("B needs first dimension of size $n, has size $(size(B,1))"))
504528
end
505529
for i = 1:m
506530
for j = 1:n
@@ -517,7 +541,7 @@ end
517541
function A_mul_Bc!(A::StridedMatrix, B::UpperTriangular)
518542
m, n = size(A)
519543
if size(B, 1) != n
520-
throw(DimensionMismatch("left and right hand side does not fit"))
544+
throw(DimensionMismatch("B needs first dimension of size $n, has size $(size(B,1))"))
521545
end
522546
for i = 1:m
523547
for j = 1:n
@@ -533,7 +557,7 @@ end
533557
function A_mul_Bc!(A::StridedMatrix, B::UnitUpperTriangular)
534558
m, n = size(A)
535559
if size(B, 1) != n
536-
throw(DimensionMismatch("left and right hand side does not fit"))
560+
throw(DimensionMismatch("B needs first dimension of size $n, has size $(size(B,1))"))
537561
end
538562
for i = 1:m
539563
for j = 1:n
@@ -550,7 +574,7 @@ end
550574
function A_mul_Bc!(A::StridedMatrix, B::LowerTriangular)
551575
m, n = size(A)
552576
if size(B, 1) != n
553-
throw(DimensionMismatch("left and right hand side does not fit"))
577+
throw(DimensionMismatch("B needs first dimension of size $n, has size $(size(B,1))"))
554578
end
555579
for i = 1:m
556580
for j = n:-1:1
@@ -566,7 +590,7 @@ end
566590
function A_mul_Bc!(A::StridedMatrix, B::UnitLowerTriangular)
567591
m, n = size(A)
568592
if size(B, 1) != n
569-
throw(DimensionMismatch("left and right hand side does not fit"))
593+
throw(DimensionMismatch("B needs first dimension of size $n, has size $(size(B,1))"))
570594
end
571595
for i = 1:m
572596
for j = n:-1:1
@@ -651,7 +675,7 @@ end
651675
function A_rdiv_B!(A::StridedMatrix, B::UpperTriangular)
652676
m, n = size(A)
653677
if size(B, 1) != n
654-
throw(DimensionMismatch("left and right hand side does not fit"))
678+
throw(DimensionMismatch("B needs first dimension of size $n, has size $(size(B,1))"))
655679
end
656680
for i = 1:m
657681
for j = 1:n
@@ -667,7 +691,7 @@ end
667691
function A_rdiv_B!(A::StridedMatrix, B::UnitUpperTriangular)
668692
m, n = size(A)
669693
if size(B, 1) != n
670-
throw(DimensionMismatch("left and right hand side does not fit"))
694+
throw(DimensionMismatch("B needs first dimension of size $n, has size $(size(B,1))"))
671695
end
672696
for i = 1:m
673697
for j = 1:n
@@ -684,7 +708,7 @@ end
684708
function A_rdiv_B!(A::StridedMatrix, B::LowerTriangular)
685709
m, n = size(A)
686710
if size(B, 1) != n
687-
throw(DimensionMismatch("left and right hand side does not fit"))
711+
throw(DimensionMismatch("B needs first dimension of size $n, has size $(size(B,1))"))
688712
end
689713
for i = 1:m
690714
for j = n:-1:1
@@ -700,7 +724,7 @@ end
700724
function A_rdiv_B!(A::StridedMatrix, B::UnitLowerTriangular)
701725
m, n = size(A)
702726
if size(B, 1) != n
703-
throw(DimensionMismatch("left and right hand side does not fit"))
727+
throw(DimensionMismatch("B needs first dimension of size $n, has size $(size(B,1))"))
704728
end
705729
for i = 1:m
706730
for j = n:-1:1
@@ -717,7 +741,7 @@ end
717741
function A_rdiv_Bc!(A::StridedMatrix, B::UpperTriangular)
718742
m, n = size(A)
719743
if size(B, 1) != n
720-
throw(DimensionMismatch("left and right hand side does not fit"))
744+
throw(DimensionMismatch("B needs first dimension of size $n, has size $(size(B,1))"))
721745
end
722746
for i = 1:m
723747
for j = n:-1:1
@@ -733,7 +757,7 @@ end
733757
function A_rdiv_Bc!(A::StridedMatrix, B::UnitUpperTriangular)
734758
m, n = size(A)
735759
if size(B, 1) != n
736-
throw(DimensionMismatch("left and right hand side does not fit"))
760+
throw(DimensionMismatch("B needs first dimension of size $n, has size $(size(B,1))"))
737761
end
738762
for i = 1:m
739763
for j = n:-1:1
@@ -750,7 +774,7 @@ end
750774
function A_rdiv_Bc!(A::StridedMatrix, B::LowerTriangular)
751775
m, n = size(A)
752776
if size(B, 1) != n
753-
throw(DimensionMismatch("left and right hand side does not fit"))
777+
throw(DimensionMismatch("B needs first dimension of size $n, has size $(size(B,1))"))
754778
end
755779
for i = 1:m
756780
for j = 1:n
@@ -766,7 +790,7 @@ end
766790
function A_rdiv_Bc!(A::StridedMatrix, B::UnitLowerTriangular)
767791
m, n = size(A)
768792
if size(B, 1) != n
769-
throw(DimensionMismatch("left and right hand side does not fit"))
793+
throw(DimensionMismatch("B needs first dimension of size $n, has size $(size(B,1))"))
770794
end
771795
for i = 1:m
772796
for j = 1:n

0 commit comments

Comments
 (0)