Skip to content

Commit

Permalink
fixed a bug in ulvdivide.jl regarding unbalanced cluster trees
Browse files Browse the repository at this point in the history
  • Loading branch information
bonevbs committed Apr 29, 2021
1 parent 3f080ca commit 78f1041
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "HssMatrices"
uuid = "0c1fde87-e0ea-49d6-ac2f-89b694f639e1"
authors = ["Boris Bonev <[email protected]>"]
version = "0.1.3"
version = "0.1.4"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down
16 changes: 13 additions & 3 deletions src/ulvdivide.jl
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ function _unpackadd_rows!(hssA::HssMatrix{T}, hssB::HssMatrix{T}, ktree::BinaryN

hssA.A11 = _unpackadd_rows!(hssA.A11, hssB.A11, ktree.left)
hssA.A22 = _unpackadd_rows!(hssA.A22, hssB.A22, ktree.right)
hssA.sz1 == size(hssA.A11) || error("Didn't expect dimensions to change")
hssA.sz2 == size(hssA.A22) || error("Didn't expect dimensions to change")
hssA.sz1 == size(hssA.A11) || error("dimensions got screwed up!")
hssA.sz2 == size(hssA.A22) || error("dimensions got screwed up!")
else
isbranch(ktree) || throw(ArgumentError("didn't expect ktree to be a leaf node"))
k1 = ktree.left.data; k2 = ktree.right.data
Expand All @@ -218,7 +218,17 @@ function _unpackadd_rows!(hssA::HssMatrix{T}, hssB::HssMatrix{T}, ktree::BinaryN
end
return hssA
else
error("Expected hssA to be a branch node")
if isbranch(hssB)
error("Something went wrong. Expected hssB to also be a leaf node")
else
k, rk = size(hssB.U);
k <= size(hssA,1) || throw(DimensionMismatch("expected hssB to have less rows than hssA."))
hssA.U = [hssA.U zeros(size(hssA.U,1), rk)]
hssA.U[end-k+1:end, end-rk+1:end] .= hssB.U
hssA.V = [hssA.V hssB.V]
hssA.D[end-k+1:end, :] .= hssA.D[end-k+1:end, :] .+ hssB.D
return hssA
end
end
end

Expand Down
19 changes: 14 additions & 5 deletions test/examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ using Plots
K(x,y) = (x-y) > 0 ? 0.001/(x-y) : 2.
#K(x,y) = (x-y) != 0 ? 1/(x-y) : 1.
A = [ K(x,y) for x=-1:0.001:1, y=-1:0.001:1];
C = inv(A);

# test the simple implementation of cluster trees
m, n = size(A)
Expand Down Expand Up @@ -50,16 +51,24 @@ Id(i,j) = Matrix{Float64}(i.*ones(length(j))' .== ones(length(i)).*j')
IdOp = LinearMap{Float64}(n, n, (y,_,x) -> x, (y,_,x) -> x, (i,j) -> Id(i,j))
hssI = randcompress(IdOp, ccl, ccl, 0)
hssC = ldiv!(hssA, hssI)
println("rel. error in the solution of AX = I: ", norm(full(hssC) - inv(A)) / norm(inv(A)) )
println("abs. error in the solution of AX = I: ", norm(full(hssC) - inv(A)) )
println("rel. error in the solution of AX = I: ", norm(full(hssC) - C) / norm(C) )
println("abs. error in the solution of AX = I: ", norm(full(hssC) - C) )

# test left HSS division
Id(i,j) = Matrix{Float64}(i.*ones(length(j))' .== ones(length(i)).*j')
IdOp = LinearMap{Float64}(n, n, (y,_,x) -> x, (y,_,x) -> x, (i,j) -> Id(i,j))
hssI = randcompress(IdOp, ccl, ccl, 0)
hssC = rdiv!(hssI, hssA)
println("rel. error in the solution of XA = I: ", norm(full(hssC) - inv(A)) / norm(inv(A)) )
println("abs. error in the solution of XA = I: ", norm(full(hssC) - inv(A)) )
println("rel. error in the solution of XA = I: ", norm(full(hssC) - C) / norm(C) )
println("abs. error in the solution of XA = I: ", norm(full(hssC) - C) )

# desequilibrate cluster trees and try again
hssA.A11 = prune_leaves!(hssA.A11)
hssI = randcompress(IdOp, ccl, ccl, 0)
hssI.A11 = prune_leaves!(hssI.A11)
hssC = ldiv!(hssA, hssI)
println("rel. error in the solution of XA = I: ", norm(full(hssC) - C) / norm(C) )
println("abs. error in the solution of XA = I: ", norm(full(hssC) - C) )


# # test computation of generators
Expand All @@ -79,4 +88,4 @@ println("abs. error in the solution of XA = I: ", norm(full(hssC) - inv(A)) )
# @time x = ulvfactsolve(hssA, b);

# # test plotting
# plot = plotranks(hssA)
# plot = plotranks(hssA)
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ end;
Ainv = inv(A)
@test norm(Ainv - full(hssA\hssI))/norm(Ainv) c*HssOptions().rtol || norm(Ainv - full(hssA\hssI)) c*HssOptions().atol
@test norm(Ainv - full(hssI/hssA))/norm(Ainv) c*HssOptions().rtol || norm(Ainv - full(hssI/hssA)) c*HssOptions().atol
hssA.A11 = prune_leaves!(hssA.A11)
hssI.A11 = prune_leaves!(hssI.A11)
@test norm(Ainv - full(hssA\hssI))/norm(Ainv) c*HssOptions().rtol || norm(Ainv - full(hssA\hssI)) c*HssOptions().atol
@test norm(Ainv - full(hssI/hssA))/norm(Ainv) c*HssOptions().rtol || norm(Ainv - full(hssI/hssA)) c*HssOptions().atol
end

2 comments on commit 78f1041

@bonevbs
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

Release Notes:

  • Bugfix in ULV division for unequilibrated cluster trees

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/35670

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.4 -m "<description of version>" 78f1041f7e5e0fdba696ac2853b198b8f1727b9e
git push origin v0.1.4

Please sign in to comment.