I'm trying to construct a large matrix which is derived from some higher rank tensor (the rank of interest to me changes case by case, so it needs to be a general method). Currently, the process of building the tensor is taking a huge amount of time. Schematically, the thing I'm trying to build (I think) looks like this.
latsize = 20;
d = 3;
t = Table[Symbol["t" <> ToString[i]], {i, d}];
s = Table[Symbol["s" <> ToString[i]], {i, d}];
dotprod = (t - s).(t - s);
Print["Time to do the whole thing = ", First[AbsoluteTiming[
ulimit = ConstantArray[latsize, 2 d];
llimit = ConstantArray[1, 2 d];
tablelimits = Transpose[{Join[t, s], llimit, ulimit}];
Print["Time to construct tensor = ", First[AbsoluteTiming[
tensor = Table[dotprod, ##] & @@ tablelimits;
]]];
Print["Time to construct matrix = ", First[AbsoluteTiming[
matrix = ArrayReshape[tensor, {latsize^d, latsize^d}];
]]];
]]];
Print["Total number of elements = ", Length@Flatten@tensor];
The process quickly becomes lengthy as d
and latsize
are increased. Any suggestions for how to speed this up?
KroneckerProduct
,TensorProduct
andOuter
might speed things up. $\endgroup$