Consider the following example module:
computation[i_, j_] := Module[{func},
func[x_, y_] = Exp[-i*y + j*x^2]*Cos[i^3*y];
{i, j, If[x*y < 40, func[x, y], 0]}]
I want then export a table with computation[i,j]
for various values of i,j, then import it and use the resulting functions. This is what I do:
Export[FileNameJoin[{NotebookDirectory[], "data.m"}],
Flatten[Table[computation[i, j], {i, 1, 2, 1}, {j, 1, 3, 1}], {1,
2}], "MX"]
If not quitting kernel after exporting, the imported data works well:
function[x_, y_] =
Import[FileNameJoin[{NotebookDirectory[], "data.m"}], "MX"]
function[3, 4]
However, if quitting the kernel, then the system forgets what are all of these func$2649
, and the imported data does not work properly:
function[3, 4]
Could you please tell me how to export the data properly?
P.S. I need to make func
local inside Module
since, in a realistic case, without this, I would get interference between the computations within different Module launches. It is also unavoidable to use Module
, so the toy Module
shown above serves as an irreducible set of code that just reproduces the issue.
DumpSave
function? $\endgroup$computation[i_, j_] = {i, j, If[#1*#2 < 40, Exp[-i*#2 + j*#1^2]*Cos[i^3*#2], 0]&}
? $\endgroup$Evaluate
in the if statementIf[x*y < 40, Evaluate@func[x, y], 0]
basically implements what rhermans commented. Not sure if that is what Op wants. $\endgroup$