2
$\begingroup$

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]

enter image description here

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]

enter image description here

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.

$\endgroup$
4
  • 1
    $\begingroup$ Could you perhaps benefit from the DumpSave function? $\endgroup$
    – Syed
    Jan 30 at 10:56
  • $\begingroup$ I don't understand how you expect to evaluate all these functions in your list. Do you want to have nameless pure functions? $\endgroup$
    – rhermans
    Jan 30 at 11:10
  • $\begingroup$ Something like computation[i_, j_] = {i, j, If[#1*#2 < 40, Exp[-i*#2 + j*#1^2]*Cos[i^3*#2], 0]&} ? $\endgroup$
    – rhermans
    Jan 30 at 11:18
  • 1
    $\begingroup$ Adding Evaluate in the if statement If[x*y < 40, Evaluate@func[x, y], 0] basically implements what rhermans commented. Not sure if that is what Op wants. $\endgroup$
    – N0va
    Jan 30 at 11:23

1 Answer 1

4
$\begingroup$

You may benefit by using Pure Functions

computation[i_, j_] := Module[
    {
        func = Function[{x,y}, If[x*y < 40, Exp[-i*y + j*x^2]*Cos[i^3*y],0]]
    },
    { i, j, func }
]

Flatten[
    Table[
        computation[i, j]
        , {i, 1, 2, 1}
        , {j, 1, 3, 1}
    ]
    , {1, 2}
]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thanks! Do you know how to generalize this approach for the case of an arbitrary function (including implicit ones e.g. interpolations)? $\endgroup$ Jan 30 at 16:12
  • $\begingroup$ @JohnTaylor I can't answer that in general, it will depend on the specific details. If you have an concrete example of a function that you can not implement yourself, I suggest you post another question explaining what is that function, offer a minimalistic example of a function that has the same relevant characteristics as the one you want to use, tells us what have you tried, and why it doesn't work the way you expect. $\endgroup$
    – rhermans
    Jan 30 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.