Skip to content

Commit de01986

Browse files
authored
Adjoint for vectors may use trailing axes of the parent (#52379)
Close #52373, or at least the part that may be addressed here. After this, the first axis for an `Adjoint(parent::AbstractVector)` will be the second axis of the `parent`. This will change the type of the axis where the parent array type specializes `axes(A, d)`. In the short term, this would improve type-stability in cases such as ```julia julia> A = OffsetArray([1,2], 2); julia> @code_typed axes(A')[1] CodeInfo( 1 ─ %1 = $(Expr(:boundscheck))::Bool │ %2 = Base.getfield(t, i, %1)::OffsetArrays.IdOffsetRange{Int64, Base.OneTo{Int64}} └── return %2 ) => OffsetArrays.IdOffsetRange{Int64, Base.OneTo{Int64}} ``` where the result is now concretely inferred instead of being a small `Union`. In principle, with JuliaArrays/StaticArrays.jl#916, this would make the axes of the adjoint of a `StaticVector` statically sized.
1 parent b7351a9 commit de01986

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/adjtrans.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ axes(A::AdjOrTrans) = reverse(axes(A.parent))
324324
length(A::AdjOrTrans) = length(A.parent)
325325
size(v::AdjOrTransAbsVec) = (1, length(v.parent))
326326
size(A::AdjOrTransAbsMat) = reverse(size(A.parent))
327-
axes(v::AdjOrTransAbsVec) = (Base.OneTo(1), axes(v.parent)...)
327+
axes(v::AdjOrTransAbsVec) = (axes(v.parent,2), axes(v.parent)...)
328328
axes(A::AdjOrTransAbsMat) = reverse(axes(A.parent))
329329
IndexStyle(::Type{<:AdjOrTransAbsVec}) = IndexLinear()
330330
IndexStyle(::Type{<:AdjOrTransAbsMat}) = IndexCartesian()

test/adjtrans.jl

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ using Test, LinearAlgebra
66

77
const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
88

9+
isdefined(Main, :OffsetArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "OffsetArrays.jl"))
10+
using .Main.OffsetArrays
11+
912
@testset "Adjoint and Transpose inner constructor basics" begin
1013
intvec, intmat = [1, 2], [1 2; 3 4]
1114
# Adjoint/Transpose eltype must match the type of the Adjoint/Transpose of the input eltype
@@ -87,11 +90,15 @@ end
8790
@test size(Transpose(intvec)) == (1, length(intvec))
8891
@test size(Transpose(intmat)) == reverse(size(intmat))
8992
end
90-
@testset "indices methods" begin
93+
@testset "axes methods" begin
9194
@test axes(Adjoint(intvec)) == (Base.OneTo(1), Base.OneTo(length(intvec)))
9295
@test axes(Adjoint(intmat)) == reverse(axes(intmat))
9396
@test axes(Transpose(intvec)) == (Base.OneTo(1), Base.OneTo(length(intvec)))
9497
@test axes(Transpose(intmat)) == reverse(axes(intmat))
98+
99+
A = OffsetArray([1,2], 2)
100+
@test (@inferred axes(A')[2]) === axes(A,1)
101+
@test (@inferred axes(A')[1]) === axes(A,2)
95102
end
96103
@testset "IndexStyle methods" begin
97104
@test IndexStyle(Adjoint(intvec)) == IndexLinear()

0 commit comments

Comments
 (0)