

将来的に並列計算を行うために、以前質問させていただいたコード中のfor文3つを３つの関数に分けてもう少しきれいなコードにしたく、取り組んでいますが、以前質問したような欲しい結果がうまくでません。おそらくは初期値を代入するときに変なことをしている。。？  
pythonでもそうだったのですが,簡単なdef関数やfunction関数は理解しているのですが、自分で複雑な関数を作ろうとするといつもつまずいてしまい、結局一つの文に収めてしまう傾向にあります。  
並列計算をするためにもここで克服したいと思っています。  
以下にコードをあげます。  
元のコードです。

[code]

    function main()
            n = 3
            count0=0
            count1=0
            count2=0
            count3=0
            count4=0
            count5=0
            count6=0
            count7=0
            stage = zeros(Int8,n,n,n)
            stage[:,:,1]=[1 1 1;1 0 1;1 1 1]
            stage[:,:,2]=[1 0 1;0 0 0;1 0 1]
            stage[:,:,3]=[1 1 1;1 0 1;1 1 1]
            #println(walk)
            for itr in 1:10
                walk = zeros(Int8,n,n,n)
                walk[1,1,1]=1
                for t in 0:6
                    next_walk = zeros(Int8,n,n,n)
                    number=rand(UnitRange{Int8}(1:6))
                    if t==0
                        count0 += 1
                    else
                        for x = 1:n, y = 1:n, z =1:n
                            x1 = ((x-1 + (n-1)) %n) + 1
                            x2 = ((x+1 + (n-1)) %n) + 1
                            y1 = ((y-1 + (n-1)) %n) + 1
                            y2 = ((y+1 + (n-1)) %n) + 1
                            z1 = ((z-1 + (n-1)) %n) + 1 
                            z2 = ((z+1 + (n-1)) %n) + 1
                            if stage[x,y,z]== 0
                                continue
                            else
                                if walk[x,y,z]== 0
                                    continue
                                else
                                    if number == 1
                                        if stage[x2,y,z]==1
                                            next_walk[x2,y,z]=walk[x,y,z]
                                        else
                                            next_walk[x,y,z]=walk[x,y,z]
                                        end
                                    elseif number == 2
                                        if stage[x,y2,z]==1
                                            next_walk[x,y2,z]=walk[x,y,z]
                                        else
                                            next_walk[x,y,z]=walk[x,y,z]
                                        end
                                    elseif number == 3
                                        if stage[x,y,z2]==1
                                            next_walk[x,y,z2]=walk[x,y,z]
                                        else
                                            next_walk[x,y,z]=walk[x,y,z]
                                        end
                                    elseif number == 4
                                        if stage[x1,y,z]==1
                                            next_walk[x1,y,z]=walk[x,y,z]
                                        else
                                            next_walk[x,y,z]=walk[x,y,z]
                                        end
                                    elseif number == 5
                                        if stage[x,y1,z]==1
                                            next_walk[x,y1,z]=walk[x,y,z]
                                        else
                                            next_walk[x,y,z]=walk[x,y,z]
                                        end
                                    elseif number == 6
                                        if stage[x,y,z1]==1
                                            next_walk[x,y,z1]=walk[x,y,z]
                                        else
                                            next_walk[x,y,z]=walk[x,y,z]
                                        end
                                    end
                                end
                            end
                        end
                    　　walk = copy(next_walk)
                    #println(t,walk,"\n")
                    if t == 6
                        if walk[1,1,1]==1
                            count1 += 1
                        else
                            continue
                        end
                    elseif t == 36
                        if walk[1,1,1]==1
                            count2 += 1
                        else
                            continue
                        end
                    elseif t == 216
                        if walk[1,1,1]==1
                            count3 += 1
                        else
                            continue
                        end
                    else
                        continue
                            #1296,7776,46656
                    end
                    end
                end
            #println("t=0:",count0,"\n","t=6:",count1,"\n","t=36:",count2,"\n","t=216:",count3,"\n","t=1296:",count4,"\n","t=7776:",count5,"\n","t=46656:",count6,"\n")
            end
            println("t=0:",count0,"\n","t=6:",count1,"\n","t=36:",count2,"\n","t=216:",count3,"\n","t=1296:",count4,"\n","t=7776:",count5,"\n","t=46656:",count6,"\n")
        end
        main()
    
