Skip to content
This repository
tree: 826c42f4ec
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 367 lines (325 sloc) 13.912 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
## solvers and inverses using factorizations
for (getrs, potrs, getri, potri, trtri, elty) in
    (("dgetrs_","dpotrs_","dgetri_","dpotri_","dtrtri_",:Float64),
     ("sgetrs_","spotrs_","sgetri_","spotri_","strtri_",:Float32),
     ("zgetrs_","zpotrs_","zgetri_","zpotri_","ztrtri_",:Complex128),
     ("cgetrs_","cpotrs_","cgetri_","cpotri_","ctrtri_",:Complex64))
    
    @eval begin
        # SUBROUTINE DGETRS( TRANS, N, NRHS, A, LDA, IPIV, B, LDB, INFO )
        #* .. Scalar Arguments ..
        # CHARACTER TRANS
        # INTEGER INFO, LDA, LDB, N, NRHS
        # .. Array Arguments ..
        # INTEGER IPIV( * )
        # DOUBLE PRECISION A( LDA, * ), B( LDB, * )
        function _jl_lapack_getrs(trans::LapackChar, A::StridedMatrix{$elty}, ipiv::Vector{Int32}, B::StridedVecOrMat{$elty})
            if stride(A,1) != 1 || stride(B,1) != 1
                error("_jl_lapack_getrs: matrix columns must have contiguous elements")
            end
            m, n = size(A)
            if m != n || size(B, 1) != m error("_jl_lapack_getrs: dimension mismatch") end
            nrhs = size(B, 2)
            lda = stride(A, 2)
            ldb = isa(B, Vector) ? m : stride(B, 2)
            info = Array(Int32, 1)
            ccall(dlsym(_jl_liblapack, $getrs),
                  Void,
                  (Ptr{Uint8}, Ptr{Int32}, Ptr{Int32}, Ptr{$elty}, Ptr{Int32},
                   Ptr{Int32}, Ptr{$elty}, Ptr{Int32}, Ptr{Int32}),
                  &trans, &n, &nrhs, A, &lda, ipiv, B, &ldb, info)
            if info[1] != 0 error("_jl_lapack_getrs: error $(info[1])") end
            B
        end
        # SUBROUTINE DPOTRS( UPLO, N, NRHS, A, LDA, B, LDB, INFO )
        #* .. Scalar Arguments ..
        # CHARACTER UPLO
        # INTEGER INFO, LDA, LDB, N, NRHS
        # .. Array Arguments ..
        # DOUBLE PRECISION A( LDA, * ), B( LDB, * )
        function _jl_lapack_potrs(uplo::LapackChar, A::StridedMatrix{$elty}, B::StridedVecOrMat{$elty})
            if stride(A,1) != 1 || stride(B,1) != 1
                error("_jl_lapack_potrs: matrix columns must have contiguous elements")
            end
            m, n = size(A)
            if m != n || size(B,1) != m error("_jl_lapack_potrs: dimension mismatch") end
            nrhs = size(B, 2)
            lda = stride(A, 2)
            ldb = isa(B, Vector) ? m : stride(B, 2)
            info = Array(Int32, 1)
            ccall(dlsym(_jl_liblapack, $potrs),
                  Void,
                  (Ptr{Uint8}, Ptr{Int32}, Ptr{Int32}, Ptr{$elty}, Ptr{Int32},
                   Ptr{$elty}, Ptr{Int32}, Ptr{Int32}),
                  &uplo, &n, &nrhs, A, &lda, B, &ldb, info)
            if info[1] != 0 error("_jl_lapack_potrs: error $(info[1])") end
            B
        end
        # SUBROUTINE DGETRI( N, A, LDA, IPIV, WORK, LWORK, INFO )
        #* .. Scalar Arguments ..
        # INTEGER INFO, LDA, LWORK, N
        #* .. Array Arguments ..
        # INTEGER IPIV( * )
        # DOUBLE PRECISION A( LDA, * ), WORK( * )
        function _jl_lapack_getri(A::StridedMatrix{$elty}, ipiv::Vector{Int32})
            if stride(A,1) != 1
                error("_jl_lapack_getri: matrix columns must have contiguous elements");
            end
            m, n = size(A)
            if m != n || n != numel(ipiv) error("_jl_lapack_getri: dimension mismatch") end
            lda = stride(A, 2)
            info = Array(Int32, 1)
            lwork = -1
            work = Array($elty, 1)
            for i in 1:2
                ccall(dlsym(_jl_liblapack, $getri),
                      Void,
                      (Ptr{Int32}, Ptr{$elty}, Ptr{Int32}, Ptr{Int32},
                       Ptr{$elty}, Ptr{Int32}, Ptr{Int32}),
                      &n, A, &lda, ipiv, work, &lwork, info)
                if info[1] != 0 error("_jl_lapack_getri: error $(info[1])") end
                if lwork < 0
                    lwork = int32(real(work[1]))
                    work = Array($elty, lwork)
                end
            end
            A
        end
        # SUBROUTINE DTRTRI( UPLO, DIAG, N, A, LDA, INFO )
        #* .. Scalar Arguments ..
        # CHARACTER DIAG, UPLO
        # INTEGER INFO, LDA, N
        # .. Array Arguments ..
        # DOUBLE PRECISION A( LDA, * )
        function _jl_lapack_trtri(uplo::LapackChar, diag::LapackChar, A::StridedMatrix{$elty})
            if stride(A,1) != 1
                error("_jl_lapack_trtri: matrix columns must have contiguous elements");
            end
            m, n = size(A)
            if m != n error("_jl_lapack_trtri: dimension mismatch") end
            lda = stride(A, 2)
            info = Array(Int32, 1)
            ccall(dlsym(_jl_liblapack, $trtri),
                  Void,
                  (Ptr{Uint8}, Ptr{Uint8}, Ptr{Int32}, Ptr{$elty}, Ptr{Int32},
                   Ptr{Int32}),
                  &uplo, &diag, &n, A, &lda, info)
            if info[1] != 0 error("_jl_lapack_trtri: error $(info[1])") end
            A
        end
    end
