我有一个在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)
然后它返回以下内容:
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
y是一个Array{Num,1},x是一个Array{Float64,1}。
我尝试过map(float, y),convert(float,y)和float(y),但我认为无法将类型Num转换为Float64,或者至少我不知道该如何做。
convert.(Float64, A)
,其中A
是一个Int64
或Float32
数组等。请注意,在单词convert
后面加上广播点.
。 - PatrickT