Skip to content

Remove eltype check in adjtrans #40567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 2 additions & 30 deletions stdlib/LinearAlgebra/src/adjtrans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ julia> adjoint(A)
"""
struct Adjoint{T,S} <: AbstractMatrix{T}
parent::S
function Adjoint{T,S}(A::S) where {T,S}
checkeltype_adjoint(T, eltype(A))
new(A)
end
end
"""
Transpose
Expand Down Expand Up @@ -65,30 +61,6 @@ julia> transpose(A)
"""
struct Transpose{T,S} <: AbstractMatrix{T}
parent::S
function Transpose{T,S}(A::S) where {T,S}
checkeltype_transpose(T, eltype(A))
new(A)
end
end

function checkeltype_adjoint(::Type{ResultEltype}, ::Type{ParentEltype}) where {ResultEltype,ParentEltype}
Expected = Base.promote_op(adjoint, ParentEltype)
ResultEltype === Expected || error(string(
"Element type mismatch. Tried to create an `Adjoint{", ResultEltype, "}` ",
"from an object with eltype `", ParentEltype, "`, but the element type of ",
"the adjoint of an object with eltype `", ParentEltype, "` must be ",
"`", Expected, "`."))
return nothing
end

function checkeltype_transpose(::Type{ResultEltype}, ::Type{ParentEltype}) where {ResultEltype, ParentEltype}
Expected = Base.promote_op(transpose, ParentEltype)
ResultEltype === Expected || error(string(
"Element type mismatch. Tried to create a `Transpose{", ResultEltype, "}` ",
"from an object with eltype `", ParentEltype, "`, but the element type of ",
"the transpose of an object with eltype `", ParentEltype, "` must be ",
"`", Expected, "`."))
return nothing
end

# basic outer constructors
Expand Down Expand Up @@ -203,8 +175,8 @@ axes(v::AdjOrTransAbsVec) = (Base.OneTo(1), axes(v.parent)...)
axes(A::AdjOrTransAbsMat) = reverse(axes(A.parent))
IndexStyle(::Type{<:AdjOrTransAbsVec}) = IndexLinear()
IndexStyle(::Type{<:AdjOrTransAbsMat}) = IndexCartesian()
@propagate_inbounds getindex(v::AdjOrTransAbsVec, i::Int) = wrapperop(v)(v.parent[i-1+first(axes(v.parent)[1])])
@propagate_inbounds getindex(A::AdjOrTransAbsMat, i::Int, j::Int) = wrapperop(A)(A.parent[j, i])
@propagate_inbounds getindex(v::AdjOrTransAbsVec{T}, i::Int) where {T} = wrapperop(v)(v.parent[i-1+first(axes(v.parent)[1])])::T
@propagate_inbounds getindex(A::AdjOrTransAbsMat{T}, i::Int, j::Int) where {T} = wrapperop(A)(A.parent[j, i])::T
Copy link
Member

Choose a reason for hiding this comment

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

Should this also apply to the getindex methods on the next line?

@propagate_inbounds setindex!(v::AdjOrTransAbsVec, x, i::Int) = (setindex!(v.parent, wrapperop(v)(x), i-1+first(axes(v.parent)[1])); v)
@propagate_inbounds setindex!(A::AdjOrTransAbsMat, x, i::Int, j::Int) = (setindex!(A.parent, wrapperop(A)(x), j, i); A)
# AbstractArray interface, additional definitions to retain wrapper over vectors where appropriate
Expand Down
16 changes: 8 additions & 8 deletions stdlib/LinearAlgebra/test/adjtrans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ using Test, LinearAlgebra, SparseArrays
@testset "Adjoint and Transpose inner constructor basics" begin
intvec, intmat = [1, 2], [1 2; 3 4]
# Adjoint/Transpose eltype must match the type of the Adjoint/Transpose of the input eltype
@test_throws ErrorException Adjoint{Float64,Vector{Int}}(intvec)
@test_throws ErrorException Adjoint{Float64,Matrix{Int}}(intmat)
@test_throws ErrorException Transpose{Float64,Vector{Int}}(intvec)
@test_throws ErrorException Transpose{Float64,Matrix{Int}}(intmat)
@test_throws TypeError Adjoint{Float64,Vector{Int}}(intvec)[1,1]
@test_throws TypeError Adjoint{Float64,Matrix{Int}}(intmat)[1,1]
@test_throws TypeError Transpose{Float64,Vector{Int}}(intvec)[1,1]
@test_throws TypeError Transpose{Float64,Matrix{Int}}(intmat)[1,1]
# Adjoint/Transpose wrapped array type must match the input array type
@test_throws MethodError Adjoint{Int,Vector{Float64}}(intvec)
@test_throws MethodError Adjoint{Int,Matrix{Float64}}(intmat)
@test_throws MethodError Transpose{Int,Vector{Float64}}(intvec)
@test_throws MethodError Transpose{Int,Matrix{Float64}}(intmat)
@test_throws TypeError Adjoint{Int,Vector{Float64}}(intvec)[1,1]
@test_throws TypeError Adjoint{Int,Matrix{Float64}}(intmat)[1,1]
@test_throws TypeError Transpose{Int,Vector{Float64}}(intvec)[1,1]
@test_throws TypeError Transpose{Int,Matrix{Float64}}(intmat)[1,1]
# Adjoint/Transpose inner constructor basic functionality, concrete scalar eltype
@test (Adjoint{Int,Vector{Int}}(intvec)::Adjoint{Int,Vector{Int}}).parent === intvec
@test (Adjoint{Int,Matrix{Int}}(intmat)::Adjoint{Int,Matrix{Int}}).parent === intmat
Expand Down