end

abstract Factorization{T}

type CholeskyDense{T} <: Factorization{T}
    LR::Matrix{T}
    uplo::LapackChar
end

# chol() does not check that input matrix is symmetric/hermitian
# It simply uses upper triangular half
function chol{T<:LapackScalar}(A::Matrix{T}, ul::LapackChar)
    if ul != 'U' && ul != 'L'; error("Cholesky: uplo must be 'U' or 'L'"); end
    C = CholeskyDense{T}(Array(T, size(A)), ul)
    chol(C, A)
end
chol{T<:Real}(A::Matrix{T}, ul::LapackChar) = chol(float64(A), ul)
chol(A) = chol(A, 'U')

function _chol{T<:LapackScalar}(C::CholeskyDense{T})
    if _jl_lapack_potrf(C.uplo, C.LR) != 0
        error("Cholesky: Matrix is not positive-definite")
    end
    if C.uplo == 'U'
        triu!(C.LR)
    else
        tril!(C.LR)
    end
    C
end
function chol{T}(C::CholeskyDense{T}, A::Matrix{T})
    copy_to(C.LR, A)
    _chol(C)
end
function chol!{T<:LapackScalar}(A::Matrix{T}, ul::LapackChar)
    C = CholeskyDense{T}(A, ul)
    _chol(C)
end
    
factors(C::CholeskyDense) = C.LR


(\){T<:LapackScalar}(C::CholeskyDense{T}, B::StridedVecOrMat{T}) =
    _jl_lapack_potrs(C.uplo, C.LR, copy(B))

inv{T<:LapackScalar}(C::CholeskyDense{T}) = _jl_lapack_potri(C.uplo, copy(C.LR)) # should symmetrize the result
 
type LUDense{T} <: Factorization{T}
    lu::Matrix{T}
    ipiv::Vector{Int32}
    function LUDense(lu::Matrix{T}, ipiv::Vector{Int32})
        m, n = size(lu)
        m == numel(ipiv) ? new(lu, ipiv) : error("LUDense: dimension mismatch")
    end
end
size(A::LUDense) = size(A.lu)
size(A::LUDense,n) = size(A.lu,n)

lu{T<:LapackScalar}(A::Matrix{T}) = lu!(copy(A))
function lu!{T<:LapackScalar}(A::Matrix{T})
    lu, ipiv = _jl_lapack_getrf(A)
    LUDense{T}(lu, ipiv)
end

lu{T<:Real}(A::Matrix{T}) = lu(float64(A))

function det(lu::LUDense)
    m, n = size(lu.lu)
    if m != n error("det only defined for square matrices") end
    prod(diag(lu.lu)) * (bool(sum(lu.ipiv .!= 1:n) % 2) ? -1 : 1)
end

det(A::Matrix) = det(lu(A))

