# For loop and predefined functions

I tried to write a function which is basically given by a for loop and some predefined functions. I checked already that all of the predefined functions are working, but somehow my for loop does not stop. I was trying to add up these elements ProbNeu1*ProbNeu1 where in every step there is some new element from this function RuTable. Here is my code:

AvProb6[rho_, Nu_, Nstat_, list1_, list2_] :=
Module[{val, valn, RU1, value},
val = 0;
For[n = 1, n <= Nstat, n++,
RU1 = RUTable[Nu];
valn = ProbNeu1[rho, RU1, list1]*ProbNeu1[rho, RU1, list2];
val = val + valn;
];
value = (val/Nstat);
Return[value]
];


Is there someone who finds a problem with what I did here?

• Im not sure wether i understand your comment correctly. I want that in every step of the for loop, this function RuTable is evaluated new. It basically gives a random unitary matrix of a dimension which specified by Nu. So I dont know how to avoid the for loop. But thanks anyways :) Jan 17, 2020 at 14:00
• You may want to take a look at mathematica.stackexchange.com/questions/134609/… Your For loop won't stop if you pass in a non-numerical value for Nstat. Jan 17, 2020 at 14:38

If I understand what you are doing, you should be able to re-define your function to make use of vector operations, which are more natural in MMA than loops:

ClearAll[AvProb6]
AvProb6[rho_, Nu_, Nstat_, list1_, list2_] :=
Mean@
Map[
ProbNeu1[rho, #, list1] ProbNeu1[rho, #, list2] &,
Table[RUTable[Nu], Nstat]
]


Here:

• Table[RUTable[Nu], Nstat] produces a list of matrices of length Nstat using your RUTable function;
• On each item of that list, it applies ProbNeu1[rho, oneMatrixFormTheList, list1], then ProbNeu1[rho, oneMatrixFormTheList, list2], and takes the product of the two results, forming a new list of these products;
• In your original code, you were accumulating these values then dividing by the length of the list, i.e. taking the arithmetic mean; since we already have the whole list, here we directly calculate the mean value using Mean instead.

Here are a few reasons why you should consider avoiding the For loop in MMA.

• Hello and thank you for your answer. I actually had a very similar code like yours before the one i posted. The problem is that I try to do pretty complex investigations (with low computation power) and i got the advice, that it might be more efficient to use a for loop, since it might take some additional time to save a large table of the function RUTable[Nu]. Do you think that there might be a more efficient code or is there no big difference? Sorry, i barely understand anything about the complexity of algorythms. Jan 17, 2020 at 16:28
• @Jolle There is unfortunately no way to know without the code of your functions RUTable etc. However, it may be true that you could get around a memory limitation by looping, but it would not influence how much computational power you need. In fact, it would be easier to parallelize your code automatically if you use vectorized operations (e.g. use ParallelTable and ParallelMap, with some modifications). If you really want to use a loop, consider Do instead of For, which may still be easier to translate to parallel computation using ParallelDo` later on. Jan 17, 2020 at 17:22