0
$\begingroup$

I have been developing Mathematica code to interpret LaTeX code, extract formulas and apply various Mathematica operations (e.g. Simplify, FullSimplify, etc).

In doing so, I found what it seems to be a bug in Mathematica (v. 10.1 and v. 10.2, MAC OS X Yosemite), which I have already reported. However, I would like to ask two things here:

  1. If it is really a bug (the issue arises ONLY evaluating the two expressions below FAZ followed by ATEST followed by FAZ - each of them seems to be working well and they do not depend upon each other).

  2. Is there any simple workaround (see note in the end)

The following is a simplified portion of my code that shows this issue.

  1. I have defined a function FAZ which takes a pure function f and computes various quantities associated with f -- for example, simplifies, differentiates, etc

    FAZ := 
      Function[{f},
        Print @ 
          Manipulate[
            ToExpression[form] @ ToExpression[action] @ (f[x]), 
            {action, {"Simplify", "FullSimplify"}}, 
            {form, {"StandardForm", "TraditionalForm"}}];
        Print @ Manipulate[{k, D[f[x], {x, k}]}, {k, 1, 10, 1}];
        Print @ Manipulate[{k, D[InverseFunction[f][x], {x, k}]}, {k, 1, 10, 1}];];
    
  2. I have also defined another function ATEST that takes a latex string, converts it into Mathematica code

    ATEST := 
      Function[{ML}, 
        Zb := 
          Function[frm, 
            Manipulate[
              ToExpression[frm] @ ToExpression[action] @ ToExpression[#, TeXForm], 
              {{action, "Simplify", "Operation"}, {"Simplify", "FullSimplify"}}] &];
        Eq3 :=
          Function[LTXX,
            Manipulate[
              Grid[Transpose[{LTXX, Map[Zb[form], LTXX]}], 
                Frame -> All],
              {{form, "StandardForm", "Display form"}, 
                 {"StandardForm", "TraditionalForm", "TeXForm", "MathMLForm"}}]];
        Print[Eq3[ML]];];
    
  3. Now here is a sample input

    FAZ[# - 1 &];
    MLTX = 
      {"\n\\sum_{i=1}^n 2^i\n", 
       "\n\t\\label{test}\n\t\\sum_{i=1}^n 2^i=2 (2^n-1)\n"};
    ATEST[MLTX];
    

    and so far so good -- FAZ takes the function x - 1 and creates an interactive object that can simplify the function, differentiate it various times, etc

  4. The problem is that if I call FAZ again with the same argument:

    FAZ[# - 1 &];
    

    I get

    "The kernel is not responding to a dynamic evaluation"

  5. Regarding the workaround -- it seems that the problem lies in using a string as the argument {form, {"StandardForm", "TraditionalForm"}} and then using ToExpression[form] to convert this string into a function. This makes coding easier as you can use manipulate objects with options that are strings that get converted into Mathematica functions

  6. I have tried other ways to overcome this problem, but I always have the same issue, for example:

     FAZ2 := DynamicModule[{},
     Print @ Manipulate[
       form@
        action@ (#[x]), {action, {Simplify, 
         FullSimplify}}, {form, {StandardForm, TraditionalForm}}
       ];
     Print @ Manipulate[{k, D[#[x], {x, k}]}, {k, 1, 10, 1}];
     Print @ 
      Manipulate[{k, D[InverseFunction[#][x], {x, k}]}, {k, 1, 10, 
        1}];
     ] &;
    

and (ATEST2 with MLTX defined as before)

ATEST2 := DynamicModule[{},
 Manipulate[
  Grid[Transpose[{MLTX, 
    Map[Manipulate[
      frm2@actn@ ToExpression[#, TeXForm], {actn, {Simplify, 
        FullSimplify}}] &, MLTX]}], Frame -> All],
     {frm2, {StandardForm, TraditionalForm, TeXForm, MathMLForm}}]
   ];
$\endgroup$
  • $\begingroup$ ATEST seems to entirely independent of FAZ. So why is it needed to demonstrate your problem? Does FAZ behave properly if the two calls to it do not have a call to ATEST between them? $\endgroup$ – m_goldberg Nov 27 '15 at 15:19
  • $\begingroup$ Hi m_goldberg, that seems to be the problem. You can call FAZ repeatedly but if you call ATEST in between you have the problem I described. I have attempted to modify the code in various ways - but this seems to occur in all versions that do more or less the same thing. Thanks! $\endgroup$ – Diogo Gomes Nov 28 '15 at 11:52
  • $\begingroup$ You might be interested in Splice or FileTemplate for piping LaTeX through Mma reference.wolfram.com/language/tutorial/… reference.wolfram.com/language/ref/FileTemplate.html $\endgroup$ – evanb Nov 28 '15 at 18:07
  • $\begingroup$ Thanks evanb - I didn't know about those commands. $\endgroup$ – Diogo Gomes Nov 28 '15 at 21:54
1
$\begingroup$

From what I can tell you are not correctly scoping your variables. I think the Print calls and ; in unusual places is not helping. Please have a read of What are the most common pitfalls awaiting new users?.

FAZ does not need suppression of output as you actually want the output. Use something like Column to output all of the tools.

FAZ := Function[{f},
  Column[{
    Manipulate[
     ToExpression[form]@
      ToExpression[action]@(f[x]), {action, {"Simplify", 
       "FullSimplify"}}, {form, {"StandardForm", "TraditionalForm"}}],
    Manipulate[{k, D[f[x], {x, k}]}, {k, 1, 10, 1}],
    Manipulate[{k, D[InverseFunction[f][x], {x, k}]}, {k, 1, 10, 1}]
    }]]

The bigger issue is in ATEST. Each call will redefine Zb and Eq3 for all previous calls as they defined in the Global context (or whatever context your notebook is running in). These should be scoped within ATEST if this is the way you are going to go about it. Consider that Zb's definition is independent of ML so there is no reason to define this function inside of ATEST and have a new one defined on each and every call to ATEST.

ATEST :=
 Function[{ML},
  Module[{Zb, Eq3},
   Zb :=
    Function[frm, 
     Manipulate[
       ToExpression[frm]@
        ToExpression[action]@ToExpression[#, TeXForm], {{action, 
         "Simplify", "Operation"}, {"Simplify", "FullSimplify"}}] &];
   Eq3 :=
    Function[LTXX,
     Manipulate[
      Grid[Transpose[{LTXX, Map[Zb[form], LTXX]}], 
       Frame -> All], {{form, "StandardForm", 
        "Display form"}, {"StandardForm", "TraditionalForm", 
        "TeXForm", "MathMLForm"}}]];
   Eq3[ML]]
  ]

FAZ and ATEST should be defined in their own cell. Now executing the following lines each in there own cells will behave as expected.

FAZ[# - 1 &]    

MLTX = {"\n\\sum_{i=1}^n 2^i\n", 
   "\n\t\\label{test}\n\t\\sum_{i=1}^n 2^i=2 (2^n-1)\n"};

ATEST[MLTX]

And you can execute FAZ again without any issues.

FAZ[E^# - 1 &]

Hope this helps.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks Edmund - this really solved the problem - I really appreciate your time and explanation - definitely your code is much cleaner. I still think there is a bug because the problem was arising by FAZ followed by ATEST followed by FAZ - the first FAZ and the first ATEST were running just fine, the problem was appeared on the second FAZ, which in principle should not be affected by anything that ATEST was doing. $\endgroup$ – Diogo Gomes Nov 28 '15 at 21:19
  • $\begingroup$ Thanks again Edmund - it seems the prints were causing all the problems, though I don't understand exactly why. Once I removed them from other codes with the same problem, the issues were gone. $\endgroup$ – Diogo Gomes Nov 28 '15 at 21:36
  • $\begingroup$ Maybe it has something to do with printing a DynamicModule (which is what Manipulate is under the hood. It could be locked up printing that since it is never really finished displaying because it is dynamic. Just guessing, really. $\endgroup$ – Edmund Nov 28 '15 at 21:43

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.