(\){T<:LapackScalar}(lu::LUDense{T}, B::StridedVecOrMat{T}) =
    _jl_lapack_getrs('N', lu.lu, lu.ipiv, copy(B))

inv{T<:LapackScalar}(lu::LUDense{T}) = _jl_lapack_getri(copy(lu.lu), lu.ipiv)

function factors{T<:LapackScalar}(lu::LUDense{T})
    LU, ipiv = lu.lu, lu.ipiv
    m, n = size(LU)

    L = m >= n ? tril(LU, -1) + eye(m,n) : tril(LU, -1)[:, 1:m] + eye(m,m)
    U = m <= n ? triu(LU) : triu(LU)[1:n, :]
    P = [1:m]
    for i=1:min(m,n)
        t = P[i]
        P[i] = P[ipiv[i]]
        P[ipiv[i]] = t
    end
    L, U, P
end

## Multiplication by Q or Q' from a QR factorization
for (orm2r, elty) in
    (("dorm2r_",:Float64),
     ("sorm2r_",:Float32),
     ("zunm2r_",:Complex128),
     ("cunm2r_",:Complex64))
    @eval begin
        # SUBROUTINE DORM2R( SIDE, TRANS, M, N, K, A, LDA, TAU, C, LDC,
        # WORK, INFO )
        # .. Scalar Arguments ..
        # CHARACTER SIDE, TRANS
        # INTEGER INFO, K, LDA, LDC, M, N
        # .. Array Arguments ..
        # DOUBLE PRECISION A( LDA, * ), C( LDC, * ), TAU( * ), WORK( * )
        function _jl_lapack_orm2r(side::LapackChar, trans::LapackChar, A::StridedMatrix{$elty}, k::Integer, tau::Vector{$elty}, C::StridedVecOrMat{$elty})
            if stride(A,1) != 1 || stride(C,1) != 1
                error("_jl_lapack_orm2r: matrix columns must have contiguous elements");
            end
            m = size(C, 1)
            n = size(C, 2)
            if size(A, 1) != m error("_jl_lapack_orm2r: dimension mismatch") end
            lda = stride(A, 2)
            ldc = isa(C, Vector) ? m : stride(C, 2)
            work = Array($elty, side == 'L' ? n : m)
            info = Array(Int32, 1)
            ccall(dlsym(_jl_liblapack, $orm2r),
                  Void,
                  (Ptr{Uint8}, Ptr{Uint8}, Ptr{Int32}, Ptr{Int32}, Ptr{Int32}, Ptr{$elty},
                   Ptr{Int32}, Ptr{$elty}, Ptr{$elty}, Ptr{Int32}, Ptr{$elty}, Ptr{Int32}),
                  &side, &trans, &m, &n, &k, A, &lda, tau, C, &ldc, work, info)
            if info[1] != 0 error("_jl_lapack_orm2r: error $(info[1])") end
            C
        end
    end
end

abstract QRFactorization{T} <: Factorization{T}

## QR decomposition without column pivots
type QRDense{T} <: QRFactorization{T}
    hh::Matrix{T} # Householder transformations and R
    tau::Vector{T} # Scalar factors of transformations
    function QRDense(hh::Matrix{T}, tt::Vector{T})
        numel(tt) == min(size(hh)) ? new(hh, tt) : error("QR: mismatched dimensions")
    end
end
size(A::QRFactorization) = size(A.hh)
size(A::QRFactorization,n) = size(A.hh,n)

function qr{T<:LapackScalar}(A::StridedMatrix{T})
    hh, tt = _jl_lapack_geqrf(copy(A))
    QRDense{eltype(A)}(hh, tt)
end

qr{T<:Real}(x::StridedMatrix{T}) = qr(float64(x))

function factors{T<:LapackScalar}(qrd::QRDense{T})
    aa, tau = qrd.hh, qrd.tau
    R = triu(aa[1:min(size(qrd)),:])
    _jl_lapack_orgqr(aa, tau), R
end

## Multiplication by Q from the QR decomposition
(*){T<:LapackScalar}(A::QRFactorization{T}, B::StridedVecOrMat{T}) =
    _jl_lapack_orm2r('L', 'N', A.hh, size(A, 2), A.tau, copy(B))

## Multiplication by Q' from the QR decomposition
Ac_mul_B{T<:LapackScalar}(A::QRFactorization{T}, B::StridedVecOrMat{T}) =
    _jl_lapack_orm2r('L', iscomplex(A.tau) ? 'C' : 'T', A.hh, size(A, 2), A.tau, copy(B))

