ρ[n_, L_]:=
    ArrayFlatten,TensorProduct@@Table[Subscript[ρ, l,m][k],{k,n},{l,L},{m,L}]];

list2[n_, L_]:=

p1[n_, L_] := 
    Subscript[ρ, L, L][k]*
     If[k == k1, 1, 
         ParallelSum[Subscript[ρ, l, l][k1], {l, L - 1}]], {k1, n}], {k, n}]//Expand;

P1[n_, L_]:= P1[n,L]= p1[n,L] /. list2[n,L];

p0[n_, L_]:=
    Sum[Subscript[ρ, l, l][k], {l, L - 1}], {k, n}] // Expand;

P0[n_, L_]:=P0[n,L]= p0[n,L] /. list2[n,L];

Easy to evaluate...

P0[4, 3]-P0[3, 3]
P1[4, 3]-P1[3, 3]

What kind of changes are needed to speed up this calculation...

P0[5, 3]-P0[4, 3]
P1[5, 3]-P1[4, 3]

1 Answer 1


By using the command Rho[n_, L_] := Rho[n, L] = ..., we can make it faster:

Rho[n_, L_] := Rho[n, L] = FixedPoint[
        ArrayFlatten, TensorProduct@@Table[
            Subscript[\[Rho], l, m][k],{k, n}, {l, L}, {m, L}]];

list2[n_, L_] := list2[n, L] = ParallelTable[
        Rho[n, L][[i, j]] -> a[i, j], {i, L^n}, {j, L^n}] /. {a[i_, j_]  
            :> ToExpression["a" <> ToString[i]]} // Flatten;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.