是否可以在 Julia 中将 Array{Num,1} 转换为 Array{Float64,1} ?

ute*_*o10 3 typed-arrays julia ijulia-notebook

我有以下在 Julia 中使用符号的函数。一切工作正常,直到绘图那一刻

using Distributions
using Plots
using Symbolics
using SymbolicUtils

function BinomialMeasure(iter::Int64, p::Float64, current_level = nothing)

@variables m0 m1

if current_level == nothing
    current_level = [1]
end
next_level = []
for item in current_level
    append!(next_level, m0*item)
    append!(next_level, m1*item)
end

If iter != 0
    current_level = next_level
    return BinomialMeasure(iter - 1, p , current_level)
else
    return [substitute(i, Dict([m0 => p, m1 => 1 - p])) for i in next_level]
end
end

y = BinomialMeasure(10, 0.4)
x = [( i + 1 ) / length(y) for i = 1:length(y) ]
append!(x, 0)
append!(y,0)
plot(x,y) 
Run Code Online (Sandbox Code Playgroud)

然后它返回以下内容:

MethodError: no method matching AbstractFloat(::Num)
Closest candidates are:
AbstractFloat(::Real, !Matched::RoundingMode) where T<:AbstractFloat at rounding.jl:200
AbstractFloat(::T) where T<:Number at boot.jl:716
AbstractFloat(!Matched::Bool) at float.jl:258 
Run Code Online (Sandbox Code Playgroud)

y 是一个 Array{Num,1},x 是一个 Array{Float64,1}。

我尝试了map(float, y)、convert(float,y)和float(y),但我认为不可能将类型Num转换为Float64,或者至少我不知道该怎么做。

Mar*_*ush 5

val您无需使用string和即可访问该字段parse

y_val = [i.val for i in y]
Run Code Online (Sandbox Code Playgroud)

这当然比解析字符串有更好的性能