Skip to main content

Unitforge

Unitforge is a Python library for working with physical quantities that respect units, dimensional analysis, and arithmetic correctness — powered by high-performance Rust under the hood.

Built for scientific computing, simulation, and engineering workflows where unit safety is non-negotiable.


Features

  • Strong unit enforcement: Prevent unit mismatch bugs at runtime.
  • Arithmetic support: Add, subtract, multiply, and divide quantities with automatic unit resolution.
  • Unit conversion: Convert between compatible units on the fly.
  • Scientific formatting: Values display with 4 significant digits and the configured unit.
  • 3D Vectors & Matrices: Perform vector/matrix math with quantities and units.
  • NumPy integration: Convert to/from NumPy arrays easily.
  • Constants: Built-in constants like the speed of light.
  • Serialization-ready: Optional serde-based serialization support (if compiled with feature).

Installation

pip install unitforge

Example Usage

from unitforge import Force, ForceUnit, Distance, DistanceUnit

f = Force(12.0, ForceUnit.N)
print(f.to(ForceUnit.mN))  # → 12000.0

d = Distance(2.0, DistanceUnit.m)
work = f * d
print(work)  # → 24 Nm

Unit Conversion

f = Force(1.0, ForceUnit.N)
print(f.to(ForceUnit.kN))  # → 0.001

Arithmetic

f1 = Force(5.0, ForceUnit.N)
f2 = Force(3.0, ForceUnit.N)
f_sum = f1 + f2
print(f_sum.to(ForceUnit.N))  # → 8.0

Comparing Quantities

== is tolerance-based: two quantities are equal when they differ by at most 1e-9 of the larger magnitude. An infinity equals only itself, and NaN equals nothing. <, <=, > and >= compare exactly, so for nearly equal values a == b and a < b can both be true. For explicit tolerances use a.close_abs(b, threshold) (absolute, threshold is a quantity) or a.close_rel(b, threshold) (relative to the mean magnitude). Quantities are not hashable.

a = Force(1.0, ForceUnit.N)
b = Force(1.0 + 1e-12, ForceUnit.N)
print(a == b, a < b)  # → True True

Vector and Matrix Support

from unitforge import Vector3, Distance, DistanceUnit

v = Vector3.from_list([
    Distance(3., DistanceUnit.m),
    Distance(4., DistanceUnit.m),
    Distance(12., DistanceUnit.m)
])

print(v.norm())  # → 13.0 m

NumPy Interoperability

import numpy as np
from unitforge import Vector3, ForceUnit

arr = np.array([1.0, 2.0, 3.0])
vec = Vector3.from_array(arr, ForceUnit.N)
arr_back = vec.to_array(ForceUnit.N)

print(arr_back)  # → [1. 2. 3.]

Quantities Available

Examples include:

  • Acceleration, AreaOfMoment, Density, Stiffness, Time, Volume, Angle, Area, Distance, ForcePerVolume, Strain, Velocity, AngularAcceleration, Charge, ForceArea, Force, Stress, VelocitySquared, AngularVelocity, Compliance, ForceDistance, ForceVolume, Mass, Voltage
  • Each with associated unit enums (e.g., ForceUnit, DistanceUnit, ...)

from math import isclose
from unitforge import Force, ForceUnit

f = Force(12., ForceUnit.N)
assert isclose((f * 2).to(ForceUnit.N), 24.)

Contributing

Contributions are welcome! While the core is implemented in Rust, Python usage feedback, bug reports, and API improvements are highly valued.


Repository

GitLab – Unitforge Crate

Viscous coefficients

Version 0.5.15 adds StrainRate, DynamicViscosity, TranslationalDamping, RotationalDamping, ForceRate, and ForceTime with their corresponding Unit enums. For example:

from unitforge import (
    Stress, StressUnit, Time, TimeUnit, StrainRate, StrainRateUnit,
    DynamicViscosityUnit,
)
viscosity = Stress(200.0, StressUnit.MPa) * Time(0.5, TimeUnit.s)
assert viscosity.to(DynamicViscosityUnit.MPa_s) == 100.0
stress = viscosity * StrainRate(0.002, StrainRateUnit.m_m_s)

Shear conventions belong to the constitutive/tensor layer; Unitforge adds no factors of two. Radians retain the existing dimensionless-angle convention. For compatibility, StressRate / Stress still returns Frequency, while StressRate / StrainRate returns Stress.

Metadata

Release files for unitforge 0.5.19

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for unitforge 0.5.19
File Size Uploaded Provenance Actions
unitforge-0.5.19.tar.gz 124.6 kB Without provenance Details for unitforge-0.5.19.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for unitforge 0.5.19
File Interpreter ABI Platform Actions
unitforge-0.5.19-cp312-cp312-manylinux_2_34_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.34+ x86-64 Details for unitforge-0.5.19-cp312-cp312-manylinux_2_34_x86_64.whl

Total release size: 1.3 MB

Release files / unitforge-0.5.19.tar.gz

Download URL unitforge-0.5.19.tar.gz
Size 124.6 kB
Tags Source
SHA-256 checksum
How to use checksums
9f54dd17378472c8130736cb71ee5d16902bf3c137dc59d8335fcc42963d4f04
BLAKE2b-256 checksum
How to use checksums
4d4004cc3e040eb6e6685498c0b90ba96511b6e9736e60dc7778da04ca5019a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.16.0

Release files / unitforge-0.5.19-cp312-cp312-manylinux_2_34_x86_64.whl

Download URL unitforge-0.5.19-cp312-cp312-manylinux_2_34_x86_64.whl
Size 1.1 MB
Tags CPython 3.12 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
943ba65c795851988db615a08057e50aa84d982c6bdf8cce9119a47b69ff7cf7
BLAKE2b-256 checksum
How to use checksums
3316a8ad88dd4f1b880cf088d02fcce9beb1e7619c976610726dc3a7198a6df1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.16.0

Security for unitforge 0.5.19

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.5.19 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page