0

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.

  • [Sympy](https://docs.sympy.org/latest/modules/parsing.html) seems also interesting in this case. – JohanC Feb 09 '21 at 21:36
  • people say `"eval" is evil` but in this situation `eval()` could be the simplest method -ie. `from math import * ; x = 1 ; eval("sin(x)/cos(x)+1")`. I'm not sure but probably `sympy` also use `eval()`. – furas Feb 10 '21 at 02:47

0 Answers0