## Least squares solution. Should be more careful about cases with m < n
function (\){T<:LapackScalar}(A::QRDense{T}, B::StridedVecOrMat{T})
    n = numel(A.tau)
    qtb = isa(B, Vector) ? (A' * B)[1:n] : (A' * B)[1:n, :]
    ## Not sure if this avoids copying A.hh[1:n,:] but at least it is not all of A.hh
    _jl_lapack_trtrs('U','N','N', A.hh[1:n,:], qtb)
end

type QRPDense{T} <: QRFactorization{T}
    hh::Matrix{T}
    tau::Vector{T}
    jpvt::Vector{Int32}
    function QRPDense(hh::Matrix{T}, tt::Vector{T}, jj::Vector{Int32})
        m, n = size(hh)
        numel(tt) == min(m,n) && numel(jj) == n ? new(hh,tt,jj) : error("QRPDense: mismatched dimensions")
    end
end

function qrp{T<:LapackScalar}(A::StridedMatrix{T})
    aa, tau, jpvt = _jl_lapack_geqp3(copy(A))
    QRPDense{T}(aa, tau, jpvt)
end
qrp{T<:Real}(x::StridedMatrix{T}) = qrp(float64(x))

function factors{T<:LapackScalar}(qrpd::QRPDense{T})
    aa, tau = qrpd.hh, qrpd.tau
    R = triu(aa[1:min(size(qrpd)),:])
    _jl_lapack_orgqr(aa, tau), R, qrpd.jpvt
end

# FIXME \
function (\){T<:Union(Float64,Float32,Complex128,Complex64)}(A::QRPDense{T},
                                                             B::StridedVecOrMat{T})
    n = numel(A.tau)
    ## Replace this with a direct call to _jl_lapack_trtrs to save copying A.hh?
    ## Actually would need to call the appropriate Lapack subroutine to save copying.
    triu(A.hh[1:n,:]) \ (A' * B)[1:n]
end

function (\){T<:Union(Float64,Float32,Complex128,Complex64)}(A::QRPDense{T},
                                                             B::StridedVecOrMat{T})
    ## may be better to define one method for B::Vector{T} and another for StridedMatrix
    BV = isa(B, Vector)
    n = numel(A.tau)
    qtb = BV ? (A' * B)[1:n] : (A' * B)[1:n, :]
    ans = _jl_lapack_trtrs('U', 'N', 'N', A.hh[1:n,:], qtb)
    BV ? ans[invperm(A.jpvt)] : ans[invperm(A.jpvt), :]
end

##ToDo: Add methods for rank(A::QRP{T}) and adjust the (\) method accordingly
## Add rcond methods for Cholesky, LU, QR and QRP types
## Lower priority: Add LQ, QL and RQ factorizations


#### Factorizations for Tridiagonal ####
type LDLTTridiagonal{T} <: Factorization{T}
    D::Vector{T}
    E::Vector{T}
end
function ldlt{T<:LapackScalar}(A::Tridiagonal{T})
    D = copy(A.d)
    E = copy(A.dl)
    _jl_lapack_pttrf(D, E)
    LDLTTridiagonal{T}(D, E)
end

(\){T<:LapackScalar}(C::LDLTTridiagonal{T}, B::StridedVecOrMat{T}) =
    _jl_lapack_pttrs(C.D, C.E, copy(B))

type LUTridiagonal{T} <: Factorization{T}
    lu::Tridiagonal{T}
    ipiv::Vector{Int32}
    function LUTridiagonal(lu::Tridiagonal{T}, ipiv::Vector{Int32})
        m, n = size(lu)
        m == numel(ipiv) ? new(lu, ipiv) : error("LUTridiagonal: dimension mismatch")
    end
end
show(io, lu::LUTridiagonal) = print(io, "LU decomposition of ", summary(lu.lu))

function lu{T<:LapackScalar}(A::Tridiagonal{T})
    lu, ipiv = _jl_lapack_gttrf(copy(A))
    LUTridiagonal{T}(lu, ipiv)
end

function det(lu::LUTridiagonal)
    prod(lu.lu.d) * (bool(sum(lu.ipiv .!= 1:n) % 2) ? -1 : 1)
end

det(A::Tridiagonal) = det(lu(A))

(\){T<:LapackScalar}(lu::LUTridiagonal{T}, B::StridedVecOrMat{T}) =
    _jl_lapack_gttrs('N', lu.lu, lu.ipiv, copy(B))
Something went wrong with that request. Please try again.