Using PhysicalQuantities in IPython

The IPython extension makes using physical quantities easier. To load the extension use:

>>> %load_ext PhysicalQuantities.ipython

Now entering a physical quantities gets very easy:

>>> d = 2.3 s**3
>>> print("d = %s" %d)
d = 2.3 s^3
>>> t = 3 A
>>> print("t = %s" %t)
t = 3 A
>>> v = 2.3e3 * d / t
>>> print("v = %s" %v)
v = 1763.3333333333333 s^3/A

Unit conversion

The easiest way to scale a unit is to use prefix attributes:

>>> u = 1 V
>>> print(u)
1 V
>>> print(u.mV)
1000.0 mV
>>> print(u.uV)
1000000.0 uV

To convert between different representations of a unit, to() can be used:

>>> a = 1 N * 1 m
>>> print(a)
1 m*N
>>> print(a.to('J'))
1.0 J

Using other value types

The PhysicalQuantity class tries to be a wrapper around the value of a given quantity, i.e. not only single numbers can be used. For examples using Numpy arrays, take a look at the Using Numpy Arrays notebook.

>>> u = (1 + 1j) * 1V
>>> print("u = %s" %u)
u = (1+1j) V
>>> u = [1,2,3] * 1V
>>> print("u = %s" %u)
u = [1, 2, 3] V
>>> a = [1, 2, 3] * 1V
>>> a

\([1, 2, 3] $\text{V}\)

>>> a.value
[1, 2, 3]
>>> 2*a

\([1, 2, 3, 1, 2, 3] \text{V}\)

List of all defined Units:

All predefined units can be listed using the list() or html_list() function of a unit:

>>> from PhysicalQuantities import units_html_list
>>> units_html_list()
NameBase UnitQuantity
Wb1.0 $\frac{\text{m}^{2}\cdot \text{kg}}{\text{A}\cdot \text{s}^2}$Weber
s1.0 $\text{s}$Second
h3600.0 $\text{s}$Hour
lx1.0 $\frac{\text{cd}\cdot \text{sr}}{\text{m}^2}$Lux
sr1.0 $\text{sr}$Streradian
min60.0 $\text{s}$Minute
J1.0 $\frac{\text{m}^{2}\cdot \text{kg}}{\text{s}^2}$Joule
Pa1.0 $\frac{\text{kg}}{\text{m}\cdot \text{s}^2}$Pascal
arcsec4.84813681109536e-06 $\text{rad}$seconds of arc
cd1.0 $\text{cd}$Candela
lm1.0 $\text{cd}\cdot \text{sr}$Lumen
H1.0 $\frac{\text{m}^{2}\cdot \text{kg}}{\text{A}^2\cdot \text{s}^2}$Henry
m1.0 $\text{m}$Metre
T1.0 $\frac{\text{kg}}{\text{A}\cdot \text{s}^2}$Tesla
S1.0 $\frac{\text{A}^{2}\cdot \text{s}^{3}}{\text{m}^2\cdot \text{kg}}$Siemens
C1.0 $\text{A}\cdot \text{s}$Coulomb
deg0.017453292519943295 $\text{rad}$Degree
K1.0 $\text{K}$Kelvin
g0.001 $\text{kg}$Gram
kg1 $\text{kg}$Kilogram
F1.0 $\frac{\text{A}^{2}\cdot \text{s}^{4}}{\text{m}^2\cdot \text{kg}}$Farad
W1.0 $\frac{\text{m}^{2}\cdot \text{kg}}{\text{s}^3}$Watt
arcmin0.0002908882086657216 $\text{rad}$minutes of arc
Hz1.0 $\frac{1}{\text{s}}$Hertz
A1.0 $\text{A}$Ampere
Ohm1.0 $\frac{\text{m}^{2}\cdot \text{kg}}{\text{A}^2\cdot \text{s}^3}$Ohm
N1.0 $\frac{\text{m}\cdot \text{kg}}{\text{s}^2}$Newton
V1.0 $\frac{\text{m}^{2}\cdot \text{kg}}{\text{A}\cdot \text{s}^3}$Volt
rad1.0 $\text{rad}$Radian
mol1.0 $\text{mol}$Mol