Skip to content

Commit

Permalink
Fix residual issues and add fallback methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kshyatt committed Aug 13, 2015
1 parent b0f3397 commit a67c4e2
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 25 deletions.
6 changes: 2 additions & 4 deletions base/linalg/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,21 @@ function triu!(D::Diagonal,k::Integer=0)
n = size(D,1)
if abs(k) > n
throw(ArgumentError("requested diagonal, $k, out of bounds in matrix of size ($n,$n)"))
elseif k != 0
elseif k > 0
fill!(D.diag,0)
end
return D
end
triu(D::Diagonal,k::Integer=0) = triu!(copy(D),k)

function tril!(D::Diagonal,k::Integer=0)
n = size(D,1)
if abs(k) > n
throw(ArgumentError("requested diagonal, $k, out of bounds in matrix of size ($n,$n)"))
elseif k != 0
elseif k < 0
fill!(D.diag,0)
end
return D
end
tril(D::Diagonal,k::Integer=0) = tril!(copy(D),k)

==(Da::Diagonal, Db::Diagonal) = Da.diag == Db.diag
-(A::Diagonal)=Diagonal(-A.diag)
Expand Down
2 changes: 2 additions & 0 deletions base/linalg/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ cross(a::AbstractVector, b::AbstractVector) = [a[2]*b[3]-a[3]*b[2], a[3]*b[1]-a[

triu(M::AbstractMatrix) = triu!(copy(M))
tril(M::AbstractMatrix) = tril!(copy(M))
triu(M::AbstractMatrix,k::Integer) = triu!(copy(M),k)
tril(M::AbstractMatrix,k::Integer) = tril!(copy(M),k)
triu!(M::AbstractMatrix) = triu!(M,0)
tril!(M::AbstractMatrix) = tril!(M,0)

Expand Down
19 changes: 8 additions & 11 deletions base/linalg/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ function tril!(A::UpperTriangular,k::Integer=0)
fill!(A.data,0)
return A
elseif k == 0
return UpperTriangular(diagm(diag(A)))
for j in 1:n, i in 1:j-1
A.data[i,j] = 0
end
return A
else
return UpperTriangular(tril!(A.data,k))
end
Expand Down Expand Up @@ -200,7 +203,10 @@ function triu!(A::LowerTriangular,k::Integer=0)
fill!(A.data,0)
return A
elseif k == 0
return LowerTriangular(diagm(diag(A)))
for j in 1:n, i in j+1:n
A.data[i,j] = 0
end
return A
else
return LowerTriangular(triu!(A.data,k))
end
Expand Down Expand Up @@ -236,15 +242,6 @@ function tril!(A::UnitLowerTriangular,k::Integer=0)
return tril!(LowerTriangular(A.data),k)
end

tril(A::UpperTriangular,k::Integer=0) = tril!(copy(A),k)
tril(A::LowerTriangular,k::Integer=0) = tril!(copy(A),k)
tril(A::UnitUpperTriangular,k::Integer=0) = tril!(copy(A),k)
tril(A::UnitLowerTriangular,k::Integer=0) = tril!(copy(A),k)
triu(A::UpperTriangular,k::Integer=0) = triu!(copy(A),k)
triu(A::LowerTriangular,k::Integer=0) = triu!(copy(A),k)
triu(A::UnitUpperTriangular,k::Integer=0) = triu!(copy(A),k)
triu(A::UnitLowerTriangular,k::Integer=0) = triu!(copy(A),k)

transpose{T,S}(A::LowerTriangular{T,S}) = UpperTriangular{T, S}(transpose(A.data))
transpose{T,S}(A::UnitLowerTriangular{T,S}) = UnitUpperTriangular{T, S}(transpose(A.data))
transpose{T,S}(A::UpperTriangular{T,S}) = LowerTriangular{T, S}(transpose(A.data))
Expand Down
8 changes: 2 additions & 6 deletions base/linalg/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ function tril!(M::SymTridiagonal, k::Integer=0)
elseif k < -1
fill!(M.ev,0)
fill!(M.dv,0)
return Tridiagonal(M.ev,M.dv,M.ev)
return Tridiagonal(M.ev,M.dv,copy(M.ev))
elseif k == -1
fill!(M.dv,0)
return Tridiagonal(M.ev,M.dv,zeros(M.ev))
Expand All @@ -166,7 +166,6 @@ function tril!(M::SymTridiagonal, k::Integer=0)
return Tridiagonal(M.ev,M.dv,copy(M.ev))
end
end
tril(M::SymTridiagonal, k::Integer=0) = tril!(copy(M),k)

function triu!(M::SymTridiagonal, k::Integer=0)
n = length(M.dv)
Expand All @@ -175,7 +174,7 @@ function triu!(M::SymTridiagonal, k::Integer=0)
elseif k > 1
fill!(M.ev,0)
fill!(M.dv,0)
return Tridiagonal(M.ev,M.dv,M.ev)
return Tridiagonal(M.ev,M.dv,copy(M.ev))
elseif k == 1
fill!(M.dv,0)
return Tridiagonal(zeros(M.ev),M.dv,M.ev)
Expand All @@ -185,7 +184,6 @@ function triu!(M::SymTridiagonal, k::Integer=0)
return Tridiagonal(M.ev,M.dv,copy(M.ev))
end
end
triu(M::SymTridiagonal, k::Integer=0) = triu!(copy(M),k)

###################
# Generic methods #
Expand Down Expand Up @@ -383,7 +381,6 @@ function tril!(M::Tridiagonal, k::Integer=0)
end
return M
end
tril(M::Tridiagonal, k::Integer=0) = tril!(copy(M),k)

function triu!(M::Tridiagonal, k::Integer=0)
n = length(M.d)
Expand All @@ -401,7 +398,6 @@ function triu!(M::Tridiagonal, k::Integer=0)
end
return M
end
triu(M::Tridiagonal, k::Integer=0) = triu!(copy(M),k)

###################
# Generic methods #
Expand Down
10 changes: 6 additions & 4 deletions test/linalg/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ for relty in (Float32, Float64, BigFloat), elty in (relty, Complex{relty})
# test triu/tril
@test istriu(D)
@test istril(D)
@test triu(D,1) == zeros(D)
@test triu(D,0) == D
@test tril(D,1) == zeros(D)
@test tril(D,0) == D
@test triu(D,1) == zeros(D)
@test triu(D,0) == D
@test triu(D,-1) == D
@test tril(D,1) == D
@test tril(D,-1) == zeros(D)
@test tril(D,0) == D

# factorize
@test factorize(D) == D
Expand Down

0 comments on commit a67c4e2

Please sign in to comment.