gpt4 book ai didi

variables - 如何在 julia 中将变量类型 "SentinelArrays"转换为 "Arrays{Float64,n}"

转载 作者:行者123 更新时间:2023-12-04 08:13:24 27 4
gpt4 key购买 nike

我试图通过使其类型稳定来优化我的 Julia 代码。因此,我尝试在函数头中声明变量类型。但是其中一个变量的类型是 ::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
我尝试通过 typeof(a)function calc(a::SentinelArrays.ChainedVector{Float64,Array{Float64,1}}, b::Arrays{Float64,2})但是,这会引发 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 calc(a, b::AbstractArray{T,2}) where T
因为这适用于这两种类型:
julia> SentinelMatrix{Int64, Int64, Missing, Matrix{Int64}} <: AbstractArray{T,2} where T
true

关于variables - 如何在 julia 中将变量类型 "SentinelArrays"转换为 "Arrays{Float64,n}",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65837433/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com