Skip to content

Commit

Permalink
Fix #6838. Add setindex! for triangular matrix types.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasnoack committed Jan 16, 2015
1 parent 5900fab commit ffc9778
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
5 changes: 5 additions & 0 deletions base/linalg/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ getindex{T,S}(A::LowerTriangular{T,S}, i::Integer, j::Integer) = i >= j ? A.data
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]))
getindex{T,S}(A::UpperTriangular{T,S}, i::Integer, j::Integer) = i <= j ? A.data[i,j] : zero(A.data[i,j])

setindex!(A::UpperTriangular, x, i::Integer, j::Integer) = i <= j ? (A.data[i,j] = x; A) : throw(BoundsError())
setindex!(A::UnitUpperTriangular, x, i::Integer, j::Integer) = i < j ? (A.data[i,j] = x; A) : throw(BoundsError())
setindex!(A::LowerTriangular, x, i::Integer, j::Integer) = i >= j ? (A.data[i,j] = x; A) : throw(BoundsError())
setindex!(A::UnitLowerTriangular, x, i::Integer, j::Integer) = i > j ? (A.data[i,j] = x; A) : throw(BoundsError())

istril{T,S}(A::LowerTriangular{T,S}) = true
istril{T,S}(A::UnitLowerTriangular{T,S}) = true
istril{T,S}(A::UpperTriangular{T,S}) = false
Expand Down
32 changes: 31 additions & 1 deletion test/linalg/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,41 @@ for elty1 in (Float32, Float64, Complex64, Complex128, BigFloat, Int)
# similar
@test isa(similar(A1), t1)

# Linear indexing
# getindex
## Linear indexing
for i = 1:length(A1)
@test A1[i] == full(A1)[i]
end

## Cartesian indexing
for i = 1:size(A1, 1)
for j = 1:size(A1, 2)
@test A1[i,j] == full(A1)[i,j]
end
end

# setindex! (and copy)
A1c = copy(A1)
for i = 1:size(A1, 1)
for j = 1:size(A1, 2)
if uplo1 == :U
if i > j || (i == j && t1 == UnitUpperTriangular)
@test_throws BoundsError A1c[i,j] = 0
else
A1c[i,j] = 0
@test A1c[i,j] == 0
end
else
if i < j || (i == j && t1 == UnitLowerTriangular)
@test_throws BoundsError A1c[i,j] = 0
else
A1c[i,j] = 0
@test A1c[i,j] == 0
end
end
end
end

# istril/istriu
if uplo1 == :L
@test istril(A1)
Expand Down

0 comments on commit ffc9778

Please sign in to comment.