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
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
Metadata
Release files for unitforge 0.5.12
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| unitforge-0.5.12.tar.gz | 96.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| unitforge-0.5.12-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.2 MB
Release files / unitforge-0.5.12.tar.gz
| Download URL | unitforge-0.5.12.tar.gz |
|---|---|
| Size | 96.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
14742ef97a0d04a8b2dc922382098e51c538478f7f57d3f63f9ef9c4c75faf57
|
|
BLAKE2b-256 checksum How to use checksums |
0f86db81a0e97bf09e8968d9bf1dafc549e864669b1e82cb32cc4630d49bd930
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.15.0
|
Release files / unitforge-0.5.12-cp312-cp312-manylinux_2_34_x86_64.whl
| Download URL | unitforge-0.5.12-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 |
a5e20f9f76d5383a924a56367c36df88b2750f74351341ff1c6a47c7cf0112cd
|
|
BLAKE2b-256 checksum How to use checksums |
227a71dcae135284ed304af9f4044342c4e67da44055d4d9f175aa0a1d299687
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.15.0
|