[英]How to convert variable type “SentinelArrays” to “Arrays{Float64,n}” in julia
我正在尝试通过使其类型稳定来优化我的 Julia 代码。 因此,我尝试在 function 标头中声明变量类型。 但是其中一个变量的类型为::SentinelArrays.ChainedVector{Float64,Array{Float64,1}}
,如下面的代码片段所示。
代码示例:
df=CSV.read("text.csv", DataFrame)
a = view(df, :, 1)
#this has a type of ::SentinelArrays.ChainedVector{Float64,Array{Float64,1}}
b = view(df, :, 2:4)
#while type of this is ::Arrays{Float64,2}
#I would like to pass the type of the arrays in the function.
function calc(a, b::Arrays{Float64,2})
a+b
end
我尝试在function
calc(a::SentinelArrays.ChainedVector{Float64,Array{Float64,1}}, b::Arrays{Float64,2})
中传递typeof(a)
) 但是,这会引发no method matching
的错误. 我可以知道分配此类型的正确方法,或者是否可以将其转换为普通Array{Float64,1}
。 请提出解决此问题的方法。 提前致谢。
您可以只写Array(a)
,其中a
是您的SentinelArray
,如下所示:
julia> u = SentinelArray(rand(1:8,4))
4-element SentinelVector{Int64, Int64, Missing, Vector{Int64}}:
2
3
5
3
julia> Array(u)
4-element Vector{Union{Missing, Int64}}:
2
3
5
3
但是,通常您只需将 function 签名设置为:
function calc(a, b::AbstractArray{T,2}) where T
因为这适用于这两种类型:
julia> SentinelMatrix{Int64, Int64, Missing, Matrix{Int64}} <: AbstractArray{T,2} where T
true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.