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

RFC: Fix type instability for \ #9196

Closed
wants to merge 2 commits 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
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export
Markdown,

# Types
€,
AbstractMatrix,
AbstractSparseArray,
AbstractSparseMatrix,
Expand Down
2 changes: 2 additions & 0 deletions base/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export
BLAS,

# Types
€,
SymTridiagonal,
Tridiagonal,
Bidiagonal,
Expand Down Expand Up @@ -193,6 +194,7 @@ 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")))

immutable €{T} end

include("linalg/exceptions.jl")
include("linalg/generic.jl")
Expand Down
94 changes: 47 additions & 47 deletions base/linalg/cholesky.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ 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
function chol!{T<:BlasFloat,UpLo}(A::StridedMatrix{T}, ::Type{€{UpLo}})
C, info = LAPACK.potrf!(char_uplo(UpLo), A)
return @assertposdef Triangular{eltype(C),typeof(C),UpLo,false}(C) info
end

function chol!{T}(A::AbstractMatrix{T}, uplo::Symbol=:U)
function chol!{T,UpLo}(A::AbstractMatrix{T}, ::Type{€{UpLo}})
n = chksquare(A)
@inbounds begin
if uplo == :L
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)
A[k,k] = chol!(A[k,k], €{UpLo})
AkkInv = inv(A[k,k]')
for j = 1:k
for i = k + 1:n
Expand All @@ -35,12 +35,12 @@ function chol!{T}(A::AbstractMatrix{T}, uplo::Symbol=:U)
end
end
end
elseif uplo == :U
elseif UpLo == :U
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], uplo)
A[k,k] = chol!(A[k,k], €{UpLo})
AkkInv = inv(A[k,k])
for j = k + 1:n
for i = 1:k - 1
Expand All @@ -50,49 +50,55 @@ function chol!{T}(A::AbstractMatrix{T}, uplo::Symbol=:U)
end
end
else
throw(ArgumentError("uplo must be either :U or :L but was $(uplo)"))
throw(ArgumentError("UpLo must be either :U or :L but was $(UpLo)"))
end
end
return Triangular(A, uplo, false)
return Triangular(A, €{UpLo}, €{false})
end
chol!(A::AbstractMatrix) = chol!(A, €{:U})

function cholfact!{T<:BlasFloat}(A::StridedMatrix{T}, uplo::Symbol=:U; pivot=false, tol=0.0)
uplochar = char_uplo(uplo)
function cholfact!{T<:BlasFloat,UpLo}(A::StridedMatrix{T}, ::Type{€{UpLo}}; 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)
return Cholesky{T,typeof(A),UpLo}(chol!(A, €{UpLo}).data)
end
cholfact!(A::AbstractMatrix, uplo::Symbol=:U) = Cholesky{eltype(A),typeof(A),uplo}(chol!(A, uplo).data)
cholfact!{UpLo}(A::AbstractMatrix, ::Type{€{UpLo}}) = Cholesky{eltype(A),typeof(A),UpLo}(chol!(A, €{UpLo}).data)
cholfact!(A::AbstractMatrix) = cholfact!(A, €{: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,UpLo}(A::StridedMatrix{T}, ::Type{€{UpLo}}; pivot=false, tol=0.0) = cholfact!(copy(A), €{UpLo}, pivot=pivot, tol=tol)
function cholfact{T,UpLo}(A::StridedMatrix{T}, ::Type{€{UpLo}}; pivot=false, tol=0.0)
S = promote_type(typeof(chol(one(T))),Float32)
S <: BlasFloat && return cholfact!(convert(AbstractMatrix{S}, A), uplo, pivot = pivot, tol = tol)
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 != T && return cholfact!(convert(AbstractMatrix{S}, A), €{UpLo})
return cholfact!(copy(A), €{UpLo})
end
function cholfact(x::Number, uplo::Symbol=:U)
function cholfact{UpLo}(x::Number, ::Type{€{UpLo}})
xf = fill(chol!(x), 1, 1)
Cholesky{:U, eltype(xf), typeof(xf)}(xf)
end
cholfact(A; pivot=false, tol=0.0) = cholfact(A, €{:U}; pivot = pivot, tol = tol)

chol(A::AbstractMatrix, uplo::Symbol=:U) = chol!(copy(A), uplo)
function chol!(x::Number, uplo::Symbol=:U)
chol{UpLo}(A::AbstractMatrix, ::Type{€{UpLo}}) = chol!(copy(A), €{UpLo})
chol(A::AbstractMatrix) = chol!(copy(A), €{:U})
function chol!(x::Number)
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, ::Type) = chol!(x)
chol(x::Number) = chol!(x)
chol(x::Number, ::Type) = chol!(x, €{:U})

function convert{Tnew,Told,S,UpLo}(::Type{Cholesky{Tnew}},C::Cholesky{Told,S,UpLo})
Cnew = convert(AbstractMatrix{Tnew}, C.UL)
Expand All @@ -106,39 +112,33 @@ convert{T}(::Type{Factorization{T}}, C::Cholesky) = convert(Cholesky{T}, C)
convert{T}(::Type{CholeskyPivoted{T}},C::CholeskyPivoted) = CholeskyPivoted(convert(AbstractMatrix{T},C.UL),C.uplo,C.piv,C.rank,C.tol,C.info)
convert{T}(::Type{Factorization{T}}, C::CholeskyPivoted) = convert(CholeskyPivoted{T}, C)

