Type of recursive dimensionless element

I am trying to come up with a function that gives me a recursive type without a single element. So, for example, to shorten it, call him ruet, I would like:

A = zeros(5,5)
reut(A) == Float64
using Unitful
A = zeros(5,5)*1u"kg"
reut(A) == Float64
AA = [zeros(5,5) for i in 1:5]
reut(AA) == Array{Float64,2}
AofA = [copy(A) for i in 1:5]
reut(AofA) == Array{Float64,2}
using StaticArrays
AofSA = [@SVector [2.0,3.0] for i in 1:5]
reut(AofSA) == SVector{2,Float64}
AofuSA = [@SVector [2.0u"kg",3.0u"kg"] for i in 1:5]
reut(AofuSA) == SVector{2,Float64}

So basically separate the blocks, but return the correct element type, which could be an array. This is an array that is heavy. I can recurs:

recursive_eltype(a) = recursive_eltype(eltype(a))
recursive_eltype{T<:Number}(a::Type{T}) = eltype(a)

and then get the type without a single element:

uEltype = recursive_eltype(u)
uEltypeNoUnits = typeof(one(uEltype))

but then it is always a type of number, and I cannot find a good way to return array types when it is an array of arrays, i.e. this method returns Float64in all the examples above. I am wondering if sending to static arrays is required and using similar_typehere.

, , , , Unitful.jl. one(u), , .

( : https://github.com/JuliaLang/julia/issues/22216)

+3
2

:

Base.@pure recursive_unitless_eltype(a) = recursive_unitless_eltype(eltype(a))
Base.@pure recursive_unitless_eltype{T<:StaticArray}(a::Type{T}) = similar_type(a,recursive_unitless_eltype(eltype(a)))
Base.@pure recursive_unitless_eltype{T<:Array}(a::Type{T}) = Array{recursive_unitless_eltype(eltype(a)),ndims(a)}
Base.@pure recursive_unitless_eltype{T<:Number}(a::Type{T}) = typeof(one(eltype(a)))

, .

+2

Julia 0.6.2-something (, , ):

function _reut(T)
    try
        T.name == Quantity.body.body.body.name && return _reut(T.parameters[1])
        getfield(T.name.module, T.name.name){_reut.(collect(T.parameters))...}
    catch
        T
    end
end
reut(T) = _reut(eltype(T))

. , eval getfield(Module,Symbol). ?

+1

Source: https://habr.com/ru/post/1690341/


All Articles