The project I'm currently working on is a CLI which would function like Wolphram Alpha, albeit much more bare-bones. If I wanted to find the integral of sin(x)/cos(x) + 1 from 0 to 2, using it would look like this:
~$ integrate "sin(x)/cos(x)+1", a=0, b=2
1.231
Currently my code looks like this:
def _integrate(N, a, b):
# Define the integrand as f
def f(x):
return math.sin(x)/(math.cos(x)+1)
sum = 0
for n in range(1, N+1):
sum += f(a + ((n-(1/2)) * ((b-a)/N)))
return sum * ((b-a)/N)
My question is how I could take the string passed at the command line, parse it, and use it as the integrand? So far I've looked into using the compiler
module, but that excludes the possibility of using other functions or constants. Any solutions or points in the right direction would be much appreciated, let me know if anything needs clarifying.