If all you needHere is the sparse array ofa a function which produces the Jacobian matrix of $f$ evaluated at a given vector, you can do the following:
sparse[v_] := (
Clear[x];
xVariables = Map[x, Range[10000]];
Evaluate[xVariables] = RandomInteger[{1,Evaluate[xVariables] 20},= 10000];v;
sparse = SparseArray[{{10000, 10000} -> x[1], {10000, 1} -> x[10000], {i_, i_} -> x[i + 1], ({i_, j_} /; j - i == 1) -> x[i]}, {10000, 10000}]
)
Here I used x[i]
as dummy variable following J.M's suggestion, and xVariables
is set to {x[1],x[2],...,x[10000]}
. The next line sets x[i]
Now you should be able to the i
-th element of the random vector. The last line is where I use the fact that the Jacobian matrix of $f$ hasit in a really nice pattern. You can check that sparse
does have the correct values, say by evaluating sparse[[1,2]]
and sparse[[1,3]]
.loop:
In[2]:= sparseTest1=sparse[RandomInteger[{1,20},10000]];
sparseTest2=sparse[RandomInteger[{1,20},10000]];
In[3]:= sparseTest1[[1,2]]
Out[3]= 11
In[4]:= sparseTest2[[1,2]]
Out[4]= 8