@@ -23,12 +23,12 @@ for t in (:LowerTriangular, :UnitLowerTriangular, :UpperTriangular, :UnitUpperTr
23
23
convert {T,S} (:: Type{Matrix} , A:: $t{T,S} ) = convert (Matrix{T}, A)
24
24
25
25
function similar {T,S,Tnew} (A:: $t{T,S} , :: Type{Tnew} , dims:: Dims )
26
- if dims[1 ] != dims[2 ]
27
- throw (ArgumentError (" Triangular matrix must be square" ))
28
- end
29
26
if length (dims) != 2
30
27
throw (ArgumentError (" Triangular matrix must have two dimensions" ))
31
28
end
29
+ if dims[1 ] != dims[2 ]
30
+ throw (ArgumentError (" Triangular matrix must be square" ))
31
+ end
32
32
B = similar (A. data, Tnew, dims)
33
33
return $ t (B)
34
34
end
@@ -119,33 +119,41 @@ getindex{T,S}(A::UpperTriangular{T,S}, i::Integer, j::Integer) = i <= j ? A.data
119
119
120
120
function setindex! (A:: UpperTriangular , x, i:: Integer , j:: Integer )
121
121
if i > j
122
- throw (BoundsError (A,(i,j)))
122
+ x == 0 || throw (ArgumentError (" cannot set index in the lower triangular part ($i , $j ) of an UpperTriangular matrix to a nonzero value ($x )" ))
123
+ else
124
+ A. data[i,j] = x
123
125
end
124
- A. data[i,j] = x
125
126
return A
126
127
end
127
128
128
129
function setindex! (A:: UnitUpperTriangular , x, i:: Integer , j:: Integer )
129
- if i >= j
130
- throw (BoundsError (A,(i,j)))
130
+ if i > j
131
+ x == 0 || throw (ArgumentError (" cannot set index in the lower triangular part ($i , $j ) of a UnitUpperTriangular matrix to a nonzero value ($x )" ))
132
+ elseif i == j
133
+ x == 1 || throw (ArgumentError (" cannot set index on the diagonal ($i , $j ) of a UnitUpperTriangular matrix to a non-unit value ($x )" ))
134
+ else
135
+ A. data[i,j] = x
131
136
end
132
- A. data[i,j] = x
133
137
return A
134
138
end
135
139
136
140
function setindex! (A:: LowerTriangular , x, i:: Integer , j:: Integer )
137
141
if i < j
138
- throw (BoundsError (A,(i,j)))
142
+ x == 0 || throw (ArgumentError (" cannot set index in the upper triangular part ($i , $j ) of a LowerTriangular matrix to a nonzero value ($x )" ))
143
+ else
144
+ A. data[i,j] = x
139
145
end
140
- A. data[i,j] = x
141
146
return A
142
147
end
143
148
144
149
function setindex! (A:: UnitLowerTriangular , x, i:: Integer , j:: Integer )
145
- if i <= j
146
- throw (BoundsError (A,(i,j)))
150
+ if i < j
151
+ x == 0 || throw (ArgumentError (" cannot set index in the upper triangular part ($i , $j ) of a UnitLowerTriangular matrix to a nonzero value ($x )" ))
152
+ elseif i == j
153
+ x == 1 || throw (ArgumentError (" cannot set diagonal index ($i , $j ) of a UnitLowerTriangular matrix to a non-unit value ($x )" ))
154
+ else
155
+ A. data[i,j] = x
147
156
end
148
- A. data[i,j] = x
149
157
return A
150
158
end
151
159
0 commit comments