Skip to content

Commit e6d1dab

Browse files
committed
update
1 parent 170cbd9 commit e6d1dab

File tree

4 files changed

+69
-65
lines changed

4 files changed

+69
-65
lines changed

docs/src/index.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ We note that this package has some overlap with [TypeUtils.jl](https://github.co
66

77
## Introduction
88

9-
### `elconvert` and `_to_eltype`
10-
`elconvert(T, x)` works like `convert(T, x)`, except that `T` refers to the eltype of the result. This can be useful for generic codes.
9+
### `convert_eltype` and `_to_eltype`
10+
`convert_eltype(T, x)` works like `convert(T, x)`, except that `T` refers to the eltype of the result. This can be useful for generic codes.
1111

12-
It should be always true that `elconvert(T, x) isa _to_eltype(T, typeof(x))`. However, since `elconvert` and `_to_eltype` use different routines, it's possible that the equality doesn't hold for some types. Please submit an issue or PR if that happens.
12+
It should be always true that `convert_eltype(T, x) isa _to_eltype(T, typeof(x))`. However, since `convert_eltype` and `_to_eltype` use different routines, it's possible that the equality doesn't hold for some types. Please submit an issue or PR if that happens.
1313

14-
If `typeof(x)` is not in Base or stdlib, the package who owns the type should implement corresponding `_to_eltype` or `elconvert`. `elconvert` has fallbacks, in which case it could be unnecessary:
15-
- For a subtype of `AbstractArray`, `elconvert` calls the constructor `AbstractArray{T}` and `_to_eltype` returns `Array`.
16-
- For a subtype of `AbstractUnitRange`, `elconvert` calls the constructor `AbstractUnitRange{T}`.
17-
- For a subtype of `AbstractRange`, `elconvert` uses broadcast through `map`.
18-
- For a `Tuple`, `elconvert` uses dot broadcast.
19-
- For other types, `elconvert` calls `convert` and `_to_eltype`.
14+
If `typeof(x)` is not in Base or stdlib, the package who owns the type should implement corresponding `_to_eltype` or `convert_eltype`. `convert_eltype` has fallbacks, in which case it could be unnecessary:
15+
- For a subtype of `AbstractArray`, `convert_eltype` calls the constructor `AbstractArray{T}` and `_to_eltype` returns `Array`.
16+
- For a subtype of `AbstractUnitRange`, `convert_eltype` calls the constructor `AbstractUnitRange{T}`.
17+
- For a subtype of `AbstractRange`, `convert_eltype` uses broadcast through `map`.
18+
- For a `Tuple`, `convert_eltype` uses dot broadcast.
19+
- For other types, `convert_eltype` calls `convert` and `_to_eltype`.
2020

21-
However, `_to_eltype` must be implemented for each type to support `baseconvert` and `precisionconvert`. The following types from Base and stdlib are explicitly supported by `_to_eltype`:
21+
However, `_to_eltype` must be implemented for each type to support `convert_basetype` and `convert_precisiontype`. The following types from Base and stdlib are explicitly supported by `_to_eltype`:
2222
```
2323
AbstractArray, AbstractDict, AbstractSet, Adjoint, Bidiagonal, BitArray, CartesianIndices, Diagonal, Dict, Hermitian, Set, StepRangeLen, Symmetric, SymTridiagonal, Transpose, TwicePrecision, UnitRange
2424
```
@@ -37,23 +37,23 @@ precisiontype(Set{Matrix{Vector{Matrix{Complex{Rational{Int}}}}}})
3737
- `sometype(T)` gets the `sometype` of type `T`.
3838
- `sometype(x) = sometype(typeof(x))` is also provided for convenience.
3939
- `_to_sometype(T,S)` converts the type `S` to have the `sometype` of `T`.
40-
- `someconvert(T,A)` converts `A` to have the `sometype` of `T`.
40+
- `convert_sometype(T,A)` converts `A` to have the `sometype` of `T`.
4141

4242
where `some` can be `el`, `base` and `precision`.
4343

44-
### On `precisionconvert`
45-
`precisionconvert` accepts an optional third argument `prec`.
44+
### On `convert_precisiontype`
45+
`convert_precisiontype` accepts an optional third argument `prec`.
4646
- When `T` has static precision, `prec` has no effect.
47-
- When `T` has dynamic precision, `prec` specifies the precision of conversion. When `prec` is not provided, the precision is decided by the external setup from `T`. The difference is significant when `precisionconvert` is called by another function:
47+
- When `T` has dynamic precision, `prec` specifies the precision of conversion. When `prec` is not provided, the precision is decided by the external setup from `T`. The difference is significant when `convert_precisiontype` is called by another function:
4848
```@repl 1
4949
precision(BigFloat)
50-
f(x) = precisionconvert(BigFloat, x, 256)
51-
g(x) = precisionconvert(BigFloat, x)
50+
f(x) = convert_precisiontype(BigFloat, x, 256)
51+
g(x) = convert_precisiontype(BigFloat, x)
5252
setprecision(128)
5353
f(π) # static precision
5454
g(π) # precision varies with the global setting
5555
```
5656
- When `T` is an integer, the conversion will dig into `Rational` as well. In contrast, since `Rational` as a whole is more "precise" than an integer, `precisiontype` doesn't unwrap `Rational`.
5757
```@repl 1
58-
precisiontype(precisionconvert(Int128, Int8(1)//Int8(2)))
58+
precisiontype(convert_precisiontype(Int128, Int8(1)//Int8(2)))
5959
```

src/EltypeExtensions.jl

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,52 @@ import Base: convert, TwicePrecision
44
using LinearAlgebra # to support 1.0, not using package extensions
55
import LinearAlgebra: AbstractQ
66

7-
export elconvert, basetype, baseconvert, precisiontype, precisionconvert
7+
export convert_eltype, basetype, convert_basetype, precisiontype, convert_precisiontype
88

99
@static if VERSION >= v"1.3"
1010
_to_eltype(::Type{T}, ::Type{UpperHessenberg{S,M}}) where {T,S,M} = UpperHessenberg{T,_to_eltype(T,M)}
11-
elconvert(::Type{T}, A::UpperHessenberg{S,M}) where {T,S,M} = UpperHessenberg{T,_to_eltype(T,M)}(A)
11+
convert_eltype(::Type{T}, A::UpperHessenberg{S,M}) where {T,S,M} = UpperHessenberg{T,_to_eltype(T,M)}(A)
1212
end
1313
@static if VERSION >= v"1.5"
1414
@inline bigfloatconvert(x, prec) = BigFloat(x, precision = prec)
1515
else
1616
@inline bigfloatconvert(x, prec) = BigFloat(x, prec)
1717
end
1818
@static if VERSION >= v"1.9"
19-
elconvert(::Type{T}, A::S) where {T,S<:Bidiagonal} = convert(_to_eltype(T, S), A)
19+
convert_eltype(::Type{T}, A::S) where {T,S<:Bidiagonal} = convert(_to_eltype(T, S), A)
2020
else
21-
elconvert(::Type{T}, A::Bidiagonal{S,V}) where {T,S,V} = Bidiagonal{T,_to_eltype(T,V)}(A.dv, A.ev, A.uplo)
21+
convert_eltype(::Type{T}, A::Bidiagonal{S,V}) where {T,S,V} = Bidiagonal{T,_to_eltype(T,V)}(A.dv, A.ev, A.uplo)
2222
end
2323
@static if VERSION >= v"1.10"
24-
elconvert(::Type{T}, A::AbstractQ) where T = convert(AbstractQ{T}, A) # see https://github.com/JuliaLang/julia/pull/46196
24+
convert_eltype(::Type{T}, A::AbstractQ) where T = convert(AbstractQ{T}, A) # see https://github.com/JuliaLang/julia/pull/46196
2525
end
2626

2727
"""
28-
elconvert(T, A)
28+
convert_eltype(T, A)
2929
3030
Similar to `convert(T, A)`, but `T` refers to the eltype. See also [`_to_eltype`](@ref).
3131
3232
# Examples
33-
```jldoctest; setup = :(using EltypeExtensions: elconvert)
34-
julia> elconvert(Float64, 1:10)
33+
```jldoctest; setup = :(using EltypeExtensions: convert_eltype)
34+
julia> convert_eltype(Float64, 1:10)
3535
1.0:1.0:10.0
3636
37-
julia> typeof(elconvert(Float64, rand(Int, 3, 3)))
37+
julia> typeof(convert_eltype(Float64, rand(Int, 3, 3)))
3838
$(repr("text/plain", Matrix{Float64}))
3939
```
4040
"""
41-
elconvert(::Type{T}, A::S) where {T,S} = convert(_to_eltype(T, S), A)
42-
elconvert(::Type{T}, A::AbstractArray) where T = convert(AbstractArray{T}, A)
43-
elconvert(::Type{T}, A::AbstractRange) where T = map(T, A)
44-
elconvert(::Type{T}, A::AbstractUnitRange) where T<:Integer = convert(AbstractUnitRange{T}, A)
45-
elconvert(::Type{T}, A::Tuple) where T = convert.(T, A)
46-
elconvert(::Type{T}, A::Set{T}) where T = A
47-
elconvert(::Type{T}, A::Set) where T = Set(convert.(T, A))
41+
convert_eltype(::Type{T}, A::S) where {T,S} = convert(_to_eltype(T, S), A)
42+
convert_eltype(::Type{T}, A::AbstractArray) where T = convert(AbstractArray{T}, A)
43+
convert_eltype(::Type{T}, A::AbstractRange) where T = map(T, A)
44+
convert_eltype(::Type{T}, A::AbstractUnitRange) where T<:Integer = convert(AbstractUnitRange{T}, A)
45+
convert_eltype(::Type{T}, A::Tuple) where T = convert.(T, A)
46+
convert_eltype(::Type{T}, A::Set{T}) where T = A
47+
convert_eltype(::Type{T}, A::Set) where T = Set(convert.(T, A))
4848

4949
"""
5050
_to_eltype(T, S)
5151
52-
Convert type `S` to have the `eltype` of `T`. See also [`elconvert`](@ref).
52+
Convert type `S` to have the `eltype` of `T`. See also [`convert_eltype`](@ref).
5353
"""
5454
_to_eltype(::Type{T}, ::Type{S}) where {T,S} = eltype(S) == S ? T : eltype(S) == T ? S : MethodError(_to_eltype, T, S)
5555
_to_eltype(::Type{T}, ::Type{<:AbstractArray{S,N}}) where {T,S,N} = AbstractArray{T,N}
@@ -74,7 +74,7 @@ for TYP in (Adjoint, Diagonal, Hermitian, Symmetric, SymTridiagonal, Transpose)
7474
@eval _to_eltype(::Type{T}, ::Type{$TYP}) where T = $TYP{T}
7575
@eval _to_eltype(::Type{T}, ::Type{$TYP{S}}) where {T,S} = $TYP{T}
7676
@eval _to_eltype(::Type{T}, ::Type{$TYP{S,M}}) where {T,S,M} = $TYP{T,_to_eltype(T,M)}
77-
@eval elconvert(::Type{T}, A::S) where {T,S<:$TYP} = convert(_to_eltype(T, S), A)
77+
@eval convert_eltype(::Type{T}, A::S) where {T,S<:$TYP} = convert(_to_eltype(T, S), A)
7878
end
7979

8080
@static if VERSION >= v"1.6"
@@ -86,7 +86,6 @@ _to_eltype(::Type{T}, ::Type{<:CartesianIndices}) where T = Array{T}
8686

8787
@static if VERSION >= v"1.7"
8888
_to_eltype(::Type{T}, ::Type{<:StepRangeLen}) where T<:Real = StepRangeLen{T,_to_eltype(T,TwicePrecision),_to_eltype(T,TwicePrecision),Int}
89-
9089
else
9190
_to_eltype(::Type{T}, ::Type{<:StepRangeLen}) where T<:Real = StepRangeLen{T,_to_eltype(T,TwicePrecision),_to_eltype(T,TwicePrecision)}
9291
end
@@ -124,11 +123,11 @@ Convert type `S` to have the [`basetype`](@ref) of `T`.
124123
_to_basetype(::Type{T}, ::Type{S}) where {T,S} = eltype(S) == S ? T : _to_eltype(_to_basetype(T, eltype(S)), S)
125124

126125
"""
127-
baseconvert(T::Type, A)
126+
convert_basetype(T::Type, A)
128127
129128
Similar to `convert(T, A)`, but `T` refers to the [`basetype`](@ref).
130129
"""
131-
baseconvert(::Type{T}, A::S) where {T,S} = convert(_to_basetype(T,S), A)
130+
convert_basetype(::Type{T}, A::S) where {T,S} = convert(_to_basetype(T,S), A)
132131

133132
"""
134133
precisiontype(T::Type)
@@ -172,30 +171,32 @@ _to_precisiontype(::Type{T}, ::Type{<:Rational}) where T<:Integer = Rational{T}
172171
_to_precisiontype(::Type{T}, ::Type{S}) where {T,S} = eltype(S) == S ? T : _to_eltype(_to_precisiontype(T, eltype(S)), S)
173172

174173
"""
175-
precisionconvert(T::Type, A, prec)
174+
convert_precisiontype(T::Type, A, prec)
176175
177176
Convert `A` to have the [`precisiontype`](@ref) of `T`. `prec` is optional.
178177
- When `T` has static precision (e.g. `Float64`), `prec` has no effect.
179178
- When `T` has dynamic precision (e.g. `BigFloat`), `prec` specifies the precision of conversion. When `prec` is not provided, the precision is decided by the external setup from `T`.
180179
- When `T` is an integer, the conversion will dig into `Rational` as well. In contrast, since `Rational` as a whole is more "precise" than an integer, [`precisiontype`](@ref) doesn't unwrap `Rational`.
181180
182181
# Examples
183-
```jldoctest; setup = :(using EltypeExtensions: precisionconvert)
184-
julia> precisionconvert(BigFloat, 1//3+im, 128)
182+
```jldoctest; setup = :(using EltypeExtensions: convert_precisiontype)
183+
julia> convert_precisiontype(BigFloat, 1//3+im, 128)
185184
$(repr(bigfloatconvert(1//3, 128))) + 1.0im
186185
187-
julia> precisionconvert(Float16, [[m/n for n in 1:3] for m in 1:3])
186+
julia> convert_precisiontype(Float16, [[m/n for n in 1:3] for m in 1:3])
188187
3-element $(repr(Vector{Vector{Float16}})):
189188
[1.0, 0.5, 0.3333]
190189
[2.0, 1.0, 0.6665]
191190
[3.0, 1.5, 1.0]
192191
```
193192
"""
194-
precisionconvert(::Type{T}, A::S) where {T,S} = convert(_to_precisiontype(T,S), A)
195-
precisionconvert(::Type{T}, A::S, prec) where {T,S} = precisionconvert(T, A)
196-
precisionconvert(::Type{BigFloat}, A::S) where {S} = convert(_to_precisiontype(BigFloat,S), A)
197-
precisionconvert(::Type{BigFloat}, x::Real, prec) = bigfloatconvert(x, prec)
198-
precisionconvert(::Type{BigFloat}, x::Complex, prec) = Complex(bigfloatconvert(real(x), prec), bigfloatconvert(imag(x), prec))
199-
precisionconvert(::Type{BigFloat}, A, prec) = precisionconvert.(BigFloat, A, prec)
193+
convert_precisiontype(::Type{T}, A::S) where {T,S} = convert(_to_precisiontype(T,S), A)
194+
convert_precisiontype(::Type{T}, A::S, prec) where {T,S} = convert_precisiontype(T, A)
195+
convert_precisiontype(::Type{BigFloat}, A::S) where {S} = convert(_to_precisiontype(BigFloat,S), A)
196+
convert_precisiontype(::Type{BigFloat}, x::Real, prec) = bigfloatconvert(x, prec)
197+
convert_precisiontype(::Type{BigFloat}, x::Complex, prec) = Complex(bigfloatconvert(real(x), prec), bigfloatconvert(imag(x), prec))
198+
convert_precisiontype(::Type{BigFloat}, A, prec) = convert_precisiontype.(BigFloat, A, prec)
199+
200+
include("deprecated.jl")
200201

201202
end

src/deprecated.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@deprecate elconvert convert_eltype
2+
@deprecate baseconvert convert_basetype
3+
@deprecate precisionconvert convert_precisiontype

test/runtests.jl

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using Aqua
66
using LinearAlgebra
77

88
function testelconvert(T, A)
9-
@test elconvert(T, A) isa _to_eltype(T, typeof(A))
9+
@test convert_eltype(T, A) isa _to_eltype(T, typeof(A))
1010
end
1111

1212
@testset "elconvert" begin
@@ -40,40 +40,40 @@ end
4040

4141
@testset "bugs" begin
4242
@test _to_precisiontype(Float64, Complex) == Complex{Float64}
43-
@test precisionconvert(BigFloat, rand(ComplexF64, 3)) isa Vector{Complex{BigFloat}}
43+
@test convert_precisiontype(BigFloat, rand(ComplexF64, 3)) isa Vector{Complex{BigFloat}}
4444

4545
@testset "#7" begin
4646
setprecision(256)
47-
f(x) = precisionconvert(BigFloat, x, 256)
48-
g(x) = precisionconvert(BigFloat, x)
47+
f(x) = convert_precisiontype(BigFloat, x, 256)
48+
g(x) = convert_precisiontype(BigFloat, x)
4949
setprecision(128)
5050
@test precision(f(π)) == 256 # static precision
5151
@test precision(g(π)) == 128 # precision varies with the global setting
5252
end
5353

5454
@testset "#8" begin
55-
@test precisionconvert(Int128, Int8(1)//Int8(2)) isa Rational{Int128}
55+
@test convert_precisiontype(Int128, Int8(1)//Int8(2)) isa Rational{Int128}
5656
end
5757

5858
@testset "#10" begin
59-
@test elconvert(Int32, 1:5) === Int32(1):Int32(5)
59+
@test convert_eltype(Int32, 1:5) === Int32(1):Int32(5)
6060
end
6161
end
6262

6363
@testset "Misc" begin
6464
@testset "Moved from DomainSets.jl" begin
65-
@test elconvert(Float64, [1,2]) isa Vector{Float64}
66-
@test elconvert(Float64, [1,2]) == [1,2]
67-
@test elconvert(Float64, Set([1,2])) isa Set{Float64}
68-
@test elconvert(Float64, Set([1,2])) == Set([1,2])
69-
@test elconvert(Float64, 1:5) isa AbstractVector{Float64}
70-
@test elconvert(Float64, 1:5) == 1:5
71-
@test elconvert(Float64, 1) isa Float64
72-
@test elconvert(Float64, 1) == 1
65+
@test convert_eltype(Float64, [1,2]) isa Vector{Float64}
66+
@test convert_eltype(Float64, [1,2]) == [1,2]
67+
@test convert_eltype(Float64, Set([1,2])) isa Set{Float64}
68+
@test convert_eltype(Float64, Set([1,2])) == Set([1,2])
69+
@test convert_eltype(Float64, 1:5) isa AbstractVector{Float64}
70+
@test convert_eltype(Float64, 1:5) == 1:5
71+
@test convert_eltype(Float64, 1) isa Float64
72+
@test convert_eltype(Float64, 1) == 1
7373

74-
@test elconvert(Float64, Set([1,2])) isa Set{Float64}
75-
@test elconvert(Float64, (1,2)) isa NTuple{2,Float64}
76-
@test elconvert(Int, (1,2)) == (1,2)
74+
@test convert_eltype(Float64, Set([1,2])) isa Set{Float64}
75+
@test convert_eltype(Float64, (1,2)) isa NTuple{2,Float64}
76+
@test convert_eltype(Int, (1,2)) == (1,2)
7777
end
7878
end
7979

0 commit comments

Comments
 (0)