Reputation: 2005
I am trying to optimize my Julia code by making it type-stable. Hence, I tried to declare the variable types in the function headers. But one of the variables has a type of ::SentinelArrays.ChainedVector{Float64,Array{Float64,1}}
as shown in the code snippet below.
The code example:
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
I tried passing the typeof(a)
in the function
calc(a::SentinelArrays.ChainedVector{Float64,Array{Float64,1}}, b::Arrays{Float64,2})
however, this throws an error of no method matching
.
May I know the correct way to assign this type or maybe if can convert this to normal Array{Float64,1}
.
Please suggest a solution to this issue. Thanks in advance.
Upvotes: 2
Views: 580
Reputation: 42234
You can just write Array(a)
where a
is your SentinelArray
as here:
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
However, normally you would just make the function signature to be something like:
function calc(a, b::AbstractArray{T,2}) where T
because this would work with both those types:
julia> SentinelMatrix{Int64, Int64, Missing, Matrix{Int64}} <: AbstractArray{T,2} where T
true
Upvotes: 2