Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tril and triu for special matrices #12574

Merged
merged 5 commits into from
Aug 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion base/linalg/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,44 @@ ctranspose(M::Bidiagonal) = Bidiagonal(conj(M.dv), conj(M.ev), !M.isupper)
istriu(M::Bidiagonal) = M.isupper || all(M.ev .== 0)
istril(M::Bidiagonal) = !M.isupper || all(M.ev .== 0)

function tril(M::Bidiagonal, k::Integer=0)
n = length(M.dv)
if abs(k) > n
throw(ArgumentError("requested diagonal, $k, out of bounds in matrix of size ($n,$n)"))
elseif M.isupper && k < 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this could be:

    elseif k > 0
          return M
    elseif M.isupper
          if k < 0
               return Bidiagonal(zeros(M.dv),zeros(M.ev),true)
          else
               return Bidiagonal(M.dv,zeros(M.ev),true)
          end
    elseif k < -1
       return Bidiagonal(zeros(M.dv),zeros(M.ev),false)
    elseif k == -1
       return Bidiagonal(zeros(M.dv),M.ev,false)
    else
       return M
    end

This avoids the rechecking of M.isupper and passing it to Bidiagonal.

return Bidiagonal(zeros(M.dv),zeros(M.ev),M.isupper)
elseif k < -1
return Bidiagonal(zeros(M.dv),zeros(M.ev),M.isupper)
elseif !M.isupper && k == 0
return M
elseif M.isupper && k == 0
return Bidiagonal(M.dv,zeros(M.ev),M.isupper)
elseif !M.isupper && k == -1
return Bidiagonal(zeros(M.dv),M.ev,M.isupper)
elseif k > 0
return M
end
end

function triu(M::Bidiagonal, k::Integer=0)
n = length(M.dv)
if abs(k) > n
throw(ArgumentError("requested diagonal, $k, out of bounds in matrix of size ($n,$n)"))
elseif !M.isupper && k > 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above with tril

return Bidiagonal(zeros(M.dv),zeros(M.ev),M.isupper)
elseif k > 1
return Bidiagonal(zeros(M.dv),zeros(M.ev),M.isupper)
elseif M.isupper && k == 0
return M
elseif !M.isupper && k == 0
return Bidiagonal(M.dv,zeros(M.ev),M.isupper)
elseif M.isupper && k == 1
return Bidiagonal(zeros(M.dv),M.ev,M.isupper)
elseif k < 0
return M
end
end

function diag{T}(M::Bidiagonal{T}, n::Integer=0)
if n==0
return M.dv
Expand Down Expand Up @@ -249,4 +287,3 @@ function eigvecs{T}(M::Bidiagonal{T})
Q #Actually Triangular
end
eigfact(M::Bidiagonal) = Eigen(eigvals(M), eigvecs(M))

4 changes: 2 additions & 2 deletions base/linalg/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ isposdef(D::Diagonal) = all(D.diag .> 0)

factorize(D::Diagonal) = D

tril!(D::Diagonal,i::Integer) = i == 0 ? D : zeros(D)
triu!(D::Diagonal,i::Integer) = i == 0 ? D : zeros(D)
tril(D::Diagonal,i::Integer=0) = i == 0 ? D : zeros(D)
triu(D::Diagonal,i::Integer=0) = i == 0 ? D : zeros(D)

==(Da::Diagonal, Db::Diagonal) = Da.diag == Db.diag
-(A::Diagonal)=Diagonal(-A.diag)
Expand Down
6 changes: 6 additions & 0 deletions base/linalg/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ end
ctranspose(A::Hermitian) = A
trace(A::Hermitian) = real(trace(A.data))

#tril/triu
tril(A::Hermitian,k::Integer=0) = tril(A.data,k)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these are right. Aren't half the elements in A.data potentially garbage?

julia> A = rand(4,4)
4x4 Array{Float64,2}:
 0.992492  0.88037   0.40931   0.311544
 0.623913  0.93559   0.310044  0.598238
 0.393736  0.051534  0.103072  0.74386
 0.747731  0.684462  0.813315  0.0329609

