作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图通过使其类型稳定来优化我的 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/
我是一名优秀的程序员,十分优秀!