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.

Note that phyunit.Quantity as a parameter in pyplot.plot() is actually its value property.

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

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

nu = SI.c / lam  # frequency
I = 2 * SI.h * SI.c**2 / (lam**5 * (np.exp(SI.h * nu / (SI.kB * T)) - 1))  # intensity

plt.plot(lam, I)  # same as `plt.plot(lam.value, I.value)`
plt.xlabel('Wavelength [um]')
plt.ylabel('Intensity [W/m^3/sr]')
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.0.tar.gz (42.7 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.0-py3-none-any.whl (48.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: phyunit-0.2.0.tar.gz
  • Upload date:
  • Size: 42.7 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.0.tar.gz
Algorithm Hash digest
SHA256 a358be897bc5e8f76a6053d796f930831b63e2d18a16e198aebded2026ba7122
MD5 a43ec8de97e663e1699c3bde0fd10a91
BLAKE2b-256 eaf84d6e7b0a624d166737180799af06a312fab14dabbfaeefe0cd7433f357de

See more details on using hashes here.

File details

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

File metadata

  • Download URL: phyunit-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 48.7 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 71986620b99b7525d174f2966b20060f9d827fd8294c4d135d92f77097aa269a
MD5 b41ec2862cb38802a06e535fedb4c9b1
BLAKE2b-256 425b1ad3c8fcfb71427ced7c62a35fe6e9e4d968b55a277191f606ab4bb3631d

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