4
$\begingroup$

I try to use the Wolfram Python client library to evaluate a differential equation in Python.

This does not work smoothly, but using

from wolframclient.evaluation import WolframLanguageSession
from wolframclient.language import wl, wlexpr
session = WolframLanguageSession()


with WolframLanguageSession() as s:
    result = s.evaluate_wrap("TexForm[DSolve[y'[x] + y[x] == 0, y[x], x][[1, 1, 2]]]")

As you see, I already tried to bring DSolve into TexForm in this expression to be able to read it in Python.

But with using TexForm or without I get something like

(With TexForm)
Global`TexForm[Times[Power[E, Times[-1, Global`x]], C[1]]]

(Without TexForm)
Times[Power[E, Times[-1, Global`x]], C[1]]

How can I bring that into a form, that I can use in Python, i.e. in sympy or even a LaTex String?

$\endgroup$
8
  • $\begingroup$ How about CForm ? That gives C(1)/Power(E,x). $\endgroup$
    – flinty
    Commented Jul 9, 2020 at 14:51
  • $\begingroup$ That would be great, but the Python client gives CForm[Times[Power[E, Times[-1, Global`x]], C[1]]] for that. Although it evaluates well in Mathematica. $\endgroup$ Commented Jul 9, 2020 at 14:52
  • 1
    $\begingroup$ For a moment there I thought it was an encoding issue and I did result = s.evaluate("DSolve[y'[x] + y[x] == 0, y[x], x][[1, 1, 2]]".encode('ascii')) but that just returned a string back, not an expression. I think there's a bug here, because I can't seem to do any DSolve's. Other functions like Solve[x^2==3,x] work fine. $\endgroup$
    – flinty
    Commented Jul 9, 2020 at 15:22
  • 1
    $\begingroup$ I also tried (First[y[x] /. (DSolve[{y'[x] + y[x] == 0, y[0] == 1}, y[x], x])])//InputForm to eliminate the [[1,1,2]], added an initial condition, and use InputForm in case the generated parameter was the problem. But it still fails. $\endgroup$
    – flinty
    Commented Jul 9, 2020 at 15:27
  • 1
    $\begingroup$ How did you manage to get Times[Power[E, Times[-1, Global`x]], C[1]] ? All I get is String expected at position 1 in StringForm[MessageName[General, pkspec1], Slot[1]] $\endgroup$
    – flinty
    Commented Jul 9, 2020 at 15:31

1 Answer 1

4
$\begingroup$

For some reason the CForm / FortranForm / TeXForm etc. is unevaluated. However, if you convert it into a string it will work:

with WolframLanguageSession() as s:
    result = s.evaluate_wrap("With[{eq = {D[y[x], x] + y[x] == 0, y[0] == 1}}, DSolve[eq, y[x], x]]")
    tmp = s.evaluate_wrap("ToString[CForm[{}]]".format(result.result[0][0][1]))

The result: WolframKernelEvaluationResult<expression=Power(E,-x)>

Replacing CForm with TeXForm, we get WolframKernelEvaluationResult<expression=e^{-x}>

I think the reason for this behaviour is hinted at in the documentation and a sentence like this appears in the 'possible issues' section for each kind of output form:

Even when an output omits CForm from the top level, it is not stripped from subexpressions

Mathematica does not seem to redirect the special output forms to the normal output as strings, so that's why we need ToString here.

$\endgroup$

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.