-
Notifications
You must be signed in to change notification settings - Fork 3
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
Direct solving with Cholesky factors #63
Comments
You can do ldiv!(hchol.L,b) to solve the system and write the solution inside |
Hi @maltezfaria , Thanks very much for the hint; I was actually trying to play with ldiv!, but the solution seems wrong. Specifically, I first compute Do I need to permute the vectors into external/internal order somewhere? x1 = randn(m)
y = hchol.L*x1
ldiv!(hchol.L, y) # y is actually x2 now since idiv! is in-place
plot(x1, marker=:x)
plot!(y, marker=:o) |
I was also trying to do permutation (by stealing some code from using HMatrices, LinearAlgebra, StaticArrays, Random
using HMatrices: coltree, rowtree, loc2glob, permute!, invpermute!
using Plots
# copied from README.md
const Point3D = SVector{3,Float64}
m = 800
X = Y = [Point3D(sin(θ)cos(ϕ),sin(θ)*sin(ϕ),cos(θ)) for (θ,ϕ) in zip(π*rand(m),2π*rand(m))]
function G(x,y)
d = norm(x-y) + 1e-8
exp(-d)
end
K = KernelMatrix(G,X,Y)
# copied from test/cholesky_test.jl
splitter = CardinalitySplitter(; nmax = 50)
Xclt = ClusterTree(X, splitter)
Yclt = ClusterTree(Y, splitter)
adm = StrongAdmissibilityStd(10)
comp = PartialACA(; atol = 1e-6)
H = assemble_hmatrix(Hermitian(K), Xclt, Yclt; adm, comp, threads=false, distributed = false)
# Cholesky factorization
hchol = cholesky(H; atol = 1e-6)
# Experiment with ldiv! and (inv)permute!:
# 1) compute y=L*x1 and then 2) compute x2=L\y
# adapted from ldiv! of src/cholesky.jl
p = hchol.factors
ctree = coltree(p)
rtree = rowtree(p)
# 1. Compute y/y_copy=L*x with manual permutation
x1 = randn(m)
# permute x1 to internal order
permute!(x1, loc2glob(ctree))
# multiplication: L*x1
y = hchol.L*x1
# permute y to external order
invpermute!(y, loc2glob(ctree))
# 2. Compute x2= L\y with manual permutation
# permute y to internal order
permute!(y, loc2glob(ctree))
# division: L\y
ldiv!(hchol.L, y)
# permute y (actually x2 since it's in-place) back to external order
invpermute!(y, loc2glob(ctree))
plot(x1, marker=:x)
plot!(y, marker=:o) |
I just figured out the following code gives the same x = randn(m)
x1 = deepcopy(x)
y = hchol.L*x
# permute y to internal order
permute!(y, loc2glob(ctree))
ldiv!(hchol.L, y)
# permute y (actually x2 since it's in-place) back to external order
invpermute!(y, loc2glob(ctree))
plot(x1, marker=:x)
plot!(y, marker=:o) Does the code make sense to you? So |
You are right: one has to be careful with the internal vs. external ordering here! From a quick look at the code it does seem that |
Thanks for all the discussion! I'll have a bit more tests with BTW, is p = hchol.factors
ctree = coltree(p)
rtree = rowtree(p) and Xclt = ClusterTree(X, splitter) |
Hi Luiz, |
Thanks for testing and reporting the findings.
I think so. AFAIR the cluster trees do not get mutated in the construction.
I am still skeptical that this works as expected, so it is better to leave this issue open for now. I think using I think the proper "fix" here is to clearly distinguish internal methods which always use the local numbering induced by the clustering algorithms and external methods which handle the permutations. If we do that, then things like |
Hi Luiz, I wonder if it is possible to export/evalute the dense matrix behind the computed Cholesky factor. If that's possible, the verification of the correctness of Charlie |
This starts to tweak with the internals a bit, but assuming you stored the data on the upper triangular part for the Cholesky factorization (the default AFAIU) you can do: U = UpperTriangular(Matrix(hchol.factors))
L = adjoint(U) Here is what it does:
I will be busy for the next couple of weeks (summer conferences), but if you start implementing any of these changes, feel free to open a draft PR so that we can further discuss the implementation/issues. |
Hi @chaozg, I am going over some old tickets in this repo, and I was wondering if you ever managed to do what you wanted with |
Hi @maltezfaria , thanks for the follow-up message. So far I'm happy with the computed Cholesky factor though I didn't spend much time on further experiments you suggested yet. I'm still keen on using HMatrices.jl in a project later and I'll definitely ask you other questions or raise an issue some time! Feel free to close this issue if you need to.
|
Sounds good. At some point I will add better support for Cholesky, but I think it is OK to close this for now. |
Hi Luiz,
Thank you very much for adding the Cholesky factorization feature – it’s fantastic!
I have a question regarding the use of the computed Cholesky factor for solving linear systems. Specifically, can the Cholesky factor be used to solve equations of the form Lx=b or L^Tx=b.
I have a minimal (but not working code) here and my goal is to use
hchol.L
as a direct solver:For now, the last line leads to the following error message:
Could you provide any guidance on how to properly use the Cholesky factor in this context?
Thank you again for your help!
Charlie
The text was updated successfully, but these errors were encountered: