From ffc9778ec2ae34550629a8530ed04922a41a5864 Mon Sep 17 00:00:00 2001 From: Andreas Noack Date: Fri, 16 Jan 2015 16:15:03 -0500 Subject: [PATCH] Fix #6838. Add setindex! for triangular matrix types. --- base/linalg/triangular.jl | 5 +++++ test/linalg/triangular.jl | 32 +++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/base/linalg/triangular.jl b/base/linalg/triangular.jl index b0658b1029e3f..2ec3bb7881da7 100644 --- a/base/linalg/triangular.jl +++ b/base/linalg/triangular.jl @@ -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 diff --git a/test/linalg/triangular.jl b/test/linalg/triangular.jl index 785a6c3dbb16a..1d9fa1ed18bbf 100644 --- a/test/linalg/triangular.jl +++ b/test/linalg/triangular.jl @@ -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)