Skip to content

Commit c22b86d

Browse files
authored
Add row/colsupport for InvLayout(TriangularLayout) properly (#341)
* Implement inv(TriangularLayout) properly * Support ranges
1 parent 66ae7a2 commit c22b86d

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "LazyArrays"
22
uuid = "5078a376-72f3-5289-bfd5-ec5146d43c02"
3-
version = "2.1.8"
3+
version = "2.1.9"
44

55
[deps]
66
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"

src/linalg/inv.jl

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,23 @@ getindex(L::ApplyMatrix{<:Any,typeof(/)}, k::Integer, ::Colon) = permutedims(L.a
184184
getindex(L::ApplyMatrix{<:Any,typeof(/)}, k::Integer, j::Integer) = L[k,:][j]
185185

186186

187-
inv_layout(::LazyLayouts, _, A) = ApplyArray(inv, A)
187+
inv_layout(::LazyLayouts, _, A) = ApplyArray(inv, A)
188+
189+
function colsupport(lay::AbstractInvLayout{TriLay}, A, j) where {S,TriLay<:TriangularLayout{S}}
190+
isempty(j) && return 1:0
191+
B, = arguments(lay, A)
192+
if S == 'U'
193+
return firstindex(B, 2):(maximum(j) - firstindex(B, 2) + 1)
194+
else # S == 'L'
195+
return minimum(j):size(B, 2)
196+
end
197+
end
198+
function rowsupport(lay::AbstractInvLayout{TriLay}, A, k) where {S,TriLay<:TriangularLayout{S}}
199+
isempty(k) && return 1:0
200+
B, = arguments(lay, A)
201+
if S == 'U'
202+
return minimum(k):size(B, 1)
203+
else # S == 'L'
204+
return firstindex(B, 1):(maximum(k) - firstindex(B, 1) + 1)
205+
end
206+
end

test/bandedtests.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,4 +934,21 @@ LinearAlgebra.lmul!(β::Number, A::PseudoBandedMatrix) = (lmul!(β, A.data); A)
934934
end
935935
end
936936

937+
@testset "Issue #329" begin
938+
# Make sure that we aren't giving the incorrect bandwidths
939+
U = UpperTriangular(ApplyArray(inv, brand(5, 5, 1, 2)))
940+
invU = inv(U)
941+
L = LowerTriangular(ApplyArray(inv, brand(10, 10, 3, 4)))
942+
invL = inv(L)
943+
@test colsupport(invU, 1) == 1:1
944+
@test colsupport(invU, 3) == 1:3
945+
@test rowsupport(invU, 1) == 1:5
946+
@test rowsupport(invU, 4) == 4:5
947+
@test rowsupport(invU, 5) == 5:5
948+
@test colsupport(invL, 1) == 1:10
949+
@test colsupport(invL, 5) == 5:10
950+
@test rowsupport(invL, 1) == 1:1
951+
@test rowsupport(invL, 4) == 1:4
952+
end
953+
937954
end # module

test/ldivtests.jl

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,28 @@ end
181181
@test Z \ Y [-2.0 1.0; 1.5 -0.5]
182182
end
183183

184-
end # module
184+
@testset "Issue #329" begin
185+
U = UpperTriangular(ApplyArray(inv, rand(5, 5)))
186+
invU = inv(U)
187+
L = LowerTriangular(ApplyArray(inv, rand(10, 10)))
188+
invL = inv(L)
189+
@test colsupport(invU, 1) == 1:1
190+
@test colsupport(invU, 3) == 1:3
191+
@test colsupport(invU, 1:3) == 1:3
192+
@test rowsupport(invU, 1) == 1:5
193+
@test rowsupport(invU, 4) == 4:5
194+
@test rowsupport(invU, 5) == 5:5
195+
@test rowsupport(invU, 3:5) == 3:5
196+
@test colsupport(invL, 1) == 1:10
197+
@test colsupport(invL, 5) == 5:10
198+
@test colsupport(invL, 2:4) == 2:10
199+
@test rowsupport(invL, 1) == 1:1
200+
@test rowsupport(invL, 4) == 1:4
201+
@test rowsupport(invL, 2:2:4) == 1:4
202+
@test colsupport(invU, ()) == 1:0
203+
@test rowsupport(invU, ()) == 1:0
204+
@test colsupport(invL, ()) == 1:0
205+
@test rowsupport(invL, ()) == 1:0
206+
end
207+
208+
end # module

0 commit comments

Comments
 (0)