Mathematica Partition在Julia中的功能是什么?
Mathematica Partition[list,n]采用一个数组并将其分成不重叠的长度子列表n.另一方面,Julia中的分区函数接受一个数组,并将该数组的所有分区都放入n子集中.
在我尝试练习朱莉娅时,我制作了一个绘制分叉图的程序.我的代码如下:
function bifur(x0,y0,a=1.3,b=0.4,n=1000,m=10000)
i,x,y=1,x0,y0
while i < n && abs(x) < m
x,y = a - x^2 + y, b * x
i += 1
end
if abs(x) < m
return x
else
return 1000
end
end
la = Float64[];
lx = Float64[];
for a=0:400
for j = 1:1000
x0 = rand()
y0 = rand()
x = bifur(x0,y0,a/100)
if x != 1000
push!(la,a/100)
push!(lx,x)
end
end
end
using Gadfly
myplot = Gadfly.plot( x=la, y=lx , Scale.x_discrete, Scale.y_continuous, Geom.point)
draw(PNG("myplot.png",10inch,8inch),myplot)
Run Code Online (Sandbox Code Playgroud)
我得到的输出是这个图像: …
我试图通过重复Julia中一些简单的ProjectEuler问题来学习Julia.到目前为止,一切都非常顺利,直到我遇到这个令人沮丧的问题.我花了一些时间调试我的代码,这就是我发现的:(希望我不会错过这里真正愚蠢的东西)
function is_abundant(n::Int) #just a function
return prod(map(x->int((x[1]^(x[2]+1)-1)/(x[1]-1)),factor(n))) > 2 * n
end
abundants=[12] #there should be a better way to initialize an Array
for i=13:28120
if is_abundant(i)
push!(abundants,i)
end
end
le=abundants; #The following lines are the problems
ri=abundants;
d=length(abundants)
println(d)
pop!(le)
shift!(ri)
println(le==ri, " ", endof(ri), " ", endof(abundants))
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:
6964
true 6962 6962
这意味着朱莉娅改变了所有三组le,ri并且abundants每个pop!和shift!命令都改变了.我能够通过使用一个愚蠢的额外身份映射来解决这个bug /问题:
le=map(x->x,abundants)
ri=map(x->x,abundants)
Run Code Online (Sandbox Code Playgroud)
现在输出会改变到我最初的预期:
6964
false 6963 6964
我的问题是,如果这不是一个错误,为什么朱莉娅保持一个等价关系le,ri并abundants …