Use query phrase interpretation to compute a math equation - python

I am using python and html to build a webpage where I can enter a math equation in a textbox and compute the math equation like google calculator.
Are there any libraries or suggestions as to how I may go about doing this?

You're looking for sympy
from sympy import sympify, Symbol
>>> sympify('2 ^ 3').evalf()
8
>>> x = Symbol('x')
>>> sympify('x ^ 2').evalf(subs={x: 2})
4

Related

Why is my sympy Simult eqn calc not working

I have a simultaneous equation calculator that takes in a lst which contains the strings of the two equations and is supposed to output the solutions to X and Y. Here is the code for it.
x, y = symbols('x,y')
transformations=(standard_transformations + (implicit_multiplication_application,))
eqs_sympy = [Eq(parse_expr(e.split('=')[0], transformations=transformations),
parse_expr(e.split('=')[1], transformations=transformations))
for e in final_lst]
sol = solve(eqs_sympy)
An example of final_lst : ["5x^2 + 1y^5 = 12", "5x^3 + 18y^2 = 42"] (Replace ^ with **)
However, sol just outputs a blank list, why is this so?
These highly nonlinear equations are too high of order for SymPy to give an explicit solution. You can get a numerical solution, however, if you have a good initial guess for the solution. You can get a reasonable guess by graphing the equations or by solving the related equations that don't have the highest powered terms. Use nsolve (e.g. here). Here is one of the 3 roots:
>>> from sympy import nsolve
>>> tuple(nsolve(eqs,(x,y),(-1,1)))
(-0.731937744431011, 1.56277202986898)

Having trouble with computing Taylor Series with python Sympy

I want to compute the first 3 Taylor series for the function:
f(x) = e^{-x^2} \sin( x - \pi).
I write like this. But got an error message: cannot convert expression to float. How can I fix this?
import sympy
import math
sympy.init_printing(pretty_print=True)
x = sympy.Symbol('x')
f = math.exp(-x**2)*sin(x-(math.pi))
f.series(x0=math.pi, n=3)
You're using math methods instead of sympy methods. Try:
f = sympy.exp(-x**2)*sympy.sin(x-(sympy.pi))

How to apart Exponential function in python

I am trying to apart Exponential function in python.
import sympy as sym
from sympy.abc import t
from sympy import exp
u = (3*(exp(4*t) - 1)*exp(-4*t))/4
apart = sym.apart(u, t)
print(apart)
But i get the error:
exp(4*t) contains an element of the set of generators
it looks like exp() is confusing it. For a workaround
import sympy as sym
from sympy.abc import t,z
from sympy import exp
u = (3*(exp(4*t) - 1)*exp(-4*t))/4
expr = sym.apart(u.subs(exp(t),z), z)
expr = expr.subs(z,exp(t))
Which gives
Out[3]: 3/4 - 3*exp(-4*t)/4
Using 3.7 on conda
Your expression is a univariate, rational function in terms of exp(t):
>>> u.subs(exp(t),y)
3*(y**4 - 1)/(4*y**4)
>>> apart(_)
3/4 - 3/(4*y**4)
>>> _.subs(y, exp(t))
3/4 - 3*exp(-4*t)/4
But SymPy can handle such non-symbol generators so for such an expression sym.apart(u) would have given the same result as shown above. When you said the generator was t it detected the exp(t) and raised the error since an expression like t + exp(t) has two generators that depend on t.

Substitute function calculates SymPy expression

Let's say I have an equation:
equation = d1*d2
If I use substitute function:
equation.subs({d1: 20, d2:10})
Python will output 200 instead of 20*10.
How can I only substitute symbols with numbers and not calculate the expression?
I am using SymPy, and print equation in LaTeX form, then copy them to Word.
did you mean something like this?
>>> equation = "d1*d2"
>>> equation
'd1*d2'
>>> equation.replace("d1","20").replace("d2","10")
'20*10'

Sympy - not converting `solve()` correctly

I apologize if this has been asked already.
I am just learning about SymPy and I'm wondering why it won't spit out a correct answer for what seems to be a simple equation.
from sympy.solvers import solve
from sympy import Symbol, simplify
from sympy.abc import x, alpha, sigma
alpha = Symbol('alpha')
x = Symbol('x')
sigma = Symbol('sigma')
solve((alpha - 0.5*(sigma**2))*((alpha + 0.5*(sigma**2)))**(-1)+ (1/7),sigma**2, simplify = True)
It spits out [2.0* alpha], which I know is incorrect. In fact, the answer should be [2.6666*alpha] or something like that. I'm assuming that SymPy is for some reason converting the number 2.666 to an integer string.
How can I fix this problem? Also, is there any way I could get the fractional form of the solution?
You're probably using Python 2.7, so 1/7 is giving you integer division:
>>> 1/7
0
>>> 1./7
0.14285714285714285
>>> solve((alpha - 0.5*(sigma**2))*((alpha + 0.5*(sigma**2)))**(-1)+ (1/7),sigma**2, simplify = True)
[2.0*alpha]
>>> solve((alpha - 0.5*(sigma**2))*((alpha + 0.5*(sigma**2)))**(-1)+ (1./7),sigma**2, simplify = True)
[2.66666666666667*alpha]
If you want the fractional answer, maybe something like
>>> from sympy import Rational
>>> solve((alpha - (sigma**2)/2)*((alpha + (sigma**2)/2))**(-1)+ Rational(1,7),sigma**2, simplify = True)
[8*alpha/3]
You can also use help(solve) to read the docstring of solve that tells how to use the rational keyword:
>>> solve(x-.3)
[0.300000000000000]
>>> solve(x-.3, rational=True)
[3/10]

Categories

Resources