relations on the index variables of a ForLoop
The ForLoop to be analyzed
This command computes the iteration space of the ForLoop loop, which is the set of values for the indices into the arrays over all iterations of loop. This set is represented as the integer solutions to a list of non-strict inequalities. The relations are written in terms of the loop's index variables and parameters.
The returned system of inequalities has not been simplified and may represent an empty set for trivial loops.
Find the possible values of i,j used by the given nested loop, as encoded by a list of inequalities:
p1 := proc(a, b, n)
local i, j;
for i from 1 to min(n + 2, 10) do
for j from i to n + 2 do
a[i, j] := b[i + 1] + a[i - 1, j - 1]
loop1 := CreateLoop(p1):
iteration_space1 := IterationSpace(loop1);
Specifying a value for the parameter n, the integer solutions to the given list of inequalities can be computed:
iteration_space1_2 ≔ subs⁡n=2,iteration_space1:isolve⁡convert⁡iteration_space1_2,set
These solutions correspond to the values of the loop variables for which the loop's statements will be executed. Note that the order of solutions returned by isolve will not necessarily match the order of the loop's execution.
Trivial Loop with an Empty Iteration Space
The body of the following loop will never be executed:
p2 := proc(a)
for i from 2 to 1 do
a[i] := a[i - 1]:
loop2 := CreateLoop(p2):
iteration_space2 := IterationSpace(loop2);
Its iteration space is a list of infeasible inequalities, i.e. there are no solutions for i:
The CodeTools[ProgramAnalysis][IterationSpace] command was introduced in Maple 2016.
For more information on Maple 2016 changes, see Updates in Maple 2016.
Download Help Document