QrAlgo.jl 2.12 KB
Newer Older
1
module QrAlgo
Michel Juillard's avatar
Michel Juillard committed
2

3
4
5
6
import LinearAlgebra.BlasInt
import LinearAlgebra.BLAS.@blasfunc
import LinearAlgebra.BLAS.libblas
import LinearAlgebra.LAPACK: liblapack, chklapackerror
Michel Juillard's avatar
Michel Juillard committed
7
8
9

export QrWS, dgeqrf_core!, dormrqf_core!

10
mutable struct QrWS
Michel Juillard's avatar
Michel Juillard committed
11
12
    tau::Vector{Float64}
    work::Vector{Float64}
Michel Juillard's avatar
Michel Juillard committed
13
14
15
16
17
18
19
20
    lwork::Ref{BlasInt}
    info::Ref{BlasInt}

    function QrWS(A::StridedMatrix{Float64})
        nn,mm = size(A)
        m = Ref{BlasInt}(mm)
        n = Ref{BlasInt}(nn)
        RldA = Ref{BlasInt}(max(1,stride(A,2)))
Michel Juillard's avatar
Michel Juillard committed
21
22
        tau = Vector{Float64}(min(nn,mm))
        work = Vector{Float64}(1)
Michel Juillard's avatar
Michel Juillard committed
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
        lwork = Ref{BlasInt}(-1)
        info = Ref{BlasInt}(0)
        ccall((@blasfunc(dgeqrf_), liblapack), Void,
              (Ref{BlasInt},Ref{BlasInt},Ptr{Float64},Ref{BlasInt},
               Ptr{Float64},Ptr{Float64},Ref{BlasInt},Ref{BlasInt}),
              m,n,A,RldA,tau,work,lwork,info)
        chklapackerror(info[])
        lwork = Ref{BlasInt}(real(work[1]))
        work = Array{Float64}(lwork[])
        new(tau,work,lwork,info)
    end
end

function dgeqrf_core!(ws::QrWS,A::StridedMatrix{Float64})
    mm,nn = size(A)
    m = Ref{BlasInt}(mm)
    n = Ref{BlasInt}(nn)
    RldA = Ref{BlasInt}(max(1,stride(A,2)))
    ccall((@blasfunc(dgeqrf_), liblapack), Void,
          (Ref{BlasInt},Ref{BlasInt},Ptr{Float64},Ref{BlasInt},
           Ptr{Float64},Ptr{Float64},Ref{BlasInt},Ref{BlasInt}),
          m,n,A,RldA,ws.tau,ws.work,ws.lwork,ws.info)
    chklapackerror(ws.info[])
end

function dormrqf_core!(ws::QrWS,side::Ref{UInt8},trans::Ref{UInt8},A::StridedMatrix{Float64},
                      C::StridedMatrix{Float64})
    mm,nn = size(C)
    m = Ref{BlasInt}(mm)
    n = Ref{BlasInt}(nn)
    k = Ref{BlasInt}(length(ws.tau))
    RldA = Ref{BlasInt}(max(1,stride(A,2)))
    RldC = Ref{BlasInt}(max(1,stride(C,2)))
    ccall((@blasfunc(dormqr_), liblapack), Void,
          (Ref{UInt8},Ref{UInt8},Ref{BlasInt},Ref{BlasInt},Ref{BlasInt},Ptr{Float64},Ref{BlasInt},
           Ptr{Float64},Ptr{Float64},Ref{BlasInt},Ptr{Float64},Ref{BlasInt},Ref{BlasInt}),
          side,trans,m,n,k,A,RldA,ws.tau,C,RldC,ws.work,ws.lwork,ws.info)
    chklapackerror(ws.info[])
end

63
end