Question:

I understand that in Python `sin(pi)`

and `cos(pi/2)`

won't produce `0`

, but I'm making calculations with matrices and I need to use those values.

I'm using SymPy and at first the values of `sin(pi)`

and `cos(pi/2)`

are a little annoying. After some multiplications they start to get in the way. Is there a way to make those values be equal to `0`

in the entire module?
How can I change it in the middle of expressions?

I'll use this matrix as an example:

```
A = Matrix([
[(-sin(theta1)*sin(theta2)*cos(alpha2) + cos(theta1)*cos(theta2))*cos(theta3) + (-sin(theta1)*cos(alpha2)*cos(theta2) - sin(theta2)*cos(theta1))*sin(theta3)*cos(alpha3) + sin(alpha2)*sin(alpha3)*sin(theta1)*sin(theta3), -(-sin(theta1)*sin(theta2)*cos(alpha2) + cos(theta1)*cos(theta2))*sin(theta3) + (-sin(theta1)*cos(alpha2)*cos(theta2) - sin(theta2)*cos(theta1))*cos(alpha3)*cos(theta3) + sin(alpha2)*sin(alpha3)*sin(theta1)*cos(theta3), -(-sin(theta1)*cos(alpha2)*cos(theta2) - sin(theta2)*cos(theta1))*sin(alpha3) + sin(alpha2)*sin(theta1)*cos(alpha3), a3*(-sin(theta1)*sin(theta2)*cos(alpha2) + cos(theta1)*cos(theta2)) + d2*sin(alpha2)*sin(theta1) - d3*(-sin(theta1)*cos(alpha2)*cos(theta2) - sin(theta2)*cos(theta1))*sin(alpha3) + d3*sin(alpha2)*sin(theta1)*cos(alpha3)],
[(-sin(theta1)*sin(theta2) + cos(alpha2)*cos(theta1)*cos(theta2))*sin(theta3)*cos(alpha3) + (sin(theta1)*cos(theta2) + sin(theta2)*cos(alpha2)*cos(theta1))*cos(theta3) - sin(alpha2)*sin(alpha3)*sin(theta3)*cos(theta1), (-sin(theta1)*sin(theta2) + cos(alpha2)*cos(theta1)*cos(theta2))*cos(alpha3)*cos(theta3) - (sin(theta1)*cos(theta2) + sin(theta2)*cos(alpha2)*cos(theta1))*sin(theta3) - sin(alpha2)*sin(alpha3)*cos(theta1)*cos(theta3), -(-sin(theta1)*sin(theta2) + cos(alpha2)*cos(theta1)*cos(theta2))*sin(alpha3) - sin(alpha2)*cos(alpha3)*cos(theta1), a3*(sin(theta1)*cos(theta2) + sin(theta2)*cos(alpha2)*cos(theta1)) - d2*sin(alpha2)*cos(theta1) - d3*(-sin(theta1)*sin(theta2) + cos(alpha2)*cos(theta1)*cos(theta2))*sin(alpha3) - d3*sin(alpha2)*cos(alpha3)*cos(theta1)],
[sin(alpha2)*sin(theta2)*cos(theta3) + sin(alpha2)*sin(theta3)*cos(alpha3)*cos(theta2) + sin(alpha3)*sin(theta3)*cos(alpha2), -sin(alpha2)*sin(theta2)*sin(theta3) + sin(alpha2)*cos(alpha3)*cos(theta2)*cos(theta3) + sin(alpha3)*cos(alpha2)*cos(theta3), -sin(alpha2)*sin(alpha3)*cos(theta2) + cos(alpha2)*cos(alpha3),a3*sin(alpha2)*sin(theta2) + d2*cos(alpha2) - d3*sin(alpha2)*sin(alpha3)*cos(theta2) + d3*cos(alpha2)*cos(alpha3)],
[0,0,0,1]])
```

with SymPy I'll substitute the value

```
substitution = A.subs(alpha2, (-pi/2))
```

and I'll have a lot of `6.12323399573677e-17`

in the middle of it.

Use the symbolic pi from SymPy, not the numeric pi from math or NumPy modules. This is what you are probably doing:

```
from sympy import sin, cos
from math import pi
print([sin(pi), cos(pi/2)]) # [1.22464679914735e-16, 6.12323399573677e-17]
```

And this is what you should do instead:

```
from sympy import sin, cos, pi
print([sin(pi), cos(pi/2)]) # [0, 0]
```

Answer2:You could always make a function! Something like

```
from math import sin as oldsin
def sin(x):
if x % pi == 0:
#if x is an integer mult of pi, like pi, 2pi, -7pi, etc.
return 0
else:
return oldsin(x)
```

Answer3:This is a more general answer than what you may be looking for, but you could try to round down the values to zero. In Numpy, you can do this quite easily: the below example will round any values in the array less than 1e-14 to zero.

```
threshold = 1e-14
array[array < threshold] = 0
```

Hope this helps.