如何将嵌套循环向量化

9

我不太清楚如何将这组循环向量化。希望能得到指导。

ind_1 = [1,2,3];
ind_2 = [1,2,4];
K = zeros(3,3,3,3,3,3,3,3,3);
pp = rand(4,4,4);

for s = 1:3
 for t = 1:3
  for k = 1:3
   for l = 1:3
    for m = 1:3
     for n = 1:3
      for o = 1:3
       for p = 1:3
        for r = 1:3
         % the following loops are singular valued except when
         % y=3 for ind_x(y) in this case
         for a_s = ind_1(s):ind_2(s)
          for a_t = ind_1(t):ind_2(t)
           for a_k = ind_1(k):ind_2(k)
            for a_l = ind_1(l):ind_2(l)
             for a_m = ind_1(m):ind_2(m)
              for a_n = ind_1(n):ind_2(n)
               for a_o = ind_1(o):ind_2(o)
                for a_p = ind_1(p):ind_2(p)
                 for a_r = ind_1(r):ind_2(r)
                  K(s,t,k,l,m,n,o,p,r) = K(s,t,k,l,m,n,o,p,r) + ...
                    pp(a_s, a_t, a_r) * pp(a_k, a_l, a_r) * ...
                    pp(a_n, a_m, a_s) * pp(a_o, a_n, a_t) * ...
                    pp(a_p, a_o, a_k) * pp(a_m, a_p, a_l);
                 end
                end
               end
              end
             end
            end
           end
          end
         end
        end
       end
      end
     end
    end
   end
  end
 end
end

编辑:

该代码通过对每个索引的一个或两次pp乘积求和,创建一个从1到3的等级为9的张量,具体取决于ind_1ind_2的值。

编辑:

以下是一个三维示例,但请注意,在9d版本中不保留pp的索引仅仅是置换的事实:

ind_1 = [1,2,3];
ind_2 = [1,2,4];
K = zeros(3,3,3);
pp = rand(4,4,4);

for s = 1:3
 for t = 1:3
  for k = 1:3
   % the following loops are singular valued except when
   % y=3 for ind_x(y) in this case
   for a_s = ind_1(s):ind_2(s)
    for a_t = ind_1(t):ind_2(t)
     for a_k = ind_1(k):ind_2(k)
      K(s,t,k) = K(s,t,k) + ...
        pp(a_s, a_t, a_r) * pp(a_t, a_s, a_k) * ...
        pp(a_k, a_t, a_s) * pp(a_k, a_s, a_t);
     end
    end
   end
  end
 end
end

你能否详细说明一下这段代码是在做什么并添加注释? - igon
你能否创建一个2D或3D的示例来说明吗?这样我们更容易处理,而且创建示例可能会帮助你自己找出一种策略。 - tmpearce
@tmpearce:编辑后提供了一个三维示例。 - erbridge
8
这是一碗令人印象深刻的字母果环汤。 - dinkelk
1
你有没有研究过内置函数:http://www.mathworks.nl/help/matlab/ref/kron.html?如果可以使用这个函数,我怀疑其他任何东西都不会表现得更好。 - Dennis Jaheruddin
显示剩余3条评论
1个回答

5

哇!这个解决方案相当简单,但是很难找到。顺便问一下,你的公式从哪里来的?

如果您不介意暂时失去一些内存(2次4^9阵列与之前的3^9相比),您可以在最后推迟第三个和第四个超平面的累加。

在Unix系统上使用Octave 3.2.4进行测试,计算时间从23秒(67MB)降至0.17秒(98MB)

function K = tensor9_opt(pp)

  ppp = repmat(pp, [1 1 1 4 4 4 4 4 4]) ;
  % The 3 first numbers are variable indices (eg 1 for a_s to 9 for a_r)
  % Other numbers must complete 1:9 indices in any order
  T = ipermute(ppp, [1 2 9 3 4 5 6 7 8]) .* ...
      ipermute(ppp, [3 4 9 1 2 5 6 7 8]) .* ...
      ipermute(ppp, [6 5 1 2 3 4 7 8 9]) .* ...
      ipermute(ppp, [7 6 2 1 3 4 5 8 9]) .* ...
      ipermute(ppp, [8 7 3 1 2 4 5 6 9]) .* ...
      ipermute(ppp, [5 8 4 1 2 3 6 7 9]) ;

  % I have not found how to manipulate 'multi-ranges' programmatically. 
  T1 = T (:,:,:,:,:,:,:,:,1:end-1) ; T1(:,:,:,:,:,:,:,:,end) += T (:,:,:,:,:,:,:,:,end) ;
  T  = T1(:,:,:,:,:,:,:,1:end-1,:) ; T (:,:,:,:,:,:,:,end,:) += T1(:,:,:,:,:,:,:,end,:) ;
  T1 = T (:,:,:,:,:,:,1:end-1,:,:) ; T1(:,:,:,:,:,:,end,:,:) += T (:,:,:,:,:,:,end,:,:) ;
  T  = T1(:,:,:,:,:,1:end-1,:,:,:) ; T (:,:,:,:,:,end,:,:,:) += T1(:,:,:,:,:,end,:,:,:) ;
  T1 = T (:,:,:,:,1:end-1,:,:,:,:) ; T1(:,:,:,:,end,:,:,:,:) += T (:,:,:,:,end,:,:,:,:) ;
  T  = T1(:,:,:,1:end-1,:,:,:,:,:) ; T (:,:,:,end,:,:,:,:,:) += T1(:,:,:,end,:,:,:,:,:) ;
  T1 = T (:,:,1:end-1,:,:,:,:,:,:) ; T1(:,:,end,:,:,:,:,:,:) += T (:,:,end,:,:,:,:,:,:) ;
  T  = T1(:,1:end-1,:,:,:,:,:,:,:) ; T (:,end,:,:,:,:,:,:,:) += T1(:,end,:,:,:,:,:,:,:) ;
  K  = T (1:end-1,:,:,:,:,:,:,:,:) ; K (end,:,:,:,:,:,:,:,:) += T (end,:,:,:,:,:,:,:,:) ;
endfunction

pp = rand(4,4,4);
K = tensor9_opt(pp) ;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接