Skip to content

Commit 6ee0f5c

Browse files
dkarraschandyferris
authored andcommitted
Remove eltype check in adjtrans (JuliaLang#40567)
Co-authored-by: Andy Ferris <[email protected]> Co-authored-by: Andy Ferris <[email protected]>
1 parent 039807e commit 6ee0f5c

File tree

2 files changed

+10
-38
lines changed

2 files changed

+10
-38
lines changed

stdlib/LinearAlgebra/src/adjtrans.jl

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ julia> adjoint(A)
3434
"""
3535
struct Adjoint{T,S} <: AbstractMatrix{T}
3636
parent::S
37-
function Adjoint{T,S}(A::S) where {T,S}
38-
checkeltype_adjoint(T, eltype(A))
39-
new(A)
40-
end
4137
end
4238
"""
4339
Transpose
@@ -65,30 +61,6 @@ julia> transpose(A)
6561
"""
6662
struct Transpose{T,S} <: AbstractMatrix{T}
6763
parent::S
68-
function Transpose{T,S}(A::S) where {T,S}
69-
checkeltype_transpose(T, eltype(A))
70-
new(A)
71-
end
72-
end
73-
74-
function checkeltype_adjoint(::Type{ResultEltype}, ::Type{ParentEltype}) where {ResultEltype,ParentEltype}
75-
Expected = Base.promote_op(adjoint, ParentEltype)
76-
ResultEltype === Expected || error(string(
77-
"Element type mismatch. Tried to create an `Adjoint{", ResultEltype, "}` ",
78-
"from an object with eltype `", ParentEltype, "`, but the element type of ",
79-
"the adjoint of an object with eltype `", ParentEltype, "` must be ",
80-
"`", Expected, "`."))
81-
return nothing
82-
end
83-
84-
function checkeltype_transpose(::Type{ResultEltype}, ::Type{ParentEltype}) where {ResultEltype, ParentEltype}
85-
Expected = Base.promote_op(transpose, ParentEltype)
86-
ResultEltype === Expected || error(string(
87-
"Element type mismatch. Tried to create a `Transpose{", ResultEltype, "}` ",
88-
"from an object with eltype `", ParentEltype, "`, but the element type of ",
89-
"the transpose of an object with eltype `", ParentEltype, "` must be ",
90-
"`", Expected, "`."))
91-
return nothing
9264
end
9365

9466
# basic outer constructors
@@ -203,8 +175,8 @@ axes(v::AdjOrTransAbsVec) = (Base.OneTo(1), axes(v.parent)...)
203175
axes(A::AdjOrTransAbsMat) = reverse(axes(A.parent))
204176
IndexStyle(::Type{<:AdjOrTransAbsVec}) = IndexLinear()
205177
IndexStyle(::Type{<:AdjOrTransAbsMat}) = IndexCartesian()
206-
@propagate_inbounds getindex(v::AdjOrTransAbsVec, i::Int) = wrapperop(v)(v.parent[i-1+first(axes(v.parent)[1])])
207-
@propagate_inbounds getindex(A::AdjOrTransAbsMat, i::Int, j::Int) = wrapperop(A)(A.parent[j, i])
178+
@propagate_inbounds getindex(v::AdjOrTransAbsVec{T}, i::Int) where {T} = wrapperop(v)(v.parent[i-1+first(axes(v.parent)[1])])::T
179+
@propagate_inbounds getindex(A::AdjOrTransAbsMat{T}, i::Int, j::Int) where {T} = wrapperop(A)(A.parent[j, i])::T
208180
@propagate_inbounds setindex!(v::AdjOrTransAbsVec, x, i::Int) = (setindex!(v.parent, wrapperop(v)(x), i-1+first(axes(v.parent)[1])); v)
209181
@propagate_inbounds setindex!(A::AdjOrTransAbsMat, x, i::Int, j::Int) = (setindex!(A.parent, wrapperop(A)(x), j, i); A)
210182
# AbstractArray interface, additional definitions to retain wrapper over vectors where appropriate

stdlib/LinearAlgebra/test/adjtrans.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ using Test, LinearAlgebra, SparseArrays
77
@testset "Adjoint and Transpose inner constructor basics" begin
88
intvec, intmat = [1, 2], [1 2; 3 4]
99
# Adjoint/Transpose eltype must match the type of the Adjoint/Transpose of the input eltype
10-
@test_throws ErrorException Adjoint{Float64,Vector{Int}}(intvec)
11-
@test_throws ErrorException Adjoint{Float64,Matrix{Int}}(intmat)
12-
@test_throws ErrorException Transpose{Float64,Vector{Int}}(intvec)
13-
@test_throws ErrorException Transpose{Float64,Matrix{Int}}(intmat)
10+
@test_throws TypeError Adjoint{Float64,Vector{Int}}(intvec)[1,1]
11+
@test_throws TypeError Adjoint{Float64,Matrix{Int}}(intmat)[1,1]
12+
@test_throws TypeError Transpose{Float64,Vector{Int}}(intvec)[1,1]
13+
@test_throws TypeError Transpose{Float64,Matrix{Int}}(intmat)[1,1]
1414
# Adjoint/Transpose wrapped array type must match the input array type
15-
@test_throws MethodError Adjoint{Int,Vector{Float64}}(intvec)
16-
@test_throws MethodError Adjoint{Int,Matrix{Float64}}(intmat)
17-
@test_throws MethodError Transpose{Int,Vector{Float64}}(intvec)
18-
@test_throws MethodError Transpose{Int,Matrix{Float64}}(intmat)
15+
@test_throws TypeError Adjoint{Int,Vector{Float64}}(intvec)[1,1]
16+
@test_throws TypeError Adjoint{Int,Matrix{Float64}}(intmat)[1,1]
17+
@test_throws TypeError Transpose{Int,Vector{Float64}}(intvec)[1,1]
18+
@test_throws TypeError Transpose{Int,Matrix{Float64}}(intmat)[1,1]
1919
# Adjoint/Transpose inner constructor basic functionality, concrete scalar eltype
2020
@test (Adjoint{Int,Vector{Int}}(intvec)::Adjoint{Int,Vector{Int}}).parent === intvec
2121
@test (Adjoint{Int,Matrix{Int}}(intmat)::Adjoint{Int,Matrix{Int}}).parent === intmat

0 commit comments

Comments
 (0)