Issue
I was trying to write a code which requires in input of a polynomial function and then would take that input and render it into Latex, this is my problem. Here is my code any suggestions?
#%%
import sympy as sp
def f():
f= eval(input("Enter your polynominal based function: "))
return f
x = sp.Symbol('x')
f = f()
derivative = sp.diff(f,x)
statement = 'This is the derivative of '
statement1 = str(f)
statement2 = ' which is '
statement3 = str(derivative)
overallStatement = statement + statement1 + statement2 + statement3
sp.latex(overallStatement)
#%%
Solution
What you want is to parse a string in the sympy context, a facility that is provided by any decent computer algebra system (CAS). Take a look at the sympify function. A rough example (py 2.7) is shown below:
import sympy as sp
pol = raw_input("Enter polynomial: ")
p = sp.sympify(pol)
dpdx = p.diff()
print "f(x) : " + sp.latex(p)
print "f'(x): " + sp.latex(dpdx)
print "g(x) : " + sp.latex(p + dpdx)
# Enter polynomial: x**2
# f(x) : x^{2}
# f'(x) : 2 x
# g(x) : x^{2} + 2 x
Answered By - Sigve Karolius
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.