roundingsympylambdify

rounding part of a sympy expression


I have a sympy symbolic expression that contains sin and cos. At the end of the code I use lambdify((Phi),function(Phi))(phi) but I'm getting many non-zero values because of the angle dependence which consists of several pi values, since sin(pi) doesn't return exactly 0. What I would like to do if possible, is to round while still in symbolic form. Something like sin(phi).round(5) where phi is just a symbol, so that when you lambdify the expression and sub in a value for phi the result of sin is automatically rounded to 5d.p. Is there any way to do so?

Thanks


Solution

  • You can use the floor function to round to 5 decimal places symbolically:

    In [14]: f = floor(100000*x) / 100000
    
    In [15]: lambdify(x, f)(0.123456789)
    Out[15]: 0.12345