Skip to content

Commit

Permalink
Add row/colsupport for InvLayout(TriangularLayout) properly (#341)
Browse files Browse the repository at this point in the history
* Implement inv(TriangularLayout) properly

* Support ranges
  • Loading branch information
DanielVandH authored Jul 16, 2024
1 parent 66ae7a2 commit c22b86d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "LazyArrays"
uuid = "5078a376-72f3-5289-bfd5-ec5146d43c02"
version = "2.1.8"
version = "2.1.9"

[deps]
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
Expand Down
21 changes: 20 additions & 1 deletion src/linalg/inv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,23 @@ getindex(L::ApplyMatrix{<:Any,typeof(/)}, k::Integer, ::Colon) = permutedims(L.a
getindex(L::ApplyMatrix{<:Any,typeof(/)}, k::Integer, j::Integer) = L[k,:][j]


inv_layout(::LazyLayouts, _, A) = ApplyArray(inv, A)
inv_layout(::LazyLayouts, _, A) = ApplyArray(inv, A)

function colsupport(lay::AbstractInvLayout{TriLay}, A, j) where {S,TriLay<:TriangularLayout{S}}
isempty(j) && return 1:0
B, = arguments(lay, A)
if S == 'U'
return firstindex(B, 2):(maximum(j) - firstindex(B, 2) + 1)
else # S == 'L'
return minimum(j):size(B, 2)
end
end
function rowsupport(lay::AbstractInvLayout{TriLay}, A, k) where {S,TriLay<:TriangularLayout{S}}
isempty(k) && return 1:0
B, = arguments(lay, A)
if S == 'U'
return minimum(k):size(B, 1)
else # S == 'L'
return firstindex(B, 1):(maximum(k) - firstindex(B, 1) + 1)
end
end
17 changes: 17 additions & 0 deletions test/bandedtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -934,4 +934,21 @@ LinearAlgebra.lmul!(β::Number, A::PseudoBandedMatrix) = (lmul!(β, A.data); A)
end
end

@testset "Issue #329" begin
# Make sure that we aren't giving the incorrect bandwidths
U = UpperTriangular(ApplyArray(inv, brand(5, 5, 1, 2)))
invU = inv(U)
L = LowerTriangular(ApplyArray(inv, brand(10, 10, 3, 4)))
invL = inv(L)
@test colsupport(invU, 1) == 1:1
@test colsupport(invU, 3) == 1:3
@test rowsupport(invU, 1) == 1:5
@test rowsupport(invU, 4) == 4:5
@test rowsupport(invU, 5) == 5:5
@test colsupport(invL, 1) == 1:10
@test colsupport(invL, 5) == 5:10
@test rowsupport(invL, 1) == 1:1
@test rowsupport(invL, 4) == 1:4
end

end # module
26 changes: 25 additions & 1 deletion test/ldivtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,28 @@ end
@test Z \ Y [-2.0 1.0; 1.5 -0.5]
end

end # module
@testset "Issue #329" begin
U = UpperTriangular(ApplyArray(inv, rand(5, 5)))
invU = inv(U)
L = LowerTriangular(ApplyArray(inv, rand(10, 10)))
invL = inv(L)
@test colsupport(invU, 1) == 1:1
@test colsupport(invU, 3) == 1:3
@test colsupport(invU, 1:3) == 1:3
@test rowsupport(invU, 1) == 1:5
@test rowsupport(invU, 4) == 4:5
@test rowsupport(invU, 5) == 5:5
@test rowsupport(invU, 3:5) == 3:5
@test colsupport(invL, 1) == 1:10
@test colsupport(invL, 5) == 5:10
@test colsupport(invL, 2:4) == 2:10
@test rowsupport(invL, 1) == 1:1
@test rowsupport(invL, 4) == 1:4
@test rowsupport(invL, 2:2:4) == 1:4
@test colsupport(invU, ()) == 1:0
@test rowsupport(invU, ()) == 1:0
@test colsupport(invL, ()) == 1:0
@test rowsupport(invL, ()) == 1:0
end

end # module

2 comments on commit c22b86d

@dlfivefifty
Copy link
Member

Choose a reason for hiding this comment

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

@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/111245

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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 v2.1.9 -m "<description of version>" c22b86d699e8a52136185b8bf27b58ba7685a4dd
git push origin v2.1.9

Please sign in to comment.