julia> Symmetric(A)
tril(4x4 Base.LinAlg.Symmetric{Float64,Array{Float64,2}}:
 0.992492  0.88037   0.40931   0.311544
 0.88037   0.93559   0.310044  0.598238
 0.40931   0.310044  0.103072  0.74386
 0.311544  0.598238  0.74386   0.0329609

julia> tril(Symmetric(A))
4x4 Array{Float64,2}:
 0.992492  0.0       0.0       0.0
 0.623913  0.93559   0.0       0.0
 0.393736  0.051534  0.103072  0.0
 0.747731  0.684462  0.813315  0.0329609

julia> full(Symmetric(A))
4x4 Array{Float64,2}:
 0.992492  0.88037   0.40931   0.311544
 0.88037   0.93559   0.310044  0.598238
 0.40931   0.310044  0.103072  0.74386
 0.311544  0.598238  0.74386   0.0329609

julia> tril(full(Symmetric(A)))
4x4 Array{Float64,2}:
 0.992492  0.0       0.0       0.0
 0.88037   0.93559   0.0       0.0
 0.40931   0.310044  0.103072  0.0
 0.311544  0.598238  0.74386   0.0329609

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow. K, on it.

The lower triangle is garbage.

triu(A::Hermitian,k::Integer=0) = triu(A.data,k)
tril(A::Symmetric,k::Integer=0) = tril(A.data,k)
triu(A::Symmetric,k::Integer=0) = triu(A.data,k)

## Matvec
A_mul_B!{T<:BlasFloat,S<:StridedMatrix}(y::StridedVector{T}, A::Symmetric{T,S}, x::StridedVector{T}) = BLAS.symv!(A.uplo, one(T), A.data, x, zero(T), y)
A_mul_B!{T<:BlasComplex,S<:StridedMatrix}(y::StridedVector{T}, A::Hermitian{T,S}, x::StridedVector{T}) = BLAS.hemv!(A.uplo, one(T), A.data, x, zero(T), y)
Expand Down
60 changes: 60 additions & 0 deletions base/linalg/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,66 @@ istril(A::UnitLowerTriangular) = true
istriu(A::UpperTriangular) = true
istriu(A::UnitUpperTriangular) = true

function tril(A::UpperTriangular,k::Integer=0)
n = size(A,1)
if abs(k) > n
throw(ArgumentError("requested diagonal, $k, out of bounds in matrix of size ($n,$n)"))
elseif k < 0
return UpperTriangular(zeros(A.data))
elseif k == 0
return UpperTriangular(diagm(diag(A)))
else
return UpperTriangular(triu(tril(A.data,k)))
end
end

triu(A::UpperTriangular,k::Integer=0) = UpperTriangular(triu(triu(A.data),k))

function tril(A::UnitUpperTriangular,k::Integer=0)
n = size(A,1)
if abs(k) > n
throw(ArgumentError("requested diagonal, $k, out of bounds in matrix of size ($n,$n)"))
elseif k < 0
return UnitUpperTriangular(zeros(A.data))
elseif k == 0
return UnitUpperTriangular(eye(A))
else
return UnitUpperTriangular(triu(tril(A.data,k)))
end
end

triu(A::UnitUpperTriangular,k::Integer=0) = UnitUpperTriangular(triu(triu(A.data),k))

function triu(A::LowerTriangular,k::Integer=0)
n = size(A,1)
if abs(k) > n
throw(ArgumentError("requested diagonal, $k, out of bounds in matrix of size ($n,$n)"))
elseif k > 0
return LowerTriangular(zeros(A.data))
elseif k == 0
return LowerTriangular(diagm(diag(A)))
else
return LowerTriangular(tril(triu(A.data,k)))
end
end

tril(A::LowerTriangular,k::Integer=0) = LowerTriangular(tril(tril(A.data),k))

function triu(A::UnitLowerTriangular,k::Integer=0)
n = size(A,1)
if abs(k) > n
throw(ArgumentError("requested diagonal, $k, out of bounds in matrix of size ($n,$n)"))
elseif k > 0
return UnitLowerTriangular(zeros(A.data))
elseif k == 0
return UnitLowerTriangular(eye(A))
else
return UnitLowerTriangular(tril(triu(A.data,k)))
end
end

tril(A::UnitLowerTriangular,k::Integer=0) = UnitLowerTriangular(tril(tril(A.data),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
70 changes: 70 additions & 0 deletions base/linalg/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,41 @@ eigmin(A::SymTridiagonal) = eigvals(A, 1:1)[1]
eigvecs(A::SymTridiagonal) = eigfact(A)[:vectors]
eigvecs{T<:BlasFloat,Eigenvalue<:Real}(A::SymTridiagonal{T}, eigvals::Vector{Eigenvalue}) = LAPACK.stein!(A.dv, A.ev, eigvals)

#tril and triu

istriu(M::SymTridiagonal) = all(M.ev .== 0)
istril(M::SymTridiagonal) = all(M.ev .== 0)

function tril(M::SymTridiagonal, k::Integer=0)
n = length(M.dv)
if abs(k) > n
throw(ArgumentError("requested diagonal, $k, out of bounds in matrix of size ($n,$n)"))
elseif k < -1
return SymTridiagonal(zeros(M.dv),zeros(M.ev))
elseif k == -1
return Tridiagonal(M.ev,zeros(M.dv),zeros(M.ev))
elseif k == 0
return Tridiagonal(M.ev,M.dv,zeros(M.ev))
elseif k >= 1
return M
end
end

function triu(M::SymTridiagonal, k::Integer=0)
n = length(M.dv)
if abs(k) > n
throw(ArgumentError("requested diagonal, $k, out of bounds in matrix of size ($n,$n)"))
elseif k > 1
return SymTridiagonal(zeros(M.dv),zeros(M.ev))
elseif k == 1
return Tridiagonal(zeros(M.ev),zeros(M.dv),M.ev)
elseif k == 0
return Tridiagonal(zeros(M.ev),M.dv,M.ev)
elseif k <= -1
return M
end
end

###################
# Generic methods #
###################
Expand Down Expand Up @@ -319,6 +354,41 @@ function getindex{T}(A::Tridiagonal{T}, i::Integer, j::Integer)
end
end

#tril and triu

istriu(M::Tridiagonal) = all(M.ev .== 0)
istril(M::Tridiagonal) = all(M.ev .== 0)

function tril(M::Tridiagonal, k::Integer=0)
n = length(M.d)
if abs(k) > n
throw(ArgumentError("requested diagonal, $k, out of bounds in matrix of size ($n,$n)"))
elseif k < -1
return Tridiagonal(zeros(M.dl),zeros(M.d),zeros(M.du))
elseif k == -1
return Tridiagonal(M.dl,zeros(M.d),zeros(M.du))
elseif k == 0
return Tridiagonal(M.dl,M.d,zeros(M.du))
elseif k >= 1
return M
end
end

function triu(M::Tridiagonal, k::Integer=0)
n = length(M.d)
if abs(k) > n
throw(ArgumentError("requested diagonal, $k, out of bounds in matrix of size ($n,$n)"))
elseif k > 1
return Tridiagonal(zeros(M.dl),zeros(M.d),zeros(M.du))
elseif k == 1
return Tridiagonal(zeros(M.dl),zeros(M.d),M.du)
elseif k == 0
return Tridiagonal(zeros(M.dl),M.d,M.du)
elseif k <= -1
return M
end
end

###################
# Generic methods #
###################
Expand Down
22 changes: 22 additions & 0 deletions test/linalg/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,28 @@ for relty in (Float32, Float64, BigFloat), elty in (relty, Complex{relty})
@test func(func(T)) == T
end

debug && println("triu and tril")
@test tril(Bidiagonal(dv,ev,'U'),-1) == Bidiagonal(zeros(dv),zeros(ev),'U')
@test tril(Bidiagonal(dv,ev,'L'),-1) == Bidiagonal(zeros(dv),ev,'L')
@test tril(Bidiagonal(dv,ev,'U'),-2) == Bidiagonal(zeros(dv),zeros(ev),'U')
@test tril(Bidiagonal(dv,ev,'L'),-2) == Bidiagonal(zeros(dv),zeros(ev),'L')
@test tril(Bidiagonal(dv,ev,'U'),1) == Bidiagonal(dv,ev,'U')
@test tril(Bidiagonal(dv,ev,'L'),1) == Bidiagonal(dv,ev,'L')
@test tril(Bidiagonal(dv,ev,'U')) == Bidiagonal(dv,zeros(ev),'U')
@test tril(Bidiagonal(dv,ev,'L')) == Bidiagonal(dv,ev,'L')
@test_throws ArgumentError tril(Bidiagonal(dv,ev,'U'),n+1)

@test triu(Bidiagonal(dv,ev,'L'),1) == Bidiagonal(zeros(dv),zeros(ev),'L')
@test triu(Bidiagonal(dv,ev,'U'),1) == Bidiagonal(zeros(dv),ev,'U')
@test triu(Bidiagonal(dv,ev,'U'),2) == Bidiagonal(zeros(dv),zeros(ev),'U')
@test triu(Bidiagonal(dv,ev,'L'),2) == Bidiagonal(zeros(dv),zeros(ev),'L')
@test triu(Bidiagonal(dv,ev,'U'),-1) == Bidiagonal(dv,ev,'U')
@test triu(Bidiagonal(dv,ev,'L'),-1) == Bidiagonal(dv,ev,'L')
@test triu(Bidiagonal(dv,ev,'L')) == Bidiagonal(dv,zeros(ev),'L')
@test triu(Bidiagonal(dv,ev,'U')) == Bidiagonal(dv,ev,'U')
@test_throws ArgumentError triu(Bidiagonal(dv,ev,'U'),n+1)


debug && println("Linear solver")
Tfull = full(T)
condT = cond(map(Complex128,Tfull))
Expand Down
8 changes: 4 additions & 4 deletions test/linalg/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ for relty in (Float32, Float64, BigFloat), elty in (relty, Complex{relty})
@test_approx_eq D/D2 Diagonal(D.diag./D2.diag)

# test triu/tril
@test triu!(copy(D),1) == zeros(D)
@test triu!(copy(D),0) == D
@test tril!(copy(D),1) == zeros(D)
@test tril!(copy(D),0) == D
@test triu(D,1) == zeros(D)
@test triu(D,0) == D
@test tril(D,1) == zeros(D)
@test tril(D,0) == D

# factorize
@test factorize(D) == D
Expand Down
6 changes: 6 additions & 0 deletions test/linalg/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ let n=10
@test ctranspose(Symmetric(asym)) == Symmetric(conj(asym))
@test ctranspose(Hermitian(asym)) == asym

#tril/triu
@test triu(Symmetric(asym),1) == triu(asym,1)
@test tril(Symmetric(asym),1) == tril(asym,1)
@test triu(Hermitian(asym),1) == triu(asym,1)
@test tril(Hermitian(asym),1) == tril(asym,1)

eltya == BigFloat && continue # Revisit when implemented in julia
d, v = eig(asym)
@test_approx_eq asym*v[:,1] d[1]*v[:,1]
Expand Down
19 changes: 19 additions & 0 deletions test/linalg/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,25 @@ for elty1 in (Float32, Float64, Complex64, Complex128, BigFloat, Int)
@test !istril(A1)
end

#tril/triu
if uplo1 == :L
@test tril(A1) == A1
@test tril(A1,-1) == t1(tril(full(A1),-1))
@test tril(A1,1) == t1(tril(tril(full(A1),1)))
@test_throws ArgumentError tril(A1,n+1)
@test triu(A1) == t1(diagm(diag(A1)))
@test triu(A1,-1) == t1(tril(triu(A1.data,-1)))
@test_throws ArgumentError triu(A1,n+1)
else
@test triu(A1) == A1
@test triu(A1,1) == t1(triu(full(A1),1))
@test triu(A1,-1) == t1(triu(triu(full(A1),-1)))
@test_throws ArgumentError triu(A1,n+1)
@test tril(A1) == t1(diagm(diag(A1)))
@test tril(A1,1) == t1(triu(tril(A1.data,1)))
@test_throws ArgumentError tril(A1,n+1)
end

# factorize
@test factorize(A1) == A1

Expand Down
19 changes: 19 additions & 0 deletions test/linalg/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,25 @@ for elty in (Float32, Float64, Complex64, Complex128, Int)

# issue #1490
@test_approx_eq_eps det(ones(elty, 3,3)) zero(elty) 3*eps(real(one(elty)))

#tril/triu
@test_throws ArgumentError tril(SymTridiagonal(d,dl),n+1)
@test_throws ArgumentError tril(Tridiagonal(dl,d,du),n+1)
@test tril(SymTridiagonal(d,dl)) == Tridiagonal(dl,d,zeros(dl))
@test tril(SymTridiagonal(d,dl),1) == Tridiagonal(dl,d,dl)
@test tril(SymTridiagonal(d,dl),-1) == Tridiagonal(dl,zeros(d),zeros(dl))
@test tril(Tridiagonal(dl,d,du)) == Tridiagonal(dl,d,zeros(du))
@test tril(Tridiagonal(dl,d,du),1) == Tridiagonal(dl,d,du)
@test tril(Tridiagonal(dl,d,du),-1) == Tridiagonal(dl,zeros(d),zeros(du))

@test_throws ArgumentError triu(SymTridiagonal(d,dl),n+1)
@test_throws ArgumentError triu(Tridiagonal(dl,d,du),n+1)
@test triu(SymTridiagonal(d,dl)) == Tridiagonal(zeros(dl),d,dl)
@test triu(SymTridiagonal(d,dl),-1) == Tridiagonal(dl,d,dl)
@test triu(SymTridiagonal(d,dl),1) == Tridiagonal(zeros(dl),zeros(d),dl)
@test triu(Tridiagonal(dl,d,du)) == Tridiagonal(zeros(dl),d,du)
@test triu(Tridiagonal(dl,d,du),-1) == Tridiagonal(dl,d,du)
@test triu(Tridiagonal(dl,d,du),1) == Tridiagonal(zeros(dl),zeros(d),du)
end
end

Expand Down