Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling the DSolve function in Python #22

Closed
sambaPython24 opened this issue Jul 3, 2020 · 3 comments
Closed

Calling the DSolve function in Python #22

sambaPython24 opened this issue Jul 3, 2020 · 3 comments
Labels
wolfram language bug A bug in the Wolfram Language.

Comments

@sambaPython24
Copy link

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.

@DbxDev DbxDev added the wolfram language bug A bug in the Wolfram Language. label Jul 3, 2020
@DbxDev
Copy link
Contributor

DbxDev commented Jul 3, 2020

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

@DbxDev DbxDev closed this as completed Jul 3, 2020
@DbxDev
Copy link
Contributor

DbxDev commented Jul 3, 2020

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.

@sambaPython24
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wolfram language bug A bug in the Wolfram Language.
Projects
None yet
Development

No branches or pull requests

2 participants