Carlos_kuro
Carlos_kuro

Reputation: 112

How to make sin(pi) and cos(pi/2) zero?

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.

Upvotes: 1

Views: 3686

Answers (3)

James
James

Reputation: 63

You can use the mpmath Python library that provides the functions cospi and sinpi, which includes already the factor π in the argument, and computes more accurately the expression than cos and sin.

>>> cospi(0.5)
0.0

See https://mpmath.org/doc/1.1.0/functions/trigonometric.html#trigonometric-functions-with-modified-argument.

Upvotes: 2

user6655984
user6655984

Reputation:

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]

Upvotes: 8

diligar
diligar

Reputation: 423

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)

Upvotes: 0

Related Questions