0
$\begingroup$

I developed a Mathematica package using Mathematica 12.1 and I am calling this package from my java code: I generate a system of equations (where I call the package) from the java code, then send this system to the Mathematica Kernel to solve it, but it takes at least 2 or 3 minutes to return a result (and sometimes keeps running). When I run Mathematica front end to solve the same system, it takes few microseconds. Can anyone tell me why is Mathematica Kernel is extremely slow?

thanks a lot

$\endgroup$
4
  • 4
    $\begingroup$ Welcome to the Mathematica Stack Exchange. Tell us more about your Java setup and how you are invoking the kernel. Have you followed an(y) example from a tutorial that worked better etc. Thanks. $\endgroup$
    – Syed
    Commented Sep 17, 2022 at 10:46
  • $\begingroup$ I am working with Intellij Idea and use jlink, this is the java code where I create a connection with Mathematica then call Mathematica Kernel : KernelLink ml; String s = "-linkmode launch -linkname 'C:\\Program Files\\Wolfram Research\\Mathematica\\12.1\\MathKernel.exe'"; ml = MathLinkFactory.createKernelLink(s); ml.connect(); ml.discardAnswer(); String res = ml.evaluateToInputForm(exp,0); //exp=<<MyPackage;UsingFrontEnd[MyPackageFunc[]] $\endgroup$
    – W.G
    Commented Sep 17, 2022 at 19:26
  • $\begingroup$ No I am not following any tutorial. thanks a lot! $\endgroup$
    – W.G
    Commented Sep 17, 2022 at 19:27
  • 1
    $\begingroup$ Can you share some examples of the code in the package? $\endgroup$ Commented Sep 20, 2022 at 9:48

1 Answer 1

1
$\begingroup$

For those who are interested, I could finally solve the problem by simply sending two seperate commands to Mathematica kernel from my java code: in the first command I evaluate the call to my package, and in the second command, I evaluate the system of equations (that uses my package). So instead of send one command of the form:

ml.evaluateToOutputForm(<<Mypackage`;systemOfEquationsToSolve) 

I send the following two commands:

ml.evaluateToOutputForm("<<MyPackage`");
ml.discardAnswer();
ml.evaluateToOutputForm(systemOfEquationsToSolve);
$\endgroup$
1
  • 1
    $\begingroup$ If the function f is define in SomePackage, then a combined (Get["SomePackage`"]; f[]) is not equivalent to evaluating these two commands separately. The package must be loaded before f[] is parsed (note: parsed, not merely evaluated). Otherwise f will be interpreted in the Global context, not in your package's context. $\endgroup$
    – Szabolcs
    Commented Nov 20, 2022 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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