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

yak shaving, full deprecation style #23838

Merged
merged 3 commits into from
Sep 26, 2017
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
77 changes: 61 additions & 16 deletions base/linalg/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -984,9 +984,9 @@ end
ishermitian(x::Number) = (x == conj(x))

"""
istriu(A) -> Bool
istriu(A::AbstractMatrix, k::Integer = 0) -> Bool

Test whether a matrix is upper triangular.
Test whether `A` is upper triangular starting from the `k`th superdiagonal.

# Examples
```jldoctest
Expand All @@ -998,29 +998,36 @@ julia> a = [1 2; 2 -1]
julia> istriu(a)
false

julia> istriu(a, -1)
true

julia> b = [1 im; 0 -1]
2×2 Array{Complex{Int64},2}:
1+0im 0+1im
0+0im -1+0im

julia> istriu(b)
true

julia> istriu(b, 1)
false
```
"""
function istriu(A::AbstractMatrix)
function istriu(A::AbstractMatrix, k::Integer = 0)
m, n = size(A)
for j = 1:min(n,m-1), i = j+1:m
if !iszero(A[i,j])
return false
for j in 1:min(n, m + k - 1)
for i in max(1, j - k + 1):m
iszero(A[i, j]) || return false
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe @inbounds?

Copy link
Member Author

Choose a reason for hiding this comment

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

I am not certain this is safe for arbitrary AbstractArrays, for example OffsetArrays, and so hesitate to add an @inbounds?

Copy link
Member

Choose a reason for hiding this comment

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

Eventually, these functions should use either be restricted to StridedMatrix or the index calculations be changed to work with OffsetArrays.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good call :). Do you have thoughts on the best way to generalize these loops? Thanks!

Copy link
Member

Choose a reason for hiding this comment

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

The easiest might be to reuse the existing loops and then use the i and j to index into the actual indices for the array, e.g. ii = indices(A, 1)[i], and then index with ii and jj.

end
end
return true
end
istriu(x::Number) = true

"""
istril(A) -> Bool
istril(A::AbstractMatrix, k::Integer = 0) -> Bool

Test whether a matrix is lower triangular.
Test whether `A` is lower triangular starting from the `k`th superdiagonal.

# Examples
```jldoctest
Expand All @@ -1032,24 +1039,64 @@ julia> a = [1 2; 2 -1]
julia> istril(a)
false

julia> istril(a, 1)
true

julia> b = [1 0; -im -1]
2×2 Array{Complex{Int64},2}:
1+0im 0+0im
0-1im -1+0im

julia> istril(b)
true

julia> istril(b, -1)
false
```
"""
function istril(A::AbstractMatrix)
function istril(A::AbstractMatrix, k::Integer = 0)
m, n = size(A)
for j = 2:n, i = 1:min(j-1,m)
if !iszero(A[i,j])
return false
for j in max(1, k + 2):n
for i in 1:min(j - k - 1, m)
iszero(A[i, j]) || return false
end
end
return true
end
istril(x::Number) = true

"""
isbanded(A::AbstractMatrix, kl::Integer, ku::Integer) -> Bool

Test whether `A` is banded with lower bandwidth starting from the `kl`th superdiagonal
and upper bandwidth extending through the `ku`th superdiagonal.

# Examples
```jldoctest
julia> a = [1 2; 2 -1]
2×2 Array{Int64,2}:
1 2
2 -1

julia> isbanded(a, 0, 0)
false

julia> isbanded(a, -1, 1)
true

julia> b = [1 0; -im -1] # lower bidiagonal
2×2 Array{Complex{Int64},2}:
1+0im 0+0im
0-1im -1+0im

julia> isbanded(b, 0, 0)
false

julia> isbanded(b, -1, 0)
true
```
"""
isbanded(A::AbstractMatrix, kl::Integer, ku::Integer) = istriu(A, kl) && istril(A, ku)

"""
isdiag(A) -> Bool
Expand All @@ -1075,12 +1122,10 @@ julia> isdiag(b)
true
```
"""
isdiag(A::AbstractMatrix) = istril(A) && istriu(A)

istriu(x::Number) = true
istril(x::Number) = true
isdiag(A::AbstractMatrix) = isbanded(A, 0, 0)
isdiag(x::Number) = true


