nammerkage
nammerkage

Reputation: 304

Get results from sympy units

I'm trying to get some electrodynamics going with units in sympy. To check how i'm doing, i'm checking this relation:

equation

So i have defined mu0 and epsilon 0, and i would like to get the speed of light out:

import sympy.physics.units as u
import sympy as sp
sp.sqrt(1/(u.magnetic_constant*u.electric_constant))
>>>> 1/(sqrt(magnetic_constant)*sqrt(vacuum_permittivity))

So my result is not so useful. I have tried to find solutions online, bit i can't figure it out. Would love your input

Upvotes: 1

Views: 636

Answers (1)

Francesco Bonazzi
Francesco Bonazzi

Reputation: 1957

You can simply use the convert_to function defined in the units module:

In [3]: expr = sp.sqrt(1/(u.magnetic_constant*u.electric_constant))

In [4]: expr
Out[4]: 
                      1                      
─────────────────────────────────────────────
  ___________________   _____________________
╲╱ magnetic_constant ⋅╲╱ vacuum_permittivity 

In [5]: u.convert_to(expr, u.speed_of_light)
Out[5]: speed_of_light

In [6]: u.convert_to(expr, u.meter/u.second)
Out[6]: 
299792458⋅meter
───────────────
     second   

Upvotes: 2

Related Questions