econ_ugrad
econ_ugrad

Reputation: 263

Fast way to Implement a Loop

I am trying to compute a summation in Julia using the following loop.

  for (k_j,kk) = enumerate(k)
       value=0
       for (s_j,ss) = enumerate(s), (z_j,zz) = enumerate(z), (w_j,ww) = enumerate(w)
                    value=value+V₀[w_j,z_j,k_j,s_j]*H[s_i,s_j]*mat[w_j,w_i,z_j,z_i]*G[z_i,z_j]
        end
  end

Which is basically calling over specific entries of matrices and adding them up. I've tried to make this faster and leaner with a reduce or mapreduce, but haven't been able to get the code off the ground.

Any suggestion is appreciated, Thanks

Upvotes: 2

Views: 117

Answers (1)

econ_ugrad
econ_ugrad

Reputation: 263

I solved it with something like this

     y=gridmake(1:ssize,1:zsize,1:wsize)
      ysize=ssize*zsize*wsize
      for (k_j,kk) = enumerate(k)
           # value=0
           # for (s_j,ss) = enumerate(s), (z_j,zz) = enumerate(z), (w_j,ww) = enumerate(w)
           #              value=value+V₀[w_j,z_j,k_j,s_j]*H[s_i,s_j]*mat[w_j,w_i,z_j,z_i]*G[z_i,z_j]
           #  end
            F[k_j]=(mapreduce(y_i -> V₀[y[y_i,3],y[y_i,2],k_j,y[y_i,1]]*H[s_i,y[y_i,1]]*mat[y[y_i,3],w_i,y[y_i,2],z_i]*G[z_i,y[y_i,2]], + ,1:ysize))
      end

Anyway, I'm open to more efficient suggestions

Upvotes: 2

Related Questions