"""
linreg(x, y)

Expand Down
100 changes: 34 additions & 66 deletions base/linalg/special.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

# Methods operating on different special matrix types


# Interconversion between special matrix types

# conversions from Diagonal to other special matrix types
convert(::Type{Bidiagonal}, A::Diagonal) =
Bidiagonal(A.diag, fill!(similar(A.diag, length(A.diag)-1), 0), :U)
convert(::Type{SymTridiagonal}, A::Diagonal) =
Expand All @@ -11,88 +14,53 @@ convert(::Type{Tridiagonal}, A::Diagonal) =
Tridiagonal(fill!(similar(A.diag, length(A.diag)-1), 0), A.diag,
fill!(similar(A.diag, length(A.diag)-1), 0))

function convert(::Type{Diagonal}, A::Union{Bidiagonal, SymTridiagonal})
if !iszero(A.ev)
# conversions from Bidiagonal to other special matrix types
convert(::Type{Diagonal}, A::Bidiagonal) =
iszero(A.ev) ? Diagonal(A.dv) :
throw(ArgumentError("matrix cannot be represented as Diagonal"))
end
Diagonal(A.dv)
end

function convert(::Type{SymTridiagonal}, A::Bidiagonal)
if !iszero(A.ev)
convert(::Type{SymTridiagonal}, A::Bidiagonal) =
iszero(A.ev) ? SymTridiagonal(A.dv, A.ev) :
throw(ArgumentError("matrix cannot be represented as SymTridiagonal"))
end
SymTridiagonal(A.dv, A.ev)
end

convert(::Type{Tridiagonal}, A::Bidiagonal{T}) where {T} =
convert(::Type{Tridiagonal}, A::Bidiagonal) =
Tridiagonal(A.uplo == 'U' ? fill!(similar(A.ev), 0) : A.ev, A.dv,
A.uplo == 'U' ? A.ev : fill!(similar(A.ev), 0))

function convert(::Type{Bidiagonal}, A::SymTridiagonal)
if !iszero(A.ev)
# conversions from SymTridiagonal to other special matrix types
convert(::Type{Diagonal}, A::SymTridiagonal) =
iszero(A.ev) ? Diagonal(A.dv) :
throw(ArgumentError("matrix cannot be represented as Diagonal"))
convert(::Type{Bidiagonal}, A::SymTridiagonal) =
iszero(A.ev) ? Bidiagonal(A.dv, A.ev, :U) :
throw(ArgumentError("matrix cannot be represented as Bidiagonal"))
end
Bidiagonal(A.dv, A.ev, :U)
end
convert(::Type{Tridiagonal}, A::SymTridiagonal) =
Tridiagonal(copy(A.ev), A.dv, A.ev)

function convert(::Type{Diagonal}, A::Tridiagonal)
if !(iszero(A.dl) && iszero(A.du))
# conversions from Tridiagonal to other special matrix types
convert(::Type{Diagonal}, A::Tridiagonal) =
iszero(A.dl) && iszero(A.du) ? Diagonal(A.d) :
throw(ArgumentError("matrix cannot be represented as Diagonal"))
end
Diagonal(A.d)
end

function convert(::Type{Bidiagonal}, A::Tridiagonal)
if iszero(A.dl)
return Bidiagonal(A.d, A.du, :U)
elseif iszero(A.du)
return Bidiagonal(A.d, A.dl, :L)
else
convert(::Type{Bidiagonal}, A::Tridiagonal) =
iszero(A.dl) ? Bidiagonal(A.d, A.du, :U) :
iszero(A.du) ? Bidiagonal(A.d, A.dl, :L) :
throw(ArgumentError("matrix cannot be represented as Bidiagonal"))
end
end

function convert(::Type{SymTridiagonal}, A::Tridiagonal)
if A.dl != A.du
convert(::Type{SymTridiagonal}, A::Tridiagonal) =
A.dl == A.du ? SymTridiagonal(A.d, A.dl) :
throw(ArgumentError("matrix cannot be represented as SymTridiagonal"))
end
SymTridiagonal(A.d, A.dl)
end

function convert(::Type{Tridiagonal}, A::SymTridiagonal)
Tridiagonal(copy(A.ev), A.dv, A.ev)
end

function convert(::Type{Diagonal}, A::AbstractTriangular)
if !isdiag(A)
# conversions from AbstractTriangular to special matrix types
convert(::Type{Diagonal}, A::AbstractTriangular) =
isdiag(A) ? Diagonal(diag(A)) :
throw(ArgumentError("matrix cannot be represented as Diagonal"))
end
Diagonal(diag(A))
end

function convert(::Type{Bidiagonal}, A::AbstractTriangular)
fA = full(A)
if fA == diagm(diag(A)) + diagm(diag(fA, 1), 1)
return Bidiagonal(diag(A), diag(fA,1), :U)
elseif fA == diagm(diag(A)) + diagm(diag(fA, -1), -1)
return Bidiagonal(diag(A), diag(fA,-1), :L)
else
convert(::Type{Bidiagonal}, A::AbstractTriangular) =
isbanded(A, 0, 1) ? Bidiagonal(diag(A), diag(A, 1), :U) : # is upper bidiagonal
isbanded(A, -1, 0) ? Bidiagonal(diag(A), diag(A, -1), :L) : # is lower bidiagonal
throw(ArgumentError("matrix cannot be represented as Bidiagonal"))
end
end

convert(::Type{SymTridiagonal}, A::AbstractTriangular) =
convert(SymTridiagonal, convert(Tridiagonal, A))

function convert(::Type{Tridiagonal}, A::AbstractTriangular)
fA = full(A)
if fA == diagm(diag(A)) + diagm(diag(fA, 1), 1) + diagm(diag(fA, -1), -1)
return Tridiagonal(diag(fA, -1), diag(A), diag(fA,1))
else
convert(::Type{Tridiagonal}, A::AbstractTriangular) =
isbanded(A, -1, 1) ? Tridiagonal(diag(A, -1), diag(A), diag(A, 1)) : # is tridiagonal
throw(ArgumentError("matrix cannot be represented as Tridiagonal"))
end
end


# Constructs two method definitions taking into account (assumed) commutativity
# e.g. @commutative f(x::S, y::T) where {S,T} = x+y is the same is defining
Expand Down
54 changes: 54 additions & 0 deletions test/linalg/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,57 @@ end
@test_throws ErrorException adjoint(rand(2,2,2,2))
@test_throws ErrorException transpose(rand(2,2,2,2))
end

@testset "generic functions for checking whether matrices have banded structure" begin
using Base.LinAlg: isbanded
pentadiag = [1 2 3; 4 5 6; 7 8 9]
tridiag = [1 2 0; 4 5 6; 0 8 9]
ubidiag = [1 2 0; 0 5 6; 0 0 9]
lbidiag = [1 0 0; 4 5 0; 0 8 9]
adiag = [1 0 0; 0 5 0; 0 0 9]
@testset "istriu" begin
@test !istriu(pentadiag)
@test istriu(pentadiag, -2)
@test !istriu(tridiag)
@test istriu(tridiag, -1)
@test istriu(ubidiag)
@test !istriu(ubidiag, 1)
@test !istriu(lbidiag)
@test istriu(lbidiag, -1)
@test istriu(adiag)
end
@testset "istril" begin
@test !istril(pentadiag)
@test istril(pentadiag, 2)
@test !istril(tridiag)
@test istril(tridiag, 1)
@test !istril(ubidiag)
@test istril(ubidiag, 1)
@test istril(lbidiag)
@test !istril(lbidiag, -1)
@test istril(adiag)
end
@testset "isbanded" begin
@test isbanded(pentadiag, -2, 2)
@test !isbanded(pentadiag, -1, 2)
@test !isbanded(pentadiag, -2, 1)
@test isbanded(tridiag, -1, 1)
@test !isbanded(tridiag, 0, 1)
@test !isbanded(tridiag, -1, 0)
@test isbanded(ubidiag, 0, 1)
@test !isbanded(ubidiag, 1, 1)
@test !isbanded(ubidiag, 0, 0)
@test isbanded(lbidiag, -1, 0)
@test !isbanded(lbidiag, 0, 0)
@test !isbanded(lbidiag, -1, -1)
@test isbanded(adiag, 0, 0)
@test !isbanded(adiag, -1, -1)
@test !isbanded(adiag, 1, 1)
end
@testset "isdiag" begin
@test !isdiag(tridiag)
@test !isdiag(ubidiag)
@test !isdiag(lbidiag)
@test isdiag(adiag)
end
end