WolframResearch/WolframClientForPython

Calling the DSolve function in Python

sambaPython24 opened this issue · 3 comments

Hello,
this is a problem regarding the DSolve function from Mathematica, called in Python.

When I try to call an integration statement
`from wolframclient.evaluation import WolframLanguageSession
from wolframclient.language import wl, wlexpr
session = WolframLanguageSession()

alpha = wlexpr("Integrate[1/(x^3 + 1), {x, 0, 1}]")
result = session.evaluate(alpha)
print(result," ",type(result))`
everything works fine in Mathematica and in Mathematica called by Python.

When I try to run this
`from wolframclient.evaluation import WolframLanguageSession
from wolframclient.language import wl, wlexpr
session = WolframLanguageSession()

diff_0 = "DSolve[y'[x] + y[x] == a Sin[x], y[x], x]"
diff_1 = "DSolve[{y'[x] + y[x] == a Sin[x], y[0] == 0}, y, x]"
diff_2 = "DSolve[y'[x] + y[x] == a Sin[x], y[x], x]"

alpha = wlexpr(diff_0)
result = session.evaluate(alpha)
print(result," ",result)`

I get the error
String expected at position 1 in StringForm[MessageName[General, pkspec1], Slot[1]]. The expression Slot[1] cannot be used as a part specification. String expected at position 1 in StringForm[MessageName[General, pkspec1], i]. The expression i cannot be used as a part specification. String expected at position 1 in StringForm[MessageName[General, pkspec1], Slot[1]]. The expression Slot[1] cannot be used as a part specification. String expected at position 1 in StringForm[MessageName[General, pkspec1], i]. The expression i cannot be used as a part specification.
in Python while it works in Mathematica and I don't understand, why.

It's a bug in the WolframLanguage, not in the client library.

See the difference between:

DSolve[y'[x] + y[x] == a Sin[x], y[x], x]

And the following that will display errors:

EvaluationData[
 DSolve[y'[x] + y[x] == a Sin[x], y[x], x]
 ]

This is the same bug as in: #4

Let me add a few things about this issue.
First the errors are displayed as log warnings. It's always possible to silent them or to simply ignore them. I agree the user experience is not really satisfying. That being said the result is returned and is correct.
Alternatively it's possible to evaluate data using evaluate_wrap function as follows:

with WolframLanguageSession() as s:
    result = s.evaluate_wrap("DSolve[y'[x] + y[x] == a Sin[x], y[x], x]")

This will not display any warning except if you get the result with:

result.get()

Instead, you can directly access the result field:

result.result

In general, I would not advise to proceed this way but in this case it can be convenient.

Hey, thank you for your answer, it helped me a lot.
When I call
alpha = result.get() print(alpha)
I get
((Rule[Global`y[Global`x], Plus[Times[Power[E, Times[-1, Global`x]], C[1]], Times[Rational[1, 2], Global`a, Plus[Times[-1, Cos[Global`x]], Sin[Global`x]]]]],),)
and when I try to get the actual result
by
alpha = result.get()[0][0][1] print(alpha)
I indeed get the String
Plus[Times[Power[E, Times[-1, Global`x]], C[1]], Times[Rational[1, 2], Global`a, Plus[Times[-1, Cos[Global`x]], Sin[Global`x]]]]
So, I guess, until the general problem is fixed, I should use that.