full{T,S}(C::Cholesky{T,S,:U}) = C[:U]'C[:U]
full{T,S}(C::Cholesky{T,S,:L}) = C[:L]*C[:L]'
full{T,S}(C::Cholesky{T,S,:U}) = C[€{:U}]'C[€{:U}]
full{T,S}(C::Cholesky{T,S,:L}) = C[€{:L}]*C[€{:L}]'

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)
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 == :p && return C.piv
if d == :P
n = size(C, 1)
P = zeros(T, n, n)
for i=1:n
P[C.piv[i],i] = one(T)
end
return P
getindex{T,S,UpLo}(C::Cholesky{T,S,UpLo}, ::Type{€{:U}}) = Triangular(UpLo == :U ? C.UL : C.UL', €{:U})
getindex{T,S,UpLo}(C::Cholesky{T,S,UpLo}, ::Type{€{:L}}) = Triangular(UpLo == :L ? C.UL : C.UL', €{:L})
getindex{T,S,UpLo}(C::Cholesky{T,S,UpLo}, ::Type{€{:UL}}) = Triangular(C.UL, €{UpLo})
getindex{T<:BlasFloat}(C::CholeskyPivoted{T}, ::Type{€{:U}}) = Triangular(C.uplo == 'U' ? C.UL : C.UL', €{:U})
getindex{T<:BlasFloat}(C::CholeskyPivoted{T}, ::Type{€{:L}}) = Triangular(C.uplo == 'L' ? C.UL : C.UL', €{:L})
getindex{T<:BlasFloat}(C::CholeskyPivoted{T}, ::Type{€{:p}}) = C.piv
function getindex{T<:BlasFloat}(C::CholeskyPivoted{T}, ::Type{€{:P}})
n = size(C, 1)
P = zeros(T, n, n)
for i = 1:n
P[C.piv[i],i] = one(T)
end
throw(KeyError(d))
return P
end

show{T,S<:AbstractMatrix,UpLo}(io::IO, C::Cholesky{T,S,UpLo}) = (println("$(typeof(C)) with factor:");show(io,C[UpLo]))

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, €{: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))

function A_ldiv_B!{T<:BlasFloat}(C::CholeskyPivoted{T}, B::StridedVector{T})
chkfullrank(C)
Expand All @@ -156,8 +156,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, €{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),:]

function det{T,S,UpLo}(C::Cholesky{T,S,UpLo})
dd = one(T)
Expand Down
34 changes: 17 additions & 17 deletions base/linalg/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -307,16 +307,16 @@ 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)))
retmat = SchurF[:vectors]*R*SchurF[:vectors]'
R = full(sqrtm(Triangular(SchurF[€{:T}], €{:U}, €{false})))
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)))
SchurF[:vectors]*R*SchurF[:vectors]'
R = full(sqrtm(Triangular(SchurF[€{:T}], €{:U}, €{false})))
SchurF[€{:vectors}]*R*SchurF[€{:vectors}]'
end
sqrtm(a::Number) = (b = sqrt(complex(a)); imag(b) == 0 ? real(b) : b)
sqrtm(a::Complex) = sqrt(a)
Expand All @@ -325,9 +325,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, €{:U}, €{false}))
elseif istril(Ac)
Ai = inv(Triangular(A, :L, false))
Ai = inv(Triangular(A, €{:L}, €{false}))
else
Ai = inv(lufact(Ac))
end
Expand Down Expand Up @@ -377,7 +377,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, €{:L})
end
if utri
return Bidiagonal(diag(A), diag(A, 1), true)
Expand All @@ -392,7 +392,7 @@ function factorize{T}(A::Matrix{T})
end
end
if utri
return Triangular(A, :U)
return Triangular(A, €{:U})
end
if herm
try
Expand All @@ -413,9 +413,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, €{:L}),B)
end
istriu(A) && return \(Triangular(A, :U),B)
istriu(A) && return \(Triangular(A, €{:U}),B)
return \(lufact(A),B)
end
return qrfact(A,pivot=eltype(A)<:BlasFloat)\B
Expand All @@ -440,12 +440,12 @@ function pinv{T}(A::StridedMatrix{T}, tol::Real)
end
end
SVD = svdfact(A, thin=true)
S = eltype(SVD[:S])
Sinv = zeros(S, length(SVD[:S]))
index = SVD[:S] .> tol*maximum(SVD[:S])
Sinv[index] = one(S) ./ SVD[:S][index]
S = eltype(SVD[€{:S}])
Sinv = zeros(S, length(SVD[€{:S}]))
index = SVD[€{:S}] .> tol*maximum(SVD[€{:S}])
Sinv[index] = one(S) ./ SVD[€{:S}][index]
Sinv[find(!isfinite(Sinv))] = zero(S)
return SVD[:Vt]'scale(Sinv, SVD[:U]')
return SVD[€{:Vt}]'scale(Sinv, SVD[€{:U}]')
end
function pinv{T}(A::StridedMatrix{T})
tol = eps(real(float(one(T))))*maximum(size(A))
Expand All @@ -459,8 +459,8 @@ function null{T}(A::StridedMatrix{T})
m, n = size(A)
(m == 0 || n == 0) && return eye(T, n)
SVD = svdfact(A, thin=false)
indstart = sum(SVD[:S] .> max(m,n)*maximum(SVD[:S])*eps(eltype(SVD[:S]))) + 1
return SVD[:V][:,indstart:end]
indstart = sum(SVD[€{:S}] .> max(m,n)*maximum(SVD[€{:S}])*eps(eltype(SVD[€{:S}]))) + 1
return SVD[€{:V}][:,indstart:end]
end
null(a::StridedVector) = null(reshape(a, length(a), 1))

Expand Down
Loading