Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have the following code:

Sigma = {{1, 1}, {1, 1}};

sol = 
 Sum[ReplacePart[Apply[KroneckerProduct, Table[id, {i, 1, 2}]], {i -> Sigma}], {i, 1, 1}]

id = IdentityMatrix[2];

sol // Dimensions
{4, 4}
sol2 = 
  Sum[ReplacePart[Apply[KroneckerProduct, Table[id, {i, 1, 2}]], {i -> Sigma}], {i, 1, 1}]

sol2 // Dimensions
 {4}

As you see, when I evaluate the Sum[...] expression a second time, the dimension of the result changes and gives the wrong result. The result must be a 4 by 4 matrix. If I had put the id before sol, then I would have gotten the wrong result on the first evaluation.

How can I resolve this problem?


I think the problem is with

ReplacePart[Apply[KroneckerProduct, Table[id, {i, 1, 2}]], {i -> Sigma}]

Before assigning id the position i is clear because we have something like {id, id} so ReplacePart can change the position i which contains id with Sigma. However, after assigning id, the position i is not clear anymore. I cannot find a solution to this problem.

share|improve this question
    
Try ClearAll[sol, sol2, id, sigma]; before all your code; and move the definition of id before sol =... –  kguler Jun 4 at 13:29
    
@kguler. It didn't work. –  yashar Jun 4 at 13:35
    
@yashar - it worked perfectly with me after following kgulers advice. Also, you should use ":=" (SetDelayed) instead of just "=". –  eldo Jun 4 at 13:38
    
@yashar I get 4 for both dimensions and also sol==sol2 gives True (btw, all this on MMA V 9.0.1.0 Windows 8 64bit) –  kguler Jun 4 at 13:44
    
@kguler. The dimension 4 is the wrong result. The result of the calculation must be a matrix. –  yashar Jun 4 at 13:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.