Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 递归无单元元素类型_Arrays_Recursion_Types_Julia - Fatal编程技术网

Arrays 递归无单元元素类型

Arrays 递归无单元元素类型,arrays,recursion,types,julia,Arrays,Recursion,Types,Julia,我试图找到一个函数,它给了我递归的无单位元素类型。例如,为了缩短它,我们称之为ruet,我想: 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{Flo

我试图找到一个函数,它给了我递归的无单位元素类型。例如,为了缩短它,我们称之为
ruet
,我想:

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}
所以基本上去掉了单位,但仍然返回正确的元素类型,可以是数组。阵列部分很难。我可以递归:

recursive_eltype(a) = recursive_eltype(eltype(a))
recursive_eltype{T<:Number}(a::Type{T}) = eltype(a)
但是这总是数字类型,当它是数组数组时,我似乎找不到一个好的方法来获取数组类型,也就是说,在上面的所有示例中,这个方法都返回
Float64
。我想知道这里是否需要在静态阵列上调度并使用
类似的\u类型

请注意,如果可能的话,我希望解决方案对Unitful.jl没有要求。可以通过
one(u)
获得数字的无单位类型,因此我认为这应该是可能的


(与Julia问题有些相关:)

使用Julia版本0.6.2-something(代码肯定不是很容易移植):


问题中的测试通过了。仍然不可推断,但将
eval
替换为
getfield(模块、符号)
。你从哪里得到这些问题的?

我想到了:

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)))
Base.@pure recursive\u unitless\u eltype(a)=recursive\u unitless\u eltype(a))

Base.@pure recursive_unitless_eltype{TI几乎可以肯定你不想要这样的东西:
reut(A)=eval(parse(repr(eltype(A)),repr(recursive_eltype(A)),repr(recursive_eltype(A))
,但谁知道呢?:)我不想要评估。我希望这是可以推断的。“你从哪里得到这些问题?”开发是一个关于通用算法的问题的无限源。这一个。如果你添加
基,这个解决方案可以正确地推断。@pure
成为函数。太好了!这可以在v0.7/1.0上检查吗?另一个进行推断的方法可能就是将它变成一个
@生成的
函数。嗯,这不是我想要的贝卡使用它似乎需要依赖Unitful.jl,而不是像
one
oneunit
这样的基本工具来获取无单元类型。
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))
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)))