I am trying to run a code that gives out a matrix but, I seem to encounter some memory issues while trying to run it. The code is:
rm[{mx_, mz_}] := {0.048 mx (1 - 1/(2 Abs[mx])), 0.048 mz (1 - 1/(2 Abs[mz]))};
ListOfm = Outer[{#1, #2} &, DeleteCases[ Range[-128/2, 128/2, 1], 0, Infinity], DeleteCases[ Range[-128/2, 128/2, 1], 0, Infinity], 1];
ListOfrm = Map[rm, ListOfm, {2}];
rn[{nx_, nz_}] := {0.016 nx (1 - 1/(2 Abs[nx])), 0.016 nz (1 - 1/(2 Abs[nz]))}
ListOfn = Outer[{#1, #2} &, DeleteCases[Range[-384/2, 384/2, 1], 0, Infinity], DeleteCases[Range[-384/2, 384/2, 1], 0, Infinity], 1];
ListOfrn = Map[rn, ListOfn, {2}];
Fun = Compile[{{rm, _Real, 1}, {rn, _Real, 1}},
Exp[-((Compile`GetElement[rm, 1] - Compile`GetElement[rn, 1])^2/2 +
(Compile`GetElement[rm, 2] - Compile`GetElement[rn, 2])^2/2)],
CompilationTarget -> "C", Parallelization -> True, RuntimeAttributes -> {Listable},
RuntimeOptions -> "Speed"];
With[{Fun= Fun},
Mat = Compile[{{rm, _Real, 2}, {rn, _Real, 2}},
Table[Table[Fun[Compile`GetElement[rm, i], Compile`GetElement[rn, j]],
{i, 1, Length[rm]}], {j, 1, Length[rn]}],
CompilationTarget -> "C", Parallelization -> True,
RuntimeAttributes -> {Listable}, RuntimeOptions -> "Speed"]];
When I input
Mat[Flatten[ListOfrm, 1], Flatten[ListOfrn, 1]]
I receive an error that
General::nomem: The current computation was aborted because there was insufficient memory available to complete the computation.
Throw::sysexc: Uncaught SystemException returned to top level. Can be caught with Catch[[Ellipsis], _SystemException].
Out[124]= SystemException["MemoryAllocationFailure"]
However if I use lower number of elements (64
and 192
) like:
ListOfm = Outer[{#1, #2} &, DeleteCases[ Range[-64/2, 64/2, 1], 0, Infinity], DeleteCases[ Range[-64/2, 64/2, 1], 0, Infinity], 1];
ListOfrm = Map[rm, ListOfm, {2}];
ListOfn = Outer[{#1, #2} &, DeleteCases[Range[-192/2, 192/2, 1], 0, Infinity], DeleteCases[Range[-192/2, 192/2, 1], 0, Infinity], 1];
ListOfrn = Map[rn, ListOfn, {2}];
the error does not show up but the computation takes a lot of time.
I want to use higher number of elements with a good time efficiency. How can I deal with this error and speed up my code?.