Skip to content

Commit 213c37a

Browse files
authored
Merge pull request #134 from sethaxen/cholnocopy
Fix #133
2 parents 84bdd88 + d81e3ce commit 213c37a

File tree

4 files changed

+15
-15
lines changed

4 files changed

+15
-15
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "PDMats"
22
uuid = "90014a1f-27ba-587c-ab20-58faa44d9150"
3-
version = "0.11.0"
3+
version = "0.11.1"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/chol.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
CholType{T,S<:AbstractMatrix} = Cholesky{T,S}
22
chol_lower(a::Matrix) = cholesky(a).L
33

4+
# always use the stored cholesky factor, not a copy
5+
chol_lower(a::CholType) = a.uplo === 'L' ? a.L : a.U'
6+
chol_upper(a::CholType) = a.uplo === 'U' ? a.U : a.L'
7+
48
if HAVE_CHOLMOD
59
CholTypeSparse{T} = SuiteSparse.CHOLMOD.Factor{T}
610

src/pdmat.jl

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,20 @@ Base.kron(A::PDMat, B::PDMat) = PDMat(kron(A.mat, B.mat), Cholesky(kron(A.chol.U
6161
### whiten and unwhiten
6262

6363
function whiten!(r::StridedVecOrMat, a::PDMat, x::StridedVecOrMat)
64-
cf = a.chol.U
6564
v = _rcopy!(r, x)
66-
ldiv!(transpose(cf), v)
65+
ldiv!(chol_lower(a.chol), v)
6766
end
6867

6968
function unwhiten!(r::StridedVecOrMat, a::PDMat, x::StridedVecOrMat)
70-
cf = a.chol.U
7169
v = _rcopy!(r, x)
72-
lmul!(transpose(cf), v)
70+
lmul!(chol_lower(a.chol), v)
7371
end
7472

7573

7674
### quadratic forms
7775

78-
quad(a::PDMat, x::AbstractVector) = sum(abs2, a.chol.U * x)
79-
invquad(a::PDMat, x::AbstractVector) = sum(abs2, a.chol.L \ x)
76+
quad(a::PDMat, x::AbstractVector) = sum(abs2, chol_upper(a.chol) * x)
77+
invquad(a::PDMat, x::AbstractVector) = sum(abs2, chol_lower(a.chol) \ x)
8078

8179
"""
8280
quad!(r::AbstractArray, a::AbstractPDMat, x::StridedMatrix)
@@ -96,25 +94,21 @@ invquad!(r::AbstractArray, a::PDMat, x::StridedMatrix) = colwise_dot!(r, x, a.ma
9694
### tri products
9795

9896
function X_A_Xt(a::PDMat, x::StridedMatrix)
99-
cf = a.chol.U
100-
z = rmul!(copy(x), transpose(cf))
97+
z = rmul!(copy(x), chol_lower(a.chol))
10198
return z * transpose(z)
10299
end
103100

104101
function Xt_A_X(a::PDMat, x::StridedMatrix)
105-
cf = a.chol.U
106-
z = lmul!(cf, copy(x))
102+
z = lmul!(chol_upper(a.chol), copy(x))
107103
return transpose(z) * z
108104
end
109105

110106
function X_invA_Xt(a::PDMat, x::StridedMatrix)
111-
cf = a.chol.U
112-
z = rdiv!(copy(x), cf)
107+
z = rdiv!(copy(x), chol_upper(a.chol))
113108
return z * transpose(z)
114109
end
115110

116111
function Xt_invA_X(a::PDMat, x::StridedMatrix)
117-
cf = a.chol.U
118-
z = ldiv!(transpose(cf), copy(x))
112+
z = ldiv!(chol_lower(a.chol), copy(x))
119113
return transpose(z) * z
120114
end

test/pdmtypes.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ for T in [Float64, Float32]
1919
X = convert(T,2.0)
2020

2121
test_pdmat(PDMat(M), M, cmat_eq=true, verbose=1) #tests of PDMat
22+
cholL = Cholesky(Matrix(transpose(cholesky(M).factors)), 'L', 0)
23+
test_pdmat(PDMat(cholL), M, cmat_eq=true, verbose=1) #tests of PDMat
2224
test_pdmat(PDiagMat(V), Matrix(Diagonal(V)), cmat_eq=true, verbose=1) #tests of PDiagMat
2325
test_pdmat(ScalMat(3,x), x*Matrix{T}(I, 3, 3), cmat_eq=true, verbose=1) #tests of ScalMat
2426
test_pdmat(PDSparseMat(sparse(M)), M, cmat_eq=true, verbose=1, t_eig=false)

0 commit comments

Comments
 (0)