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

Metadata

Release files for unitforge 0.5.14

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.14
File Size Uploaded Provenance
unitforge-0.5.14.tar.gz 110.5 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 1.1 MB

Release files / unitforge-0.5.14.tar.gz

Download URL unitforge-0.5.14.tar.gz
Size 110.5 kB
Tags Source
SHA-256 checksum
How to use checksums
b059e200af6a92e737bbc224d1759ace084812ee4314efcdb7e4c6701e29aa79
BLAKE2b-256 checksum
How to use checksums
d8ce1001af6c86a45561d617887fba0225151666f8dcca13c9ab8b78aa827284
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

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

Download URL unitforge-0.5.14-cp312-cp312-manylinux_2_34_x86_64.whl
Size 1.0 MB
Tags CPython 3.12 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
a8bb133a956538f85b3b8c01c228ab936945ab5a3c995561853e645710245a31
BLAKE2b-256 checksum
How to use checksums
f4f7926e4bcb120453561c7d14bed92f3862b931b53afda6a0d527a425ef5817
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Security for unitforge 0.5.14

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.

Release history Release notifications | RSS feed

This release

0.5.14 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