Skip to content

Wrong Unit? Zero Points!

wuzp is a header-only C++20 library that adds compile-time checks to the linear algebra libraries Eigen and Blitz++:

  • Units. A force cannot be initialized with a mass, only with a mass times an acceleration, and every element of a matrix has its own unit, so heterogeneous matrices such as a mass matrix with kg and kg m^2 entries are checked too.
  • Coordinate systems. Every tensor is expressed in a coordinate system, a type without runtime cost: adding a force expressed in A to a force expressed in B does not compile, until a transformation expresses one in the coordinate system of the other.
  • Points and vectors. Points follow the rules of affine spaces: p - q is a vector, p + v a point, and operations whose result depends on the origin, e.g. p + q or 2 * p, do not compile.
  • Index variance. Tensors know whether each index is upper (a vector) or lower (a covector), so only contractions of an upper with a lower index compile, and a covector is not mistaken for a vector.
  • Transformations. Rotations, translations and proper rigid transformations, active and passive, Lorentz transformations and Jacobians, which compose with * in the order of Drake's monogram notation and have an inverse().
  • Spatial vectors. Spatial velocities, accelerations and forces (twists and wrenches) also know the frame a motion is measured in and the point it is about, like Drake's: the velocities of two different points cannot be added, until one is shifted to the other point.

All checks happen at compile time: with Eigen, wuzp is as fast as plain Eigen (see the benchmarks).

#include <wuzp/wuzp.hpp>
#include <wuzp_eigen/ValueMatrix.hpp>
#include <wuzp_units/Unit.hpp>

struct A {};
struct B {};
using CoordinateSystemA = wuzp::CoordinateSystem<A>;
using CoordinateSystemB = wuzp::CoordinateSystem<B>;
using ForceInA = wuzp::ForceVector3<wuzp_eigen::ValueMatrix, double, CoordinateSystemA>;
using ForceInB = wuzp::ForceVector3<wuzp_eigen::ValueMatrix, double, CoordinateSystemB>;
using RotationFromAToB = wuzp::PassiveSpatialRotation3<wuzp_eigen::ValueMatrix, double, CoordinateSystemA, CoordinateSystemB>;

int main() {
  const ForceInA forceInA{1.0, 2.0, 3.0};
  const ForceInB forceInB{4.0, 5.0, 6.0};
  const RotationFromAToB rotation{1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, -1.0, 0.0};  // B is A rotated by pi/2 around x.
  // const ForceInB sum = forceInA + forceInB;  // Error: Coordinate systems do not match.
  const ForceInB sum = rotation.transform(forceInA) + forceInB;
  return 0;
}

Where to go next

The design notes record why wuzp works the way it does, with the sources of its conventions and the results of a review of its mathematical and physical soundness.