TL;DR: I have a series of variables $x_1, x_2, x_3, \dots$ which to which I assign values inside a loop. However, when I use
ValueQ to determine which values I've already assigned, the results don't appear to make much sense.
Minimal example: Code:
Clear[x]; For[i = 1, i < 4, i = i + 1, x[i] = i; Print[i, ": ", ValueQ[x[i]], " ", ValueQ [x[i + 1]], " ", ValueQ [x[i + 10]] , " ", ValueQ[x]]]
1: True True True False 2: True True True True 3: True True True True
Explanation: In $i$-th iteration of the loop, I assign the value to
x[i]. Hence, in $i$-th iteration the values
x, x, ..., x[i] are defined, but
x[i+1], x[i+2], ... are not. However, when I try to check if
x[j] are defined, Mathematica tells me that:
x[i] is defined, as expected; 2)
x[i+1] is defined, although it is not yet defined; 3)
x[i+10] is defined, although it is never defined at all; 4)
x is defined if and only if $i \geq 2$, as expected.
Motivation: In the application I have in mind, I'm computing values $x_i$ in a somewhat haphazard order. It so happens that to compute $x_i$ it is useful to know $x_j$ where $j$ is a function of $i$ (a non-trivial one). At the time $x_i$ is computed, I would like to check if $x_j$ has already been computed, and use its value if it has (if it has not, then I need to compute $x_i$ in a more time-consuming way). However, when I try to implement this using
ValueQ, I get the unexpected behaviour described above.
Question: How does this behaviour arise, and how can I change the above code to make it work?