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

WIP:Make LinAlg more type stable #8796

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 6 additions & 1 deletion base/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export
Triangular,
Diagonal,
UniformScaling,
Token,

# Functions
axpy!,
Expand Down Expand Up @@ -189,11 +190,15 @@ macro chkuplo()
Valid choices are 'U' (upper) or 'L' (lower).""")))
end

immutable Token{T}
end

char_uplo(::Type{Token{:U}}) = 'U'
char_uplo(::Type{Token{:L}}) = 'L'
const CHARU = 'U'
const CHARL = 'L'
char_uplo(uplo::Symbol) = uplo == :U ? CHARU : (uplo == :L ? CHARL : throw(ArgumentError("uplo argument must be either :U or :L")))


include("linalg/exceptions.jl")
include("linalg/generic.jl")

Expand Down
125 changes: 68 additions & 57 deletions base/linalg/cholesky.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,86 +13,97 @@ immutable CholeskyPivoted{T} <: Factorization{T}
info::BlasInt
end

function chol!{T<:BlasFloat}(A::StridedMatrix{T}, uplo::Symbol=:U)
C, info = LAPACK.potrf!(char_uplo(uplo), A)
return @assertposdef Triangular{eltype(C),typeof(C),uplo,false}(C) info
for (uplos, uploc) in ((:(:U), 'U'), (:(:L), 'L'))
@eval begin
function chol!{T<:BlasFloat}(A::StridedMatrix{T}, ::Type{Token{$uplos}})
C, info = LAPACK.potrf!($uploc, A)
return @assertposdef Triangular{eltype(C),typeof(C),$uplos,false}(C) info
end
end
end

function chol!{T}(A::AbstractMatrix{T}, uplo::Symbol=:U)
function chol!{T}(A::AbstractMatrix{T}, ::Type{Token{:L}})
n = chksquare(A)
@inbounds begin
if uplo == :L
for k = 1:n
for i = 1:k - 1
A[k,k] -= A[k,i]*A[k,i]'
end
A[k,k] = chol!(A[k,k], uplo)
AkkInv = inv(A[k,k]')
for j = 1:k
for i = k + 1:n
j == 1 && (A[i,k] = A[i,k]*AkkInv)
j < k && (A[i,k] -= A[i,j]*A[k,j]'*AkkInv)
end
for k = 1:n
for i = 1:k - 1
A[k,k] -= A[k,i]*A[k,i]'
end
A[k,k] = chol!(A[k,k], Token{:L})
AkkInv = inv(A[k,k]')
for j = 1:k
for i = k + 1:n
j == 1 && (A[i,k] = A[i,k]*AkkInv)
j < k && (A[i,k] -= A[i,j]*A[k,j]'*AkkInv)
end
end
elseif uplo == :U
for k = 1:n
end
end
return Triangular{T,typeof(A),:L,false}(A)
end
function chol!{T}(A::AbstractMatrix{T}, ::Type{Token{:U}})
n = chksquare(A)
@inbounds begin
for k = 1:n
for i = 1:k - 1
A[k,k] -= A[i,k]'A[i,k]
end
A[k,k] = chol!(A[k,k], Token{:U})
AkkInv = inv(A[k,k])
for j = k + 1:n
for i = 1:k - 1
A[k,k] -= A[i,k]'A[i,k]
end
A[k,k] = chol!(A[k,k], uplo)
AkkInv = inv(A[k,k])
for j = k + 1:n
for i = 1:k - 1
A[k,j] -= A[i,k]'A[i,j]
end
A[k,j] = A[k,k]'\A[k,j]
A[k,j] -= A[i,k]'A[i,j]
end
A[k,j] = A[k,k]'\A[k,j]
end
else
throw(ArgumentError("uplo must be either :U or :L but was $(uplo)"))
end
end
return Triangular(A, uplo, false)
return Triangular{T,typeof(A),:U,false}(A)
end
chol!(A::AbstractMatrix) = chol!(A, Token{:U})

function cholfact!{T<:BlasFloat}(A::StridedMatrix{T}, uplo::Symbol=:U; pivot=false, tol=0.0)
uplochar = char_uplo(uplo)
if pivot
A, piv, rank, info = LAPACK.pstrf!(uplochar, A, tol)
return CholeskyPivoted{T}(A, uplochar, piv, rank, tol, info)
end
return Cholesky{T,typeof(A),uplo}(chol!(A, uplo).data)
cholfact!{T<:BlasFloat,uplo}(A::StridedMatrix{T}, ::Type{Token{uplo}}) = Cholesky{T,typeof(A),uplo}(chol!(A, Token{uplo}).data)
function cholfact!{T<:BlasFloat,uplo}(A::StridedMatrix{T}, ::Type{Token{uplo}}, ::Type{Token{:pivot}}; tol = 0.0)
uplochar = char_uplo(Token{uplo})
A, piv, rank, info = LAPACK.pstrf!(uplochar, A, tol)
return CholeskyPivoted{T}(A, uplochar, piv, rank, tol, info)
end
cholfact!(A::AbstractMatrix, uplo::Symbol=:U) = Cholesky{eltype(A),typeof(A),uplo}(chol!(A, uplo).data)
cholfact!{T<:BlasFloat}(A::StridedMatrix{T}, ::Type{Token{:pivot}}; tol = 0.0) = cholfact!(A, Token{:U}, Token{:pivot}, tol = tol)

cholfact!{UpLo}(A::AbstractMatrix, ::Type{Token{UpLo}}) = Cholesky{eltype(A),typeof(A),UpLo}(chol!(A, Token{UpLo}).data)
cholfact!(A::AbstractMatrix) = cholfact!(A, Token{:U})

function cholfact!{T<:BlasFloat,S,UpLo}(C::Cholesky{T,S,UpLo})
_, info = LAPACK.potrf!(char_uplo(UpLo), C.UL)
info[1]>0 && throw(PosDefException(info[1]))
C
end

cholfact{T<:BlasFloat}(A::StridedMatrix{T}, uplo::Symbol=:U; pivot=false, tol=0.0) = cholfact!(copy(A), uplo, pivot=pivot, tol=tol)
function cholfact{T}(A::StridedMatrix{T}, uplo::Symbol=:U; pivot=false, tol=0.0)
cholfact{T<:BlasFloat}(A::StridedMatrix{T}, args...; kwargs...) = cholfact!(copy(A), args...; kwargs...)
function cholfact{T}(A::AbstractMatrix{T}, args...; kwargs...)
S = promote_type(typeof(chol(one(T))),Float32)
S <: BlasFloat && return cholfact!(convert(AbstractMatrix{S}, A), uplo, pivot = pivot, tol = tol)
pivot && throw(ArgumentError("pivot only supported for Float32, Float64, Complex{Float32} and Complex{Float64}"))
S != T && return cholfact!(convert(AbstractMatrix{S}, A), uplo)
return cholfact!(copy(A), uplo)
# S <: BlasFloat && return cholfact!(convert(AbstractMatrix{S}, A), args...; kwargs...)
# pivot && throw(ArgumentError("pivot only supported for Float32, Float64, Complex{Float32} and Complex{Float64}"))
S != T && return cholfact!(convert(AbstractMatrix{S}, A), args..., kwargs...)
return cholfact!(copy(A), args...; kwargs...)
end
function cholfact(x::Number, uplo::Symbol=:U)
function cholfact(x::Number, ::Type{Token{:U}})
xf = fill(chol!(x), 1, 1)
Cholesky{:U, eltype(xf), typeof(xf)}(xf)
end
function cholfact(x::Number, ::Type{Token{:L}})
xf = fill(chol!(x), 1, 1)
Cholesky{:L, eltype(xf), typeof(xf)}(xf)
end

chol(A::AbstractMatrix, uplo::Symbol=:U) = chol!(copy(A), uplo)
function chol!(x::Number, uplo::Symbol=:U)
chol(A::AbstractMatrix, args...) = chol!(copy(A), args...)
function chol!(x::Number, args...)
rx = real(x)
rx == abs(x) || throw(DomainError())
rxr = sqrt(rx)
convert(promote_type(typeof(x), typeof(rxr)), rxr)
end
chol(x::Number, uplo::Symbol=:U) = chol!(x, uplo)
chol(x::Number, args...) = chol!(x, args...)

function convert{Tnew,Told,S,UpLo}(::Type{Cholesky{Tnew}},C::Cholesky{Told,S,UpLo})
Cnew = convert(AbstractMatrix{Tnew}, C.UL)
Expand All @@ -113,14 +124,14 @@ size(C::Union(Cholesky, CholeskyPivoted)) = size(C.UL)
size(C::Union(Cholesky, CholeskyPivoted), d::Integer) = size(C.UL,d)

function getindex{T,S,UpLo}(C::Cholesky{T,S,UpLo}, d::Symbol)
d == :U && return Triangular(UpLo == d ? C.UL : C.UL',:U)
d == :L && return Triangular(UpLo == d ? C.UL : C.UL',:L)
d == :UL && return Triangular(C.UL, UpLo)
d == :U && return Triangular(UpLo == d ? C.UL : C.UL', Token{:U})
d == :L && return Triangular(UpLo == d ? C.UL : C.UL', Token{:L})
d == :UL && return Triangular(C.UL, Token{UpLo})
throw(KeyError(d))
end
function getindex{T<:BlasFloat}(C::CholeskyPivoted{T}, d::Symbol)
d == :U && return Triangular(symbol(C.uplo) == d ? C.UL : C.UL', :U)
d == :L && return Triangular(symbol(C.uplo) == d ? C.UL : C.UL', :L)
d == :U && return Triangular(symbol(C.uplo) == d ? C.UL : C.UL', Token{:U})
d == :L && return Triangular(symbol(C.uplo) == d ? C.UL : C.UL', Token{:L})
d == :p && return C.piv
if d == :P
n = size(C, 1)
Expand All @@ -137,8 +148,8 @@ show{T,S<:AbstractMatrix,UpLo}(io::IO, C::Cholesky{T,S,UpLo}) = (println("$(type

A_ldiv_B!{T<:BlasFloat,S<:AbstractMatrix}(C::Cholesky{T,S,:U}, B::StridedVecOrMat{T}) = LAPACK.potrs!('U', C.UL, B)
A_ldiv_B!{T<:BlasFloat,S<:AbstractMatrix}(C::Cholesky{T,S,:L}, B::StridedVecOrMat{T}) = LAPACK.potrs!('L', C.UL, B)
A_ldiv_B!{T,S<:AbstractMatrix}(C::Cholesky{T,S,:L}, B::StridedVecOrMat) = Ac_ldiv_B!(Triangular(C.UL, :L, false), A_ldiv_B!(Triangular(C.UL, :L, false), B))
A_ldiv_B!{T,S<:AbstractMatrix}(C::Cholesky{T,S,:U}, B::StridedVecOrMat) = A_ldiv_B!(Triangular(C.UL, :U, false), Ac_ldiv_B!(Triangular(C.UL, :U, false), B))
A_ldiv_B!{T,S<:AbstractMatrix}(C::Cholesky{T,S,:L}, B::StridedVecOrMat) = Ac_ldiv_B!(Triangular(C.UL, Token{:L}), A_ldiv_B!(Triangular(C.UL, Token{:L}), B))
A_ldiv_B!{T,S<:AbstractMatrix}(C::Cholesky{T,S,:U}, B::StridedVecOrMat) = A_ldiv_B!(Triangular(C.UL, Token{:U}), Ac_ldiv_B!(Triangular(C.UL, Token{:U}), B))

function A_ldiv_B!{T<:BlasFloat}(C::CholeskyPivoted{T}, B::StridedVector{T})
chkfullrank(C)
Expand All @@ -156,8 +167,8 @@ function A_ldiv_B!{T<:BlasFloat}(C::CholeskyPivoted{T}, B::StridedMatrix{T})
end
B
end
A_ldiv_B!(C::CholeskyPivoted, B::StridedVector) = C.uplo=='L' ? Ac_ldiv_B!(Triangular(C.UL, symbol(C.uplo), false), A_ldiv_B!(Triangular(C.UL, symbol(C.uplo), false), B[C.piv]))[invperm(C.piv)] : A_ldiv_B!(Triangular(C.UL, symbol(C.uplo), false), Ac_ldiv_B!(Triangular(C.UL, symbol(C.uplo), false), B[C.piv]))[invperm(C.piv)]
A_ldiv_B!(C::CholeskyPivoted, B::StridedMatrix) = C.uplo=='L' ? Ac_ldiv_B!(Triangular(C.UL, symbol(C.uplo), false), A_ldiv_B!(Triangular(C.UL, symbol(C.uplo), false), B[C.piv,:]))[invperm(C.piv),:] : A_ldiv_B!(Triangular(C.UL, symbol(C.uplo), false), Ac_ldiv_B!(Triangular(C.UL, symbol(C.uplo), false), B[C.piv,:]))[invperm(C.piv),:]
A_ldiv_B!(C::CholeskyPivoted, B::StridedVector) = C.uplo == 'L' ? Ac_ldiv_B!(Triangular(C.UL, Token{symbol(C.uplo)}), A_ldiv_B!(Triangular(C.UL, Token{symbol(C.uplo)}), B[C.piv]))[invperm(C.piv)] : A_ldiv_B!(Triangular(C.UL, Token{symbol(C.uplo)}), Ac_ldiv_B!(Triangular(C.UL, Token{symbol(C.uplo)}), B[C.piv]))[invperm(C.piv)]
A_ldiv_B!(C::CholeskyPivoted, B::StridedMatrix) = C.uplo=='L' ? Ac_ldiv_B!(Triangular(C.UL, Token{symbol(C.uplo)}), A_ldiv_B!(Triangular(C.UL, Token{symbol(C.uplo)}), B[C.piv,:]))[invperm(C.piv),:] : A_ldiv_B!(Triangular(C.UL, Token{symbol(C.uplo)}), Ac_ldiv_B!(Triangular(C.UL, Token{symbol(C.uplo)}), B[C.piv,:]))[invperm(C.piv),:]

function det{T,S,UpLo}(C::Cholesky{T,S,UpLo})
dd = one(T)
Expand Down
16 changes: 8 additions & 8 deletions base/linalg/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,15 @@ function sqrtm{T<:Real}(A::StridedMatrix{T})
issym(A) && return sqrtm(Symmetric(A))
n = chksquare(A)
SchurF = schurfact(complex(A))
R = full(sqrtm(Triangular(SchurF[:T], :U, false)))
R = full(sqrtm(Triangular(SchurF[:T], Token{:U})))
retmat = SchurF[:vectors]*R*SchurF[:vectors]'
all(imag(retmat) .== 0) ? real(retmat) : retmat
end
function sqrtm{T<:Complex}(A::StridedMatrix{T})
ishermitian(A) && return sqrtm(Hermitian(A))
n = chksquare(A)
SchurF = schurfact(A)
R = full(sqrtm(Triangular(SchurF[:T], :U, false)))
R = full(sqrtm(Triangular(SchurF[:T], Token{:U})))
SchurF[:vectors]*R*SchurF[:vectors]'
end
sqrtm(a::Number) = (b = sqrt(complex(a)); imag(b) == 0 ? real(b) : b)
Expand All @@ -321,9 +321,9 @@ function inv{S}(A::StridedMatrix{S})
T = typeof(one(S)/one(S))
Ac = convert(AbstractMatrix{T}, A)
if istriu(Ac)
Ai = inv(Triangular(A, :U, false))
Ai = inv(Triangular(A, Token{:U}))
elseif istril(Ac)
Ai = inv(Triangular(A, :L, false))
Ai = inv(Triangular(A, Token{:L}))
else
Ai = inv(lufact(Ac))
end
Expand Down Expand Up @@ -373,7 +373,7 @@ function factorize{T}(A::Matrix{T})
if utri1
return Bidiagonal(diag(A), diag(A, -1), false)
end
return Triangular(A, :L)
return Triangular(A, Token{:L})
end
if utri
return Bidiagonal(diag(A), diag(A, 1), true)
Expand All @@ -388,7 +388,7 @@ function factorize{T}(A::Matrix{T})
end
end
if utri
return Triangular(A, :U)
return Triangular(A, Token{:U})
end
if herm
try
Expand All @@ -409,9 +409,9 @@ function (\)(A::StridedMatrix, B::StridedVecOrMat)
m, n = size(A)
if m == n
if istril(A)
return istriu(A) ? \(Diagonal(A),B) : \(Triangular(A, :L),B)
return istriu(A) ? \(Diagonal(A),B) : \(Triangular(A, Token{:L}),B)
end
istriu(A) && return \(Triangular(A, :U),B)
istriu(A) && return \(Triangular(A, Token{:U}),B)
return \(lufact(A),B)
end
return qrfact(A,pivot=eltype(A)<:BlasFloat)\B
Expand Down
2 changes: 1 addition & 1 deletion base/linalg/factorization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ function A_ldiv_B!{T<:BlasFloat}(A::Union(QRCompactWY{T},QRPivoted{T}), B::Strid
# if cond(r[1:rnk, 1:rnk])*rcond < 1 break end
end
C, τ = LAPACK.tzrzf!(A.factors[1:rnk,:])
A_ldiv_B!(Triangular(C[1:rnk,1:rnk],:U),sub(Ac_mul_B!(getq(A),sub(B, 1:mA, 1:nrhs)),1:rnk,1:nrhs))
A_ldiv_B!(Triangular(C[1:rnk,1:rnk], Token{:U}), sub(Ac_mul_B!(getq(A), sub(B, 1:mA, 1:nrhs)), 1:rnk, 1:nrhs))
B[rnk+1:end,:] = zero(T)
LAPACK.ormrz!('L', iseltype(B, Complex) ? 'C' : 'T', C, τ, sub(B,1:nA,1:nrhs))
return isa(A,QRPivoted) ? B[invperm(A[:p]::Vector{BlasInt}),:] : B[1:nA,:], rnk
Expand Down
2 changes: 1 addition & 1 deletion base/linalg/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ function elementaryRightTrapezoid!(A::AbstractMatrix, row::Integer)
end

function det(A::AbstractMatrix)
(istriu(A) || istril(A)) && return det(Triangular(A, :U, false))
(istriu(A) || istril(A)) && return det(Triangular(A, Token{:U}))
return det(lufact(A))
end
det(x::Number) = x
Expand Down
4 changes: 2 additions & 2 deletions base/linalg/lu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ function getindex{T,S<:StridedMatrix}(A::LU{T,S}, d::Symbol)
end

A_ldiv_B!{T<:BlasFloat, S<:StridedMatrix}(A::LU{T, S}, B::StridedVecOrMat{T}) = @assertnonsingular LAPACK.getrs!('N', A.factors, A.ipiv, B) A.info
A_ldiv_B!{T,S<:StridedMatrix}(A::LU{T,S}, b::StridedVector) = A_ldiv_B!(Triangular(A.factors, :U, false), A_ldiv_B!(Triangular(A.factors, :L, true), b[ipiv2perm(A.ipiv, length(b))]))
A_ldiv_B!{T,S<:StridedMatrix}(A::LU{T,S}, B::StridedMatrix) = A_ldiv_B!(Triangular(A.factors, :U, false), A_ldiv_B!(Triangular(A.factors, :L, true), B[ipiv2perm(A.ipiv, size(B, 1)),:]))
A_ldiv_B!{T,S<:StridedMatrix}(A::LU{T,S}, b::StridedVector) = A_ldiv_B!(Triangular(A.factors, Token{:U}), A_ldiv_B!(Triangular(A.factors, Token{:L}, Token{true}), b[ipiv2perm(A.ipiv, length(b))]))
A_ldiv_B!{T,S<:StridedMatrix}(A::LU{T,S}, B::StridedMatrix) = A_ldiv_B!(Triangular(A.factors, Token{:U}), A_ldiv_B!(Triangular(A.factors, Token{:L}, Token{true}), B[ipiv2perm(A.ipiv, size(B, 1)),:]))
At_ldiv_B{T<:BlasFloat,S<:StridedMatrix}(A::LU{T,S}, B::StridedVecOrMat{T}) = @assertnonsingular LAPACK.getrs!('T', A.factors, A.ipiv, copy(B)) A.info
Ac_ldiv_B{T<:BlasComplex,S<:StridedMatrix}(A::LU{T,S}, B::StridedVecOrMat{T}) = @assertnonsingular LAPACK.getrs!('C', A.factors, A.ipiv, copy(B)) A.info
At_ldiv_Bt{T<:BlasFloat,S<:StridedMatrix}(A::LU{T,S}, B::StridedVecOrMat{T}) = @assertnonsingular LAPACK.getrs!('T', A.factors, A.ipiv, transpose(B)) A.info
Expand Down
4 changes: 2 additions & 2 deletions base/linalg/special.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
convert{T}(::Type{Bidiagonal}, A::Diagonal{T})=Bidiagonal(A.diag, zeros(T, size(A.diag,1)-1), true)
convert{T}(::Type{SymTridiagonal}, A::Diagonal{T})=SymTridiagonal(A.diag, zeros(T, size(A.diag,1)-1))
convert{T}(::Type{Tridiagonal}, A::Diagonal{T})=Tridiagonal(zeros(T, size(A.diag,1)-1), A.diag, zeros(T, size(A.diag,1)-1))
convert(::Type{Triangular}, A::Diagonal) = Triangular(full(A), :L)
convert(::Type{Triangular}, A::Bidiagonal) = Triangular(full(A), A.isupper ? :U : :L)
convert(::Type{Triangular}, A::Diagonal) = Triangular(full(A), Token{:L})
convert(::Type{Triangular}, A::Bidiagonal) = Triangular(full(A), Token{A.isupper ? :U : :L})
convert(::Type{Matrix}, D::Diagonal) = diagm(D.diag)

function convert(::Type{Diagonal}, A::Union(Bidiagonal, SymTridiagonal))
Expand Down
Loading