[/code]

改良中のコードです（上を参考にしています）。

[code]

     n=3
        stage1 = zeros(Int8,3,3,3)
        stage1[:,:,1]=[1 1 1;1 0 1;1 1 1]
        stage1[:,:,2]=[1 0 1;0 0 0;1 0 1]
        stage1[:,:,3]=[1 1 1;1 0 1;1 1 1]
        Stage = copy(stage1)
    function random(stage,number,walk,next_walk)#すべての座標でランダムウォーク
        for x = 1:n, y = 1:n, z =1:n
            x1 = ((x-1 + (n-1)) %n) + 1
            x2 = ((x+1 + (n-1)) %n) + 1
            y1 = ((y-1 + (n-1)) %n) + 1
            y2 = ((y+1 + (n-1)) %n) + 1
            z1 = ((z-1 + (n-1)) %n) + 1 
            z2 = ((z+1 + (n-1)) %n) + 1
            if stage[x,y,z]== 0
                continue
            else
                if walk[x,y,z]== 0
                    continue
                else
                    if number == 1
                        if stage[x2,y,z]==1
                            next_walk[x2,y,z]=walk[x,y,z]
                        else
                            next_walk[x,y,z]=walk[x,y,z]
                        end
                    elseif number == 2
                        if stage[x,y2,z]==1
                            next_walk[x,y2,z]=walk[x,y,z]
                        else
                            next_walk[x,y,z]=walk[x,y,z]
                        end
                    elseif number == 3
                        if stage[x,y,z2]==1
                            next_walk[x,y,z2]=walk[x,y,z]
                        else
                            next_walk[x,y,z]=walk[x,y,z]
                        end
                    elseif number == 4
                        if stage[x1,y,z]==1
                            next_walk[x1,y,z]=walk[x,y,z]
                        else
                            next_walk[x,y,z]=walk[x,y,z]
                        end
                    elseif number == 5
                        if stage[x,y1,z]==1
                            next_walk[x,y1,z]=walk[x,y,z]
                        else
                            next_walk[x,y,z]=walk[x,y,z]
                        end
                    elseif number == 6
                        if stage[x,y,z1]==1
                            next_walk[x,y,z1]=walk[x,y,z]
                        else
                            next_walk[x,y,z]=walk[x,y,z]
                        end
                    end
                end
            end
        end
        walk = copy(next_walk)
        return walk
    end
    
    function step_walk(time) #上の関数をt秒間繰り返し、t=xのときの原点にいたら１をたしていき、ｔ秒後の各countを返したい
        walk = zeros(Int8,n,n,n)
        walk[1,1,1]=1
        direction=rand(UnitRange{Int8}(1:6))
        count0 = 0
        count1 = 0
        count2 = 0
        for t in 0:time
            if t== 0
                count0 += 1
            else
                next_walk=zeros(Int8,n,n,n)#毎秒初期化
                walker = random(Stage,direction,walk,next_walk)
                if t==2 && walker[1,1,1]==1
                    count1 += 1
                elseif t==4 && walker[1,1,1]==1
                    count2 += 1
                else
                    continue
                end
            end
        end
        return count0,count1,count2
    end
    #show(step_walk(10))#この時点でおかしい
    
    function experiment(time,itr) #上のｔ秒後のランダムウォークの結果をitr回繰り返し、最終的な各countを返したい。
        for i in 0:itr
            walk = zeros(Int,n,n,n)#itr毎に初期化
            step_walk(time)
        end
        return step_walk(time)
    end 
    experiment(10,10)
    
[/code]

結果

[code]

    (1,0,0)#少なくとも(10,?,?)とでるはず。
    
[/code]

今、現在進行形で勉強中ですが、defやfunction関数が分かっているようでわかっていないこのもどかしさをどうにかしたい、、、、。  
宜しくお願いします。


CC BY-SA 4.0
2018-11-29T17:11:38.890
50765
2018-11-29T17:11:38.890
29111
question
python random julia
function関数を用いたプログラミングに苦戦