Optimizing the calculation of the inner loop in Mathematica

I am currently doing some calculations in Mathematica related to quantum mechanics. As we moved from 1D model to 2D lattice, the size of the problem becomes problematic.

We currently have a summation that looks something like this:

corr[r1_, r2_, i_, j_] = Sum[Cos[f[x1, x2] Angle[i] r1 + f[y1, y2] Angle[j] r2], {x1, HL}, {x2, HL}, {y1, HL + 1, 2 HL}, {y2, HL + 1, 2 HL}];

F [.,.] Is a search function for a precomputed correlation function, and Angle [.] Is also computed in advance.

There is no way to simplify this further. We have already undertaken simple optimization by converting a complex exponent (having zero imaginary part) into the cosine expression above.

The big problem is that these HLs are based on size dimension: for a linear dimension L along the HL axis, L ^ d corresponds (d = 2 here). So, our calculation is O (n ^ 8) in reality, neglecting the sum over i, j.

This is usually not so bad for L = 8, if not for the fact that we repeat this for 125 r1 values ​​and 125 from r2 to create a 125 x 125 image.

My question is: how can I most efficiently calculate this in Mathematica? I would do it in another language, but there are certain problems that will make it just as slow if I try it in something like C ++.

Additional information: this is a calculation of the correlation ND-ND (density density). All x and y refer to diskette points on a discrete two-dimensional grid. The only non-discrete thing here is our r.

+3
1

, , , ( ).
ir1=Angle[i] r1 ir2=Angle[j] r2

Sum[Cos[f[x1, x2] ir1 + f[y1, y2] ir2], {x1, HL}, {x2, HL}, {y1, HL+1, 2 HL}, {y2, HL+1, 2 HL}]
== Re@Sum[Exp[I f[x1, x2] ir1] Exp[I f[y1, y2] ir2], {x1, HL}, {x2, HL},{y1, HL+1, 2 HL}, {y2, HL+1, 2 HL}]
== Re[corr1[ir1] corr2[ir2]]

corr1[ir_]:=Sum[Exp[I f[x1, x2] ir], {x1, HL}, {x2, HL}];
corr2[ir_]:=Sum[Exp[I f[y1, y2] ir], {y1, HL+1, 2 HL}, {y2, HL+1, 2 HL}];

, , :), f , :
corr1 f - , - w. , .

corr1v2[ir_]:=Sum[ w[fval] Exp[I fval ir], {fval,fvals}],

, corr1 f ( FFT, ). corr2.
, f , , , f (, r, phi), corr1 , .

+5

Source: https://habr.com/ru/post/1754625/


All Articles