Skip to main content

A package for physical units.

Project description

PhyUnit

PhyUnit is a Python package for physical units and quantities.

Installation

PhyUnit has been uploaded as a package in pypi: phyunit. You can directly install it using pip.

pip install phyunit

Quickstart: sub-package phyunit.SI

Sub-package phyunit.SI implements SI unit definitions, from which you can import many very useful classes:

class content
phyunit.SI.SI physical constants in SI unit
phyunit.SI.si SI units
phyunit.SI.prefix SI unit prefixes
phyunit.SI.particle constants in particle physics
phyunit.SI.conversion.au atomic unit value in SI
phyunit.SI.conversion.nu natural unit value in SI
phyunit.SI.conversion.Planck Planck unit value in SI
>>> from phyunit.SI import SI, si

SI: SI physical constant class

class SI provide physical constants defined by (or derived from) SI unit system.

Like speed of light c, Planck constant h, electron mass me... directly use them.

>>> from phyunit.SI import SI

>>> SI.c
Constant(299792458, m/s)

>>> print(SI.me)
9.1093837139e-31 kg

>>> print(SI.me * SI.c**2)
8.18710578796845e-14 kg·m²/s²

particle class contains constants in particle physics, it is separated from SI class for symbol and namespace clarity.

>>> from phyunit.SI import particle

>>> print(particle.mtau)
3.16754e-27 kg

si: SI unit class

class si provides common SI units, like meter m, second s, and units with prefix like centimeter cm.

>>> from phyunit.SI import si

>>> print(1 * si.m / si.s)
1 m/s

>>> print((760 * si.mmHg).to(si.Pa))
101325.0 Pa

prefix: unit prefix factor

class prefix contains prefix from quetta- (Q-, = 10^30) to quecto- (q-, = 10^-30), and byte prefix like ki- (2^10 = 1024), Mi- (2^20), Gi- (2^30)... It's just a number factor, not Quantity.

>>> from phyunit.SI import prefix

>>> prefix.mega
1000000.0

>>> prefix.Pi  # 2**50
1125899906842624

conversion module: au, nu and Planck

Convert values in other unit system to SI unit, i.e. au.length is the atomic unit (a.u.) of length in SI unit.

>>> from phyunit.SI.conversion import au

>>> print(1 * au.length)  # 1 Bohr radius
5.291772105437146e-11 m

Tutorial: Define phyunit.Quantity

Import class phyunit.Quantity to define a quantity object with a certain value and unit:

>>> from phyunit import Quantity

>>> F = Quantity(1, 'kg.m/s2')

where F is a Quantity object, and it has properties:

>>> F.value
1

>>> F.unit
Unit('kg·m/s²')

>>> F.dimension
Dimension(T=-2, L=1, M=1, I=0, Theta=0, N=0, J=0)

Using with numpy

phyunit is compatible with numpy. You can directly operate on numpy.ndarray with units.

>>> import numpy as np

>>> length = np.array([1, 2, 3]) * si.m
>>> print(length)
[1 2 3] m

>>> print(length**2)
[1 4 9] m²

>>> print(np.sum(lengths))
6 m

Example

phyunit is also compatible with matplotlib.

import numpy as np
from matplotlib import pyplot as plt
from phyunit.SI import SI, si

lam = np.linspace(0.01, 3, 100)[:, None] * si.um  # wavelength
T = np.array([3000, 4000, 5000], dtype=np.float64) * si.K  # temperature

nu = SI.c / lam  # frequency
I = 2 * SI.h * SI.c**2 / (lam**5 * np.expm1(SI.h * nu / (SI.kB * T)))  # intensity
I.to('W/m3/sr', inplace=True)

plt.plot(lam, I)
plt.xlabel(f'Wavelength [{lam.unit}]')
plt.ylabel(f'Intensity [{I.unit}]')
plt.legend([f'T = {T[i]:n}' for i in range(T.value.size)])
plt.title('Blackbody Radiation')
plt.show()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

phyunit-0.2.1.tar.gz (44.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

phyunit-0.2.1-py3-none-any.whl (49.9 kB view details)

Uploaded Python 3

File details

Details for the file phyunit-0.2.1.tar.gz.

File metadata

  • Download URL: phyunit-0.2.1.tar.gz
  • Upload date:
  • Size: 44.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for phyunit-0.2.1.tar.gz
Algorithm Hash digest
SHA256 069dc43c63958126f38600e5c9b90bf3ebd695efd0c54d4a1db83c9fb3dc420e
MD5 b6109f1c4e5aff6867b5215af730f17c
BLAKE2b-256 66b3c5ec26b33eb2386adc7ca6b6d8fed6c1c6978a3fab212a14f559d0d97c81

See more details on using hashes here.

File details

Details for the file phyunit-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: phyunit-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 49.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for phyunit-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6e0808785c3feecd0bf279d0f817fe91e93aa1d666b6c4db1dca3ac8fb392224
MD5 d874c8c6bd11934e76d1255f6a3f8bf7
BLAKE2b-256 b08b499e97523572ef22facc2b813ca2ef0431733b462ab5b773435ebddaf96e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page