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.18

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.18
File Size Uploaded Provenance Actions
unitforge-0.5.18.tar.gz 120.8 kB Without provenance Details for unitforge-0.5.18.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for unitforge 0.5.18
File Interpreter ABI Platform Actions
unitforge-0.5.18-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.18-cp312-cp312-manylinux_2_34_x86_64.whl

Total release size: 1.2 MB

Release files / unitforge-0.5.18.tar.gz

Download URL unitforge-0.5.18.tar.gz
Size 120.8 kB
Tags Source
SHA-256 checksum
How to use checksums
0ed90d38a64c412f86cb8d98707dd99a9d2ab077a085066e9f300a408352ba24
BLAKE2b-256 checksum
How to use checksums
4b09a90c754c7af1b57304abbd5fdd9128591f849b7440772c71855294b16881
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.16.0

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

Download URL unitforge-0.5.18-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
5133d06a29aecc1c8b09e6df82ac42eaebc770b78061db8fcb18ca59c026b0a5
BLAKE2b-256 checksum
How to use checksums
60073a23f282da311d276d75fe747a1f54f776e10ee6a95bf96bf7ad33660ff3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.16.0

Security for unitforge 0.5.18

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.18 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