0

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

arsenis
  • 23
  • 7

1 Answers1

1

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
Oscar Benjamin
  • 12,649
  • 1
  • 12
  • 14
  • Been messing around with the code and checked the documentation which mentions that it gives the nearest non greater integer. floor(-1e-16) for example gives -1 instead of 0. Also the floor function will cause all positive sin values to be 0 and negative ones to be -1 which is not what I want. I simply want to round the value. Using floor function therefore I get `floor(sin(np.pi))=floor(sin(np.pi/2))=0` and `floor(sin(-np.pi))=floor(sin(-np.pi/2))=-1` which makes things worse than before. – arsenis Jan 19 '21 at 15:29
  • 1
    `round(x)` is more or less the same as `floor(x + 1/2)`. – Oscar Benjamin Jan 19 '21 at 17:35