Skip to content

Commit

Permalink
Merge pull request #18 from putianyi889/version-0.0.4.6
Browse files Browse the repository at this point in the history
add _to_eltype supports
  • Loading branch information
putianyi889 authored Mar 16, 2024
2 parents 39ad44d + 1458ed2 commit 4de713c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
27 changes: 19 additions & 8 deletions src/EltypeExtensions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import LinearAlgebra: AbstractQ

export elconvert, basetype, baseconvert, precisiontype, precisionconvert

@static if VERSION < v"1.10"
@inline bigfloatconvert(x, prec) = BigFloat(x, prec)
else
@static if VERSION >= v"1.3"
_to_eltype(::Type{T}, ::Type{UpperHessenberg{S,M}}) where {T,S,M} = UpperHessenberg{T,_to_eltype(T,M)}
elconvert(::Type{T}, A::UpperHessenberg{S,M}) where {T,S,M} = UpperHessenberg{T,_to_eltype(T,M)}(A)
end
@static if VERSION >= v"1.10" # see https://github.com/JuliaLang/julia/pull/46196
elconvert(::Type{T}, A::AbstractQ) where T = convert(AbstractQ{T}, A)
@inline bigfloatconvert(x, prec) = BigFloat(x, precision = prec)
else
@inline bigfloatconvert(x, prec) = BigFloat(x, prec)
end

"""
Expand All @@ -31,9 +36,6 @@ elconvert(::Type{T}, A::AbstractArray) where T = convert(AbstractArray{T}, A)
elconvert(::Type{T}, A::AbstractRange) where T = map(T, A)
elconvert(::Type{T}, A::AbstractUnitRange) where T<:Integer = convert(AbstractUnitRange{T}, A)
elconvert(::Type{T}, A::Tuple) where T = convert.(T, A)
if !(AbstractQ <: AbstractMatrix) # see https://github.com/JuliaLang/julia/pull/46196
elconvert(::Type{T}, A::AbstractQ) where T = convert(AbstractQ{T}, A)
end

"""
_to_eltype(T, S)
Expand All @@ -43,8 +45,17 @@ Convert type `S` to have the `eltype` of `T`.
_to_eltype(::Type{T}, ::Type{S}) where {T,S} = eltype(S) == S ? T : MethodError(_to_eltype, T, S)
_to_eltype(::Type{T}, ::Type{Array{S,N}}) where {T,S,N} = Array{T,N}
_to_eltype(::Type{T}, ::Type{<:Set}) where T = Set{T}
_to_eltype(::Type{T}, ::Type{Symmetric{S,M}}) where {T,S,M} = Symmetric{T,_to_eltype(T,M)}
_to_eltype(::Type{T}, ::Type{<:UnitRange}) where T = UnitRange{T}
for TYP in (Adjoint, Diagonal, Hermitian, Symmetric, SymTridiagonal, Transpose)
@eval _to_eltype(::Type{T}, ::Type{$TYP{S,M}}) where {T,S,M} = $TYP{T,_to_eltype(T,M)}
@eval elconvert(::Type{T}, A::S) where {T,S<:$TYP} = convert(_to_eltype(T, S), A)
end
_to_eltype(::Type{T}, ::Type{<:UnitRange}) where T<:Integer = UnitRange{T}

@static if VERSION >= v"1.7"
_to_eltype(::Type{T}, ::Type{<:UnitRange}) where T<:Real = StepRangeLen{T,Base.TwicePrecision{T},Base.TwicePrecision{T},Int}
else
_to_eltype(::Type{T}, ::Type{<:UnitRange}) where T<:Real = StepRangeLen{T,Base.TwicePrecision{T},Base.TwicePrecision{T}}
end

nutype(x) = nutype(typeof(x))
nutype(T::Type) = throw(MethodError(nutype, T))
Expand Down
24 changes: 23 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
using Documenter
using EltypeExtensions
using EltypeExtensions: _to_precisiontype
using EltypeExtensions: _to_precisiontype, _to_eltype
using Test
using Aqua
using LinearAlgebra

function testelconvert(T, A)
@test elconvert(T, A) isa _to_eltype(T, typeof(A))
end

@testset "elconvert" begin
A = rand(3,3)
testelconvert(Float16, A)
testelconvert(Float16, Symmetric(A))
testelconvert(Float16, A')
testelconvert(Float16, transpose(A))
testelconvert(Float16, SymTridiagonal(Symmetric(A)))
if VERSION >= v"1.3"
testelconvert(Float16, UpperHessenberg(A))
end
testelconvert(Float16, Hermitian(A))

r = 1:5
testelconvert(Int8, r)
testelconvert(Float64, r)
end

@testset "bugs" begin
@test _to_precisiontype(Float64, Complex) == Complex{Float64}
Expand Down

0 comments on commit 4de713c

Please sign in to comment.