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 2 very useful classes:

class content
phyunit.SI.SI SI physical constant
phyunit.SI.si SI unit
phyunit.SI.prefix SI unit prefix
>>> 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²

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

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.1.3.tar.gz (41.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.1.3-py3-none-any.whl (46.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: phyunit-0.1.3.tar.gz
  • Upload date:
  • Size: 41.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.1.3.tar.gz
Algorithm Hash digest
SHA256 573ed61226d739a2fe39d84f7c768f25a6baced70cd26bd21fe7a8707d705e0f
MD5 bc54dcf566e50ae232e25f584d0248c9
BLAKE2b-256 01e62831cd15ff96622f2d84c9974778b965e9a4ca7a345ab8764f7c17a8732d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: phyunit-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 46.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.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6b406df718b940b80b81141c10086b3ea4e741655e48c3da2424bd789b2daffa
MD5 5f5311cda20ff8aa5708a71810316b3f
BLAKE2b-256 d70a52f151f272dd8ec51f02fe9942d03169be7c9242cbdc949308e370baceb9

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