2012-11-15 5 views
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 

EDIT :

코드가 각 인덱스 pp의 하나 또는 두 배의 곱의 값을 합산하여 1~3 인덱스와 순위 -9- 텐서를 생성 값에 따라되고 ind_1ind_2이있다.

편집 : 여기

는 차원 예입니다, 명심하지만 pp의 인덱스 단순히 9D 버전에서 유지되지 않습니다 순열 있다는 사실이 :

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 
+0

이 코드가 수행하는 작업에 대해 조금 더 자세히 설명하고 의견을 추가 할 수 있습니까? – igon

+0

@igon : 편집했습니다. – erbridge

+0

설명하기 위해 2D 또는 3D 예제를 만들 수 있습니까? 우리가 함께 일하는 것이 더 쉬울 것이고, 예제를 작성하면 스스로 전략을 이해하는 데 도움이 될 것입니다. – tmpearce

답변

5

와우! 아주 간단한 해결책 이었지만 쉽게 찾을 수 없었습니다. 그건 그렇고 당신의 공식이 어디에서 오는 것인지 궁금합니다.

일시적으로 약간의 메모리 (4^9 어레이와 2^4^9 어레이 대 3^9)를 잃어도 상관이 없다면 맨 마지막에 3 번째 및 4 번째 초평면 누적을 연기 할 수 있습니다. 유닉스 상자와 옥타브 3.2.4

테스트 그것은 23S (67MB) 0.17s (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) ; 
+0

우수. 감사. – erbridge

관련 문제