1
$\begingroup$

I am new to Mathematica, so I am sorry if this is a bit of a stupid question but I really am stumped.

I am trying to define a function in Mathematica that does a very specific thing, but for some reason Mathematica keeps crashing while testing it. After messing around a bit I have found where the problem lies: when trying to evaluate a simple For loop Mathematica keeps crashing (to the point where I have to force close it with Task Manager). See the image below:

screenshot

vectorBasis is a function defined below. As you can see, Length[vectorBasis[perm]] is simply the number 3, so I really feel like the For-loop should print the numbers 1, 2, and 3. But this completely crashes Mathematica.

Can anyone explain to me why this happens and how I can avoid it? Thanks!

Edit: Someone asked for a copy paste-able code, so here it is:

perm = {2,3,4}
vectorBasis[perm]
Length[vectorBasis[perm]]
For[i = 1, i <= Length[vectorBasis[perm]], i++, Print[i]]

The reason that I hadn't included it is that for this to be evaluate-able you need the function vectorBasis, which relies on functions from the amplituhedronBoundaries package (paper). The definitions might be a bit convoluted, but they seem to work:

<<amplituhedronBoundaries`

arrayToVec[array_, dimension_] := Module[{EmptyVec = ConstantArray[0, dimension]}, For[i = 1, i <= Length[array], i++, EmptyVec = placePart[EmptyVec, array[[i]] -> 1]]; EmptyVec]

vectorBasis[permutation_] := arrayToVec[#, Length[permutation]] & /@ hypBasis[topCell[Length[permutation], permK[permutation]]]

where hypBasis, topCell and permK are defined in the package I linked above.

$\endgroup$
3
  • 5
    $\begingroup$ Please post a copy-pastable form of the code rather than an image. $\endgroup$ Commented Feb 1, 2021 at 17:16
  • 5
    $\begingroup$ Not directly related to the question, but learning Mathematica and using For loops are usually mutually exclusive undertakings ;) There are much better ways that you should learn. $\endgroup$ Commented Feb 1, 2021 at 17:35
  • 4
    $\begingroup$ Staples, can you, please, provide a code snippet to acquire & load the package these functions are dependent upon? It will make this question much better for future users & for those who wish to try to answer. $\endgroup$ Commented Feb 1, 2021 at 23:31

1 Answer 1

3
$\begingroup$

The reason you end up in an infinite loop is that you are reusing i as iteration variable. After executing vectorBasis[perm], arrayToVec has set i to $2$. Therefore, every time the condition i <= Length[vectorBasis[perm]] is checked, i is reset to $2$, and the condition will always be satisfied. An easy fix would be to make i in arrayToVec a local variable, i.e.

arrayToVec[array_, dimension_] := 
Module[{EmptyVec = ConstantArray[0, dimension],i}, 
    For[i = 1, i <= Length[array], i++, 
        EmptyVec = ReplacePart[EmptyVec, array[[i]] -> 1]]; 
    EmptyVec
]

As @J.M. suggests, a Do loop would be a better fit for the job

arrayToVec[array_, dimension_] := 
Module[{EmptyVec = ConstantArray[0, dimension]}, 
   Do[EmptyVec = ReplacePart[EmptyVec, array[[i]] -> 1], {i, Length[array]}] 
   EmptyVec
]

More compact versions without loops would be

arrayToVec[array_, dimension_] := 
    ReplacePart[ConstantArray[0,dimension],#->1&/@array]

arrayToVec[array_, dimension_] := 
    Sum[UnitVector[dimension,i],{i,array}]

Also, in case someone wants to reproduce this, the positroids` package is required, which is bundled with the arXiv preprint 1212.6974 (this defines for example Global`permK[perm]).

$\endgroup$
1
  • 1
    $\begingroup$ I would even go further and suggest that Do[] should replace For[] here. $\endgroup$ Commented Feb 2, 2021 at 13:18

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.