简体   繁体   English

具有可变数量循环的嵌套循环

[英]A nested loop with variable number of loops

I have a function that outputs a matrix in a new basis. 我有一个以新的方式输出矩阵的函数。 However, depending on the size of the matrix the number of basis matrix differ. 但是,根据矩阵的大小,基础矩阵的数量不同。 So in simplified "Matlab pseudo code": 所以在简化的“Matlab伪代码”中:

if matrixsize==1
   for a1=1:4
      out(a1)=Matrix*basis(a1)
   end

elseif matrixsize==2
   for a1=1:4
      for a2=a1:4
         out(a1,a2)=Matrix*basis(a1)*basis(a2)
      end
   end

elseif matrixsize==3
   for a1=1:4
      for a2=a1:4
          for a3=a2:4
             out(a1,a2,a3)=Matrix*basis(a1)*basis(a2)*basis(a3)
          end
      end
   end

elseif ...

and so on 等等

Is it possible to write this code, for any value of matrix size? 对于矩阵大小的任何值,是否可以编写此代码? In other words: Is it possible to create a loop that automatically creates the loops above? 换句话说:是否可以创建一个自动创建上述循环的循环? If this does not work in Matlab, is there maybe a solution in Python? 如果这在Matlab中不起作用,那么Python中可能有一个解决方案吗?

(Background: This question comes from quantum physics, where I want to write a quantum state in the Pauli basis) (背景:这个问题来自量子物理学,我想在保利基础上写一个量子态)

Here is a working Matlab code that shows the problem: 这是一个工作的Matlab代码,显示了问题:

function T=newbasis(n)

%create a random matrix
m=2^n;
M=randn(m);

%Pauli matrices
s{1}=sparse([1,0;0,1]);
s{2}=sparse([0,1;1,0]);
s{3}=sparse([0,-1i;1i,0]);
s{4}=sparse([1,0;0,-1]);

if n==1
    for a1=1:4
        T(a1)=trace(M*betterkron(s{a1}));
    end

elseif n==2
    for a1=1:4
        for a2=a1:4
            T(a1,a2)=trace(M*betterkron(s{a1},s{a2}));
        end
    end

elseif n==3
    for a1=1:4
        for a2=a1:4
            for a3=a2:4    
                T(a1,a2,a3)=trace(M*betterkron(s{a1},s{a2},s{a3}));
            end
        end
    end    


else
    T=[]
end

%Not very clever but just to keep it simple
function krn=betterkron(A,varargin)
    krn = A;
    for j = 2:nargin;
        krn = kron(krn,varargin{j-1});
    end   
end

end

Although it is possible in principle to do multiple loops like this with a recursive function, it will be complicated. 虽然原则上可以用递归函数做这样的多个循环,但它会很复杂。 Luckily using multiple loops isn't the best way to do it. 幸运的是,使用多个循环并不是最好的方法。 MATLAB lets you convert back and forth between N-dimensional subscripts and 1-dimensional linear indices. MATLAB允许您在N维下标和1维线性索引之间来回转换。 So you can do a single loop over the linear indices, then convert back to N-dimensional subscripts. 因此,您可以对线性索引执行单个循环,然后转换回N维下标。 So something like this: 所以像这样:

for i=1:numel(Matrix)  % loop over linear index 
    inds = ind2sub(size(Matrix), i);  % convert linear index to subscript

    % Each index should be greater than or equal to the previous
    % e.g. a2=a1:4, a2 starts at a1 so cannot be less than a1
    if any(diff(inds) < 0)
        continue
    end

    % Do the calculation
    % s{inds} is equivalent to s{i1}, s{i2}, ...
    T(i) = trace(M*betterkron(s{inds}));
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM