I have these functions that are used to defineMyFun below
F[x_, y_] = {{0, (17 I)/10,
2 Cos[1/2 (x - Sqrt[3] y)], (0.2` +
0.34641016151377546` I) Cos[1/2 (x - Sqrt[3] y)],
2 Cos[1/2 (x + Sqrt[3] y)], (-0.2` +
0.34641016151377546` I) Cos[1/2 (x + Sqrt[3] y)]}, {-((
17 I)/10),
0, (-0.2` + 0.34641016151377546` I) Cos[1/2 (x - Sqrt[3] y)],
2 Cos[1/2 (x - Sqrt[3] y)], (0.2` +
0.34641016151377546` I) Cos[1/2 (x + Sqrt[3] y)],
2 Cos[1/2 (x + Sqrt[3] y)]}, {2 Cos[
1/2 (x - Sqrt[3] y)], (-0.2` - 0.34641016151377546` I) Cos[
1/2 (x - Sqrt[3] y)], 0, -1.4722431864335457` - 0.85` I,
2 Cos[x], -((2 Cos[x])/
5)}, {(0.2` - 0.34641016151377546` I) Cos[
1/2 (x - Sqrt[3] y)],
2 Cos[1/2 (x - Sqrt[3] y)], -1.4722431864335457` + 0.85` I,
0, (2 Cos[x])/5,
2 Cos[x]}, {2 Cos[
1/2 (x + Sqrt[3] y)], (0.2` - 0.34641016151377546` I) Cos[
1/2 (x + Sqrt[3] y)], 2 Cos[x], (2 Cos[x])/5, 0,
1.4722431864335457` -
0.85` I}, {(-0.2` - 0.34641016151377546` I) Cos[
1/2 (x + Sqrt[3] y)],
2 Cos[1/2 (x + Sqrt[3] y)], -((2 Cos[x])/5), 2 Cos[x],
1.4722431864335457` + 0.85` I, 0}}
f1[x_, y_] = D[F[x1, y1], x1] /. { x1 -> x, y1 -> y};
f2[x_, y_] = D[F[x1, y1], y1] /. { x1 -> x, y1 -> y};
tft[r_, er_] = If[r <= er, 1, 0];
and this is MyFun
MyFun[x_, y_, z_] := Block[{val, fun, order, reslt},
{val, fun} = Eigensystem[F[x, y]];
order = Ordering[val];
val = val[[order]];
fun = fun[[order]];
reslt = (Sum[
If[in == jn,
0, (tft[val[[jn]], z] -
tft[val[[in]],
z]) (((
Conjugate[fun[[jn]]] . f1[x, y] .
fun[[in]] )*(Conjugate[fun[[in]]] . f2[x, y] .
fun[[jn]])))], {in, 6}, {jn, 6}]); Im@reslt
]
with this data
data = Flatten[Table[{x, y}, {x, -2, 2, 0.5}, {y, -2, 2, 0.5}], 1];
I evaluate
Table[ParallelSum[
1/Length@data MyFun[data[[i, 1]], data[[i, 2]], z], {i,
Length@data}], {z, -1, 1, 0.1}]; // AbsoluteTiming
{4.16042, Null}
with Parall on Table, it is faster
ParallelTable[
Sum[1/Length@data MyFun[data[[i, 1]], data[[i, 2]], z], {i,
Length@data}], {z, -1, 1, 0.1}]; // AbsoluteTiming
{2.66483, Null}
and finally with Compile but almost the same speed
compilMyFun =
Compile[{{x, _Real}, {y, _Real}, {z, _Real}}, MyFun[x, y, z],
CompilationTarget -> "WVM", RuntimeOptions -> "Speed"]
which gives
ParallelTable[
Sum[compilMyFun[data[[i, 1]], data[[i, 2]], z], {i,
Length@data}], {z, -1, 1, 0.1}]; // AbsoluteTiming
{2.71059, Null}
I was wondering if speed can be boosted more because my real data length is about 40000.
MyFuncontainsEigensystemandOrderingwhich are already compiled. That's why you should not expect any improvement here. – Henrik Schumacher Mar 04 '24 at 16:04MyFunthat does not require the eigendecomposition? – Henrik Schumacher Mar 04 '24 at 16:05SelfAdjointEigenSolverof the Eigen package viaLibraryLink. The point about using Eigen is that it might have a more performant implementation for small matrices of fixed size. It is a C++ library, so this requires some C++ experimence and some fiddling. – Henrik Schumacher Mar 04 '24 at 16:08zheevdirectly. (One should assume that this is what Mathematica does, but I am not sure.) – Henrik Schumacher Mar 04 '24 at 16:10Compilestill helps quite a bit. See my answer below. – xzczd Mar 05 '24 at 09:18