I have to create the following sparse arrays, which are reshaped in a two dimensional matrix, so that i can use the build in Dot[]
function to do a kind of multiplication.
c[theta_Real, xMax_Integer, yMax_Integer] :=
ArrayReshape[SparseArray[{
{x_, y_, s_, x_, y_, s_} -> Cos[theta/2.],
{x_, y_, 1, x_, y_, 2} -> -Sin[theta/2.],
{x_, y_, 2, x_, y_, 1} -> Sin[theta/2.]
}, {2 xMax + 1, 2 yMax + 1, 2, 2 xMax + 1, 2 yMax + 1, 2},
0.], {2 (2 xMax + 1) (2 yMax + 1), 2 (2 xMax + 1) (2 yMax + 1)}]
sX[xMax_Integer, yMax_Integer] := ArrayReshape[SparseArray[{
{x1_, y_, 1, x2_, y_, 1} /; x1 == x2 + 1 -> 1.,
{x1_, y_, 2, x2_, y_, 2} /; x1 == x2 - 1 -> 1.
}, {2 xMax + 1, 2 yMax + 1, 2, 2 xMax + 1, 2 yMax + 1, 2},
0.], {2 (2 xMax + 1) (2 yMax + 1), 2 (2 xMax + 1) (2 yMax + 1)}]
sY[xMax_Integer, yMax_Integer] := ArrayReshape[SparseArray[{
{x_, y1_, 1, x_, y2_, 1} /; y1 == y2 + 1 -> 1.,
{x_, y1_, 2, x_, y2_, 2} /; y1 == y2 - 1 -> 1.
}, {2 xMax + 1, 2 yMax + 1, 2, 2 xMax + 1, 2 yMax + 1, 2},
0.], {2 (2 xMax + 1) (2 yMax + 1), 2 (2 xMax + 1) (2 yMax + 1)}]
I'm trying to avoid direct definition of the 2D sparse matrices, because I have to handle a lot of similiar arrays with given multidimensional representation and I realized that reshaping is very fast, compared to the rest of calculation. Now I have to calculate
rF2D[a_, b_, x0_, y0_, theta1_, theta2_,
xMax_, yMax_, steps_] :=
Block[{r = r0[a, b, x0, y0, xMax, yMax],
walk = sY[xMax, yMax].c[theta2, xMax, yMax].sX[xMax,
yMax].c[theta1, xMax, yMax]},
Do[r = walk.r.ConjugateTranspose[walk], {steps}]; r]
for large xMax,yMAx
, e.g. rF2D[1.,0.,0,0,Pi/2.,Pi/4.,50,50,50]
where r0
is a matrix with complex entries:
r0[a_, b_, x0_, y0_, xMax_, yMax_] :=
ArrayReshape[SparseArray[{
{x0 + xMax + 1, y0 + yMax + 1, 1, x0 + xMax + 1, y0 + yMax + 1,
1} -> Abs[N[a]]^2,
{x0 + xMax + 1, y0 + yMax + 1, 1, x0 + xMax + 1, y0 + yMax + 1,
2} -> N[a]\[Conjugate] N[b],
{x0 + xMax + 1, y0 + yMax + 1, 2, x0 + xMax + 1, y0 + yMax + 1,
1} -> N[a] N[b]\[Conjugate],
{x0 + xMax + 1, y0 + yMax + 1, 2, x0 + xMax + 1, y0 + yMax + 1,
2} -> Abs[N[b]]^2
}, {2 xMax + 1, 2 yMax + 1, 2, 2 xMax + 1, 2 yMax + 1, 2},
0.], {2 (2 xMax + 1) (2 yMax + 1), 2 (2 xMax + 1) (2 yMax + 1)}]
At the moment this is impossible, because calculation needs too much time. For much smaller xMax,yMAx
, e.g. rF2D[1.,0.,0,0,Pi/2.,Pi/4.,10,10,10]
finishes after 5 seconds on may machine.
Is there any possibility to speed up the creation of the sparse arrays, because this seems to eat most of the time and the multiplication is doing quite fast? Thanks for your help!