[?] Avoid issue with message that "symbol does not have immediate value"?

Consider the following code:

InitializationValue[isingarray] = 2 RandomInteger[{0, 1}, {100, 100}] - 1

Do [temp = 
   beta*(isingarray[[i + 1, j]] + isingarray[[i, j + 1]] +  isingarray[[i - 1, j]] + isingarray[[i, j - 1]]);
  z = E^temp + E^-temp;
  isingarray[[i, j]] = If[(E^-temp)/z > RandomReal[{0, 1}], 1, -1], {i, 2, 99}, {j, 2, 99}];

Set::noval: Symbol isingarray in part assignment does not have an
immediate value.

Hi Fred,

You need to Initialize the symbol to the persisted value before referencing it.

InitializationValue[isingarray] = 2 RandomInteger[{0, 1}, {100, 100}] - 1
Initialize[isingarray]

That worked! Thanks so much. It’s always the little language-specific features that hang one up learning a new language.

It’s always the little language-specific features that hang one up learning a new language.

Hello Fred,

just as a remark: Are you aware that you can simply wright:

isingarray = 2 RandomInteger[{0, 1}, {100, 100}] - 1;

Do [
temp = beta*(isingarray[[i + 1, j]] + isingarray[[i, j + 1]] + isingarray[[i - 1, j]] + isingarray[[i, j - 1]]);
z = E^temp + E^-temp; isingarray[[i, j]] = If[(E^-temp)/z > RandomReal[{0, 1}], 1, -1], {i, 2, 99}, 
{j, 2, 99}];

You do not need to use InitializationValue and the like - (nearly) nobody needs those functions, and they are qualified as “Experimental”.

Regards – Henrik

Henrik,

I tried that, and it didn’t work until I added:

Initialize[isingarray]

Fred