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

add kron #100

Merged
merged 2 commits into from
Jul 27, 2019
Merged

add kron #100

merged 2 commits into from
Jul 27, 2019

Conversation

johnczito
Copy link
Member

@johnczito johnczito commented Jul 26, 2019

The kronecker product of positive definite matrices is positive definite, and the cholesky factor of a kronecker product is the kronecker product of the cholesky factors. So this PR adds

Base.kron(A::PDMat, B::PDMat) = PDMat(kron(A.mat, B.mat), Cholesky(kron(A.chol.U, B.chol.U), 'U', A.chol.info))

This functionality would be used to improve two spots in Distributions.jl where a covariance matrix is computed as the kronecker product of two PDMat instances, but we currently ignore the PDMat nature of the factors: here and here.

Turns out that directly supplying both mat and chol is faster than just supplying chol and letting mat be computed from it. MWE:

using LinearAlgebra, PDMats

X = randn(7, 7)
Y = randn(11, 11)
PDA = PDMat(X * X')
PDB = PDMat(Y * Y')

kron1(A::PDMat, B::PDMat) = PDMat(Cholesky(kron(A.chol.U, B.chol.U), 'U', A.chol.info))
kron2(A::PDMat, B::PDMat) = PDMat(kron(A.mat, B.mat), Cholesky(kron(A.chol.U, B.chol.U), 'U', A.chol.info))

julia> @time PDMat(kron(PDA.mat, PDB.mat))
  0.000183 seconds (13 allocations: 93.203 KiB)

julia> @time kron1(PDA, PDB)
  0.000176 seconds (15 allocations: 139.625 KiB)

julia> @time kron2(PDA, PDB)
  0.000108 seconds (12 allocations: 93.156 KiB)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants