Reputation: 1415
usually, I can call Mathematica functions in Python by constructing the string that I would give in a Mathematica notebook. This works for the first example, but not for the second one.
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))
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)
The second example gives me errors. Is this a problem of "DSolve" being called in Python?
The error reads
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.
Upvotes: 0
Views: 251
Reputation: 3977
Just as a quick check for you to be able to verify that Mathematica can do your second examples, if I open my web browser and I go to Wolfram Link
and I patiently wait for it to finish displaying "Type your Wolfram Language Input..."
and I give one left mouse click in the large empty white box below that
and I tap the "x" key and patiently wait for it to appear
and I tap the backspace key and patiently wait for it to disappear
and I paste the following from my clipboard and patiently wait for it to appear
DSolve[y'[x] + y[x] == a Sin[x], y[x], x]
DSolve[{y'[x] + y[x] == a Sin[x], y[0] == 0}, y, x]
DSolve[y'[x] + y[x] == a Sin[x], y[x], x]
and I press and hold Shift while I tap Enter
and I patiently wait until this appears
{{y[x] -> C[1]/E^x + (a*(-Cos[x] + Sin[x]))/2}}
{{y -> Function[{x}, -(a*(-1 + E^x*Cos[x] - E^x*Sin[x]))/(2*E^x)]}}
{{y[x] -> C[1]/E^x + (a*(-Cos[x] + Sin[x]))/2}}
which seems to indicate that Mathematica is able to correctly understand and evaluate that.
See if you are able to carefully reproduce these steps.
This does nothing to look at your communication between Python and Mathematica. All this does is try to verify that Mathematica is able to understand those differential equations and find solutions.
That is a very nice free service with fairly tight limitations on the size of problems and time it can take to check calculations. Please do not abuse that. Thank you.
Upvotes: 1