Source code for irfpy.util.sprel
''' Formulae of special relativity.
.. codeauthor:: Yoshifumi Futaana
.. autosummary::
time_dilation
length_contraction
energy
'''
from irfpy.util import constant as k
from irfpy.util import lorentztrans as lt
from irfpy.util import unitq as u
[docs]def time_dilation(t0, v):
r''' Calculate the time dilation
:param t0: Time in the rest frame (moving with the object) in ``s``.
:param v: Velocity of the object in ``m/s``.
:returns: ``t1``, time observed by an observer moving with relative to the object (``s``).
.. math::
t_1 = \gamma t_0
>>> print('%.3f' % time_dilation(1, 42300000.))
1.010
Unitful version, too.
>>> u.pprint(time_dilation_u(1 * u.s, 42300000. * u.m / u.s), fmt='%.8f')
1.01010541 s
'''
return t0 * lt.gamma(v)
time_dilation_u = u.make_unitful_function(time_dilation, [u.s, u.m / u.s], u.s)
''' Unitful version of :func:`time_dilation`
'''
[docs]def length_contraction(l0, v):
r''' Calculate the length contraction (Lorentz contraction)
:param l0: The proper length (length of the object in the rest frame) in ``m``.
:param v: Velocity of the object relative to the observer (``m/s``).
:returns: ``l1``, length observed by an observer moving relative to the object (``m``).
.. math::
l_1 = \frac{l_0}{\gamma}
>>> print('%.4f' % length_contraction(1, 13400000.))
0.9990
>>> print('%.4f' % length_contraction(1, 42300000.))
0.9900
Unitful version, too.
>>> u.pprint(length_contraction_u(1 * u.km, 42300 * u.km / u.s).rescale(u.km), fmt='%.9f')
0.989995689 km
'''
return l0 / lt.gamma(v)
length_contraction_u = u.make_unitful_function(length_contraction, [u.m, u.m / u.s], u.m)
''' Unitful version of :func:`length_contraction`.
'''
[docs]def energy(m):
r''' Return the total energy
:param m: Mass of the object in ``kg``.
:returns: The rest energy, **E**, in ``J``.
.. math::
E = m c^2
>>> me = 9.11e-31 # Electron mass
>>> print('%.3e' % energy(me))
8.188e-14
>>> ene_u = energy_u(me * u.kg)
>>> print('%.3f keV' % ene_u.r(u.keV))
511.034 keV
>>> print('%.3f MeV' % energy_u(u.k.proton_mass).r(u.MeV))
938.272 MeV
'''
return m * k.c ** 2
energy_u = u.make_unitful_function(energy, [u.kg], u.J)
''' Unitful version of :func:`energy`.
'''
import unittest
import doctest
[docs]def doctests():
return unittest.TestSuite((
doctest.DocTestSuite(),
))
if __name__ == '__main__':
unittest.main(defaultTest='doctests')