Skip to main content

A library for unit and quantity consistent computations in Rust

Project description

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

GitLab – Unitforge Crate

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unitforge-0.3.14.tar.gz (59.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unitforge-0.3.14-cp312-cp312-manylinux_2_34_x86_64.whl (636.7 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

File details

Details for the file unitforge-0.3.14.tar.gz.

File metadata

  • Download URL: unitforge-0.3.14.tar.gz
  • Upload date:
  • Size: 59.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.0

File hashes

Hashes for unitforge-0.3.14.tar.gz
Algorithm Hash digest
SHA256 6e10b7d74bf73afa7d725aa828bb1a4f47cf409f82f96db2cc81205b8a1bdea7
MD5 5c0a88b9da0d52ec3c32034dc3e02a59
BLAKE2b-256 308280791a576128f79efd26968b0815fe76b23cd0e184d8f7c3f4ade8adcf3c

See more details on using hashes here.

File details

Details for the file unitforge-0.3.14-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for unitforge-0.3.14-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 2f6b23ef95ce699106aa7157ff4a2c36a0247178bd1290f8a1797cc6710ef26f
MD5 6e14db3d61c1c99173fb25729a8c95be
BLAKE2b-256 5a5ec55fc2e83708083d22507bc1f5ce862cd713b4d4a828440fb1de582d8695

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page