From 230ddae121eb90b823e1b4316361a64aa4bec9f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Gauth=C3=A9?= Date: Wed, 13 Nov 2024 18:39:36 -0500 Subject: [PATCH 1/4] rename flip_blockvector to flip_axes --- .../src/lib/GradedAxes/src/gradedunitrangedual.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/NDTensors/src/lib/GradedAxes/src/gradedunitrangedual.jl b/NDTensors/src/lib/GradedAxes/src/gradedunitrangedual.jl index df65ed6155..729092fe95 100644 --- a/NDTensors/src/lib/GradedAxes/src/gradedunitrangedual.jl +++ b/NDTensors/src/lib/GradedAxes/src/gradedunitrangedual.jl @@ -60,7 +60,7 @@ function blockedunitrange_getindices( a_indices = getindex(nondual(a), indices) v = mortar(dual.(blocks(a_indices))) # flip v to stay consistent with other cases where axes(v) are used - return flip_blockvector(v) + return flip_axes(v) end function blockedunitrange_getindices( @@ -71,7 +71,7 @@ function blockedunitrange_getindices( # GradedOneTo appears in mortar # flip v axis to preserve dual information # axes(v) will appear in axes(view(::BlockSparseArray, [Block(1)[1:1]])) - return flip_blockvector(v) + return flip_axes(v) end function blockedunitrange_getindices( @@ -91,7 +91,7 @@ function blockedunitrange_getindices( # GradedOneTo appears in mortar # flip v axis to preserve dual information # axes(v) will appear in axes(view(::BlockSparseArray, [Block(1)])) - return flip_blockvector(v) + return flip_axes(v) end # Fixes ambiguity error. @@ -106,10 +106,10 @@ function blockedunitrange_getindices( # `only(axes(a[indices])) isa `GradedUnitRange` # if `a isa `GradedUnitRange`, for example. v = mortar(blks, labelled_length.(blks)) - return flip_blockvector(v) + return flip_axes(v) end -function flip_blockvector(v::BlockVector) +function flip_axes(v::BlockVector) block_axes = flip.(axes(v)) flipped = mortar(vec.(blocks(v)), block_axes) return flipped From 75be561d425b4c87852a9c9500b15a47b4d29f75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Gauth=C3=A9?= Date: Wed, 13 Nov 2024 19:01:52 -0500 Subject: [PATCH 2/4] use blockedunitrange_getindex(nondual(a) --- .../lib/GradedAxes/src/gradedunitrangedual.jl | 43 +++++-------------- .../src/lib/GradedAxes/test/test_dual.jl | 1 + 2 files changed, 11 insertions(+), 33 deletions(-) diff --git a/NDTensors/src/lib/GradedAxes/src/gradedunitrangedual.jl b/NDTensors/src/lib/GradedAxes/src/gradedunitrangedual.jl index 729092fe95..1881868a6b 100644 --- a/NDTensors/src/lib/GradedAxes/src/gradedunitrangedual.jl +++ b/NDTensors/src/lib/GradedAxes/src/gradedunitrangedual.jl @@ -57,61 +57,38 @@ end function blockedunitrange_getindices( a::GradedUnitRangeDual, indices::Vector{<:BlockIndexRange{1}} ) - a_indices = getindex(nondual(a), indices) - v = mortar(dual.(blocks(a_indices))) # flip v to stay consistent with other cases where axes(v) are used - return flip_axes(v) + return dual_axes(blockedunitrange_getindices(nondual(a), indices)) end function blockedunitrange_getindices( a::GradedUnitRangeDual, indices::BlockVector{<:BlockIndex{1},<:Vector{<:BlockIndexRange{1}}}, ) - v = mortar(map(b -> a[b], blocks(indices))) - # GradedOneTo appears in mortar # flip v axis to preserve dual information # axes(v) will appear in axes(view(::BlockSparseArray, [Block(1)[1:1]])) - return flip_axes(v) + return dual_axes(blockedunitrange_getindices(nondual(a), indices)) end function blockedunitrange_getindices( a::GradedUnitRangeDual, indices::AbstractVector{<:Union{Block{1},BlockIndexRange{1}}} ) - # Without converting `indices` to `Vector`, - # mapping `indices` outputs a `BlockVector` - # which is harder to reason about. - vblocks = map(index -> a[index], Vector(indices)) - # We pass `length.(blocks)` to `mortar` in order - # to pass block labels to the axes of the output, - # if they exist. This makes it so that - # `only(axes(a[indices])) isa `GradedUnitRange` - # if `a isa `GradedUnitRange`, for example. - - v = mortar(vblocks, length.(vblocks)) - # GradedOneTo appears in mortar # flip v axis to preserve dual information # axes(v) will appear in axes(view(::BlockSparseArray, [Block(1)])) - return flip_axes(v) + return dual_axes(blockedunitrange_getindices(nondual(a), indices)) end # Fixes ambiguity error. -# TODO: Write this in terms of `blockedunitrange_getindices(dual(a), indices)`. function blockedunitrange_getindices( a::GradedUnitRangeDual, indices::AbstractBlockVector{<:Block{1}} ) - blks = map(bs -> mortar(map(b -> a[b], bs)), blocks(indices)) - # We pass `length.(blks)` to `mortar` in order - # to pass block labels to the axes of the output, - # if they exist. This makes it so that - # `only(axes(a[indices])) isa `GradedUnitRange` - # if `a isa `GradedUnitRange`, for example. - v = mortar(blks, labelled_length.(blks)) - return flip_axes(v) -end - -function flip_axes(v::BlockVector) - block_axes = flip.(axes(v)) - flipped = mortar(vec.(blocks(v)), block_axes) + return dual_axes(blockedunitrange_getindices(nondual(a), indices)) +end + +function dual_axes(v::BlockVector) + # dual both v elements and v axes + block_axes = dual.(axes(v)) + flipped = mortar(dual.(vec.(blocks(v))), block_axes) return flipped end diff --git a/NDTensors/src/lib/GradedAxes/test/test_dual.jl b/NDTensors/src/lib/GradedAxes/test/test_dual.jl index 98b8838542..24ef965533 100644 --- a/NDTensors/src/lib/GradedAxes/test/test_dual.jl +++ b/NDTensors/src/lib/GradedAxes/test/test_dual.jl @@ -227,6 +227,7 @@ end @test isdual(axes(ad[[Block(1)]], 1)) # used in view(::BlockSparseVector, [Block(1)]) @test isdual(axes(ad[mortar([Block(1)[1:1]])], 1)) # used in view(::BlockSparseVector, [Block(1)[1:1]]) + @test isdual(axes(ad[mortar([[Block(1)], [Block(2)]])])) end end From 229412c2e450545af00caa060b41ea4b57230c3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Gauth=C3=A9?= Date: Wed, 13 Nov 2024 19:07:18 -0500 Subject: [PATCH 3/4] fix test --- .../lib/GradedAxes/src/gradedunitrangedual.jl | 11 +++--- .../src/lib/GradedAxes/test/test_dual.jl | 36 ++++++++++++++----- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/NDTensors/src/lib/GradedAxes/src/gradedunitrangedual.jl b/NDTensors/src/lib/GradedAxes/src/gradedunitrangedual.jl index 1881868a6b..fc3abbadd5 100644 --- a/NDTensors/src/lib/GradedAxes/src/gradedunitrangedual.jl +++ b/NDTensors/src/lib/GradedAxes/src/gradedunitrangedual.jl @@ -57,7 +57,7 @@ end function blockedunitrange_getindices( a::GradedUnitRangeDual, indices::Vector{<:BlockIndexRange{1}} ) - # flip v to stay consistent with other cases where axes(v) are used + # dual v axes to stay consistent with other cases where axes(v) are used return dual_axes(blockedunitrange_getindices(nondual(a), indices)) end @@ -65,7 +65,7 @@ function blockedunitrange_getindices( a::GradedUnitRangeDual, indices::BlockVector{<:BlockIndex{1},<:Vector{<:BlockIndexRange{1}}}, ) - # flip v axis to preserve dual information + # dual v axis to preserve dual information # axes(v) will appear in axes(view(::BlockSparseArray, [Block(1)[1:1]])) return dual_axes(blockedunitrange_getindices(nondual(a), indices)) end @@ -73,7 +73,7 @@ end function blockedunitrange_getindices( a::GradedUnitRangeDual, indices::AbstractVector{<:Union{Block{1},BlockIndexRange{1}}} ) - # flip v axis to preserve dual information + # dual v axis to preserve dual information # axes(v) will appear in axes(view(::BlockSparseArray, [Block(1)])) return dual_axes(blockedunitrange_getindices(nondual(a), indices)) end @@ -82,7 +82,10 @@ end function blockedunitrange_getindices( a::GradedUnitRangeDual, indices::AbstractBlockVector{<:Block{1}} ) - return dual_axes(blockedunitrange_getindices(nondual(a), indices)) + v = blockedunitrange_getindices(nondual(a), indices) + # v elements are not dualled by dual_axes due to different structure. + # take element dual here. + return dual_axes(dual.(v)) end function dual_axes(v::BlockVector) diff --git a/NDTensors/src/lib/GradedAxes/test/test_dual.jl b/NDTensors/src/lib/GradedAxes/test/test_dual.jl index 24ef965533..71607a7f5c 100644 --- a/NDTensors/src/lib/GradedAxes/test/test_dual.jl +++ b/NDTensors/src/lib/GradedAxes/test/test_dual.jl @@ -219,15 +219,35 @@ end @test label(ad[Block(2)]) == U1(-1) @test label(ad[Block(2)[1:1]]) == U1(-1) - I = mortar([Block(2)[1:1]]) - g = ad[I] - @test length(g) == 1 - @test label(first(g)) == U1(-1) - @test isdual(g[Block(1)]) + v = ad[mortar([Block(2)[1:1]])] + @test v isa AbstractVector{LabelledInteger{Int64,U1}} + @test length(v) == 1 + @test label(first(v)) == U1(-1) + @test unlabel(first(v)) == 3 + @test isdual(v[Block(1)]) + @test isdual(axes(v, 1)) + @test blocklabels(axes(v, 1)) == [U1(-1)] - @test isdual(axes(ad[[Block(1)]], 1)) # used in view(::BlockSparseVector, [Block(1)]) - @test isdual(axes(ad[mortar([Block(1)[1:1]])], 1)) # used in view(::BlockSparseVector, [Block(1)[1:1]]) - @test isdual(axes(ad[mortar([[Block(1)], [Block(2)]])])) + v = ad[[Block(2)]] + @test v isa AbstractVector{LabelledInteger{Int64,U1}} + @test isdual(axes(v, 1)) # used in view(::BlockSparseVector, [Block(1)]) + @test label(first(v)) == U1(-1) + @test unlabel(first(v)) == 3 + @test blocklabels(axes(v, 1)) == [U1(-1)] + + v = ad[mortar([Block(2)[1:1]])] + @test v isa AbstractVector{LabelledInteger{Int64,U1}} + @test isdual(axes(v, 1)) # used in view(::BlockSparseVector, [Block(1)[1:1]]) + @test label(first(v)) == U1(-1) + @test unlabel(first(v)) == 3 + @test blocklabels(axes(v, 1)) == [U1(-1)] + + v = ad[mortar([[Block(2)], [Block(1)]])] + @test v isa AbstractVector{LabelledInteger{Int64,U1}} + @test isdual(axes(v, 1)) + @test label(first(v)) == U1(-1) + @test unlabel(first(v)) == 3 + @test blocklabels(axes(v, 1)) == [U1(-1), U1(0)] end end From 1756c3ca273eb641fcd5c304efbada9a7f955df8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Gauth=C3=A9?= Date: Thu, 14 Nov 2024 09:35:50 -0500 Subject: [PATCH 4/4] remove vec --- .../src/lib/GradedAxes/src/gradedunitrangedual.jl | 3 +-- NDTensors/src/lib/GradedAxes/test/test_dual.jl | 10 +++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/NDTensors/src/lib/GradedAxes/src/gradedunitrangedual.jl b/NDTensors/src/lib/GradedAxes/src/gradedunitrangedual.jl index fc3abbadd5..a16da982f9 100644 --- a/NDTensors/src/lib/GradedAxes/src/gradedunitrangedual.jl +++ b/NDTensors/src/lib/GradedAxes/src/gradedunitrangedual.jl @@ -91,8 +91,7 @@ end function dual_axes(v::BlockVector) # dual both v elements and v axes block_axes = dual.(axes(v)) - flipped = mortar(dual.(vec.(blocks(v))), block_axes) - return flipped + return mortar(dual.(blocks(v)), block_axes) end Base.axes(a::GradedUnitRangeDual) = axes(nondual(a)) diff --git a/NDTensors/src/lib/GradedAxes/test/test_dual.jl b/NDTensors/src/lib/GradedAxes/test/test_dual.jl index 71607a7f5c..f2b3072dc1 100644 --- a/NDTensors/src/lib/GradedAxes/test/test_dual.jl +++ b/NDTensors/src/lib/GradedAxes/test/test_dual.jl @@ -219,7 +219,7 @@ end @test label(ad[Block(2)]) == U1(-1) @test label(ad[Block(2)[1:1]]) == U1(-1) - v = ad[mortar([Block(2)[1:1]])] + v = ad[[Block(2)[1:1]]] @test v isa AbstractVector{LabelledInteger{Int64,U1}} @test length(v) == 1 @test label(first(v)) == U1(-1) @@ -228,16 +228,16 @@ end @test isdual(axes(v, 1)) @test blocklabels(axes(v, 1)) == [U1(-1)] - v = ad[[Block(2)]] + v = ad[mortar([Block(2)[1:1]])] @test v isa AbstractVector{LabelledInteger{Int64,U1}} - @test isdual(axes(v, 1)) # used in view(::BlockSparseVector, [Block(1)]) + @test isdual(axes(v, 1)) # used in view(::BlockSparseVector, [Block(1)[1:1]]) @test label(first(v)) == U1(-1) @test unlabel(first(v)) == 3 @test blocklabels(axes(v, 1)) == [U1(-1)] - v = ad[mortar([Block(2)[1:1]])] + v = ad[[Block(2)]] @test v isa AbstractVector{LabelledInteger{Int64,U1}} - @test isdual(axes(v, 1)) # used in view(::BlockSparseVector, [Block(1)[1:1]]) + @test isdual(axes(v, 1)) # used in view(::BlockSparseVector, [Block(1)]) @test label(first(v)) == U1(-1) @test unlabel(first(v)) == 3 @test blocklabels(axes(v, 1)) == [U1(-1)]