FailureGod
FailureGod

Reputation: 382

Take sin of expression with units in sympy

The main problem

The problem is that when I take the sin of 30 degrees (with units) I can't use N to find a numeric answer. A minimum example is below.

from sympy import sin, N
from sympy.physics.units import deg
N(sin(30*deg))

So is there either


Known partial solution

I am aware of the .args way of getting the numbers but my problem with that is that if I do something like

from sympy import sin, N
from sympy.physics.units import deg, rad, convert_to
number = convert_to(30*deg, rad)
number.args

This will yield a (1/6, pi, rad). But it can also yield a tuple with just two parts. So to separate everything out I need a whole other function.

Upvotes: 0

Views: 224

Answers (1)

smichr
smichr

Reputation: 19115

Instead of using units could you just use numbers?

>>> from sympy.geometry.polygon import rad, deg
>>> rad(30) # instead of 30*deg
pi/6
>>> deg(pi)
180

Otherwise, to do a mass conversion from units to numbers you could change any Mul that has the unit deg into rad(Mul/deg):

>>> from sympy.physics.units import deg
>>> cos(30*deg)
cos(30*deg)
>>> cos(rad(30))
sqrt(3)/2
>>> pats = lambda x:x.is_Mul and x.has(deg), lambda x: rad(x/deg)
>>> cos(30*deg).replace(*pats)
sqrt(3)/2

Upvotes: 1

Related Questions