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

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.13
File Size Uploaded Provenance
unitforge-0.5.13.tar.gz 99.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for unitforge 0.5.13
File Interpreter ABI Platform
unitforge-0.5.13-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.13.tar.gz

Download URL unitforge-0.5.13.tar.gz
Size 99.9 kB
Tags Source
SHA-256 checksum
How to use checksums
cfb6666ea1bb2d6c3d433154f34c8dccfd847fc3c13057d4d451bc520c1413b1
BLAKE2b-256 checksum
How to use checksums
f18c73c80bcbfcb99f032b458ded6c543e7c3cd22c69fc1c64eb6b7687c162b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.9.0

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

Download URL unitforge-0.5.13-cp312-cp312-manylinux_2_34_x86_64.whl
Size 962.1 kB
Tags CPython 3.12 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
56d545a0ec80bdc5044d1b252539b7982ca55a0c2872fc63ffaa8e019fa8dc71
BLAKE2b-256 checksum
How to use checksums
a353fc2cae380f47283fe4a5eff6a0d3e7dd9a1445d746190d8293244da0b923
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.9.0

Security for unitforge 0.5.13

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