Vector, Tensor, and Complex Operations for Quantities
Theme
Contents

Vector, Tensor, and Complex Operations for Quantities

Vector, Tensor, and Complex Operations for Quantities

Document #: P4487R0 Latest Status
Date: 2026-11-20
Project: Programming Language C++
Audience: SG6 Numerics
Reply-to: Mateusz Pusz
<>

1 Revision history

1.1 R0

Initial revision.

2 Abstract

Work and moment of force are both a force times a length. Work is a scalar product and moment of force a vector product, but with a single operator* both are the same expression, and a moment of force converts to work. [P3045R11] therefore defines operator* only for operands of which at most one is a vector or a tensor, and never converts a quantity to one of another character. As a result, a quantity typed as a vector can’t be used to compute work or a moment of force, and moment of inertia can’t be defined as the tensor that maps an angular velocity to an angular momentum.

This paper adds the operations of [ISO/IEC 80000] part 2 that fill this gap:

They apply to quantity specifications and, except inner_quotient, to quantities. The type of a result records the operation and its operands, so work and moment of force are different quantities. The proposal removes nothing that [P3045R11] proposes and changes the meaning of nothing in it. It is implemented in a branch of [mp-units].

3 Introduction

A vector quantity with more than one component needs a linear algebra vector as its representation, and a units library that offers only operator* can’t compute a moment of force or a mechanical work from such quantities. Linear algebra libraries either don’t overload operator* for two vectors, so force * displacement doesn’t compile, or overload it as a component-wise or a scalar product, none of which is a moment of force.

Au [Au Eigen] and nholthaus/units [nholthaus/units Eigen] recently added dot and cross (unit_dot and unit_cross in nholthaus/units) for Eigen vectors in a quantity (Au) or of quantities (nholthaus/units). The unit of their result is only the product of the units of the operands, so dot(F, d) and cross(r, F) are both in N m, and nothing tells work from a moment of force. Python’s Pint provides such operations through NumPy, with the same result units.

The usual workaround is a vector of quantities. The linear algebra library then computes the products, as it needs only arithmetic on the elements (la stands for any linear algebra library):

la::vector<quantity<si::newton>> force = {1. * N, 2. * N, 3. * N};
la::vector<quantity<si::metre>> displacement = {3. * m, 2. * m, 1. * m};
quantity<si::joule> work = la::scalar_product(force, displacement);

The units library sees only the multiplications of the scalar components. It can’t tell a scalar product from a vector product, can’t check the order of the operands of a vector product, and knows nothing of the character of the result. The result above is a quantity in joules, which the magnitude of a moment of force, or a product of two scalars that happens to have the same dimension, would be as well. Such a design can’t be quantity-safe. The operations have to be known to the units library, on quantities and on quantity specifications, which is what this paper proposes:

quantity force = isq::force(vec{1., 2., 3.} * N);
quantity displacement = isq::displacement(vec{3., 2., 1.} * m);
quantity<isq::mechanical_work[J]> work = scalar_product(force, displacement);

[ISO/IEC 80000] defines mechanical work as a scalar product and moment of force as a vector product (items 4-28.4 and 4-12.1 of part 4). Their units are the same (N m, or J for the work), and only the operation tells them apart. A library with one operator* for both would give the two quantity equations the same expression, and a moment of force would convert implicitly to mechanical work.

[P3045R11] avoids that by refusing the plain product of two quantities that are declared vectors or tensors:

// auto q1 = force * displacement;  // Compile-time error in P3045R11

It also proposes magnitude() and the quantity specification magnitude(QS) of its result (see Relation to P3045R11). Every other operation is left out, so a quantity typed as a vector has no way to become a mechanical work, a moment of force, a mechanical power, or an angular momentum, and a complex quantity has no way to give its real part. A user has to drop to the representation and lose the type.

With [P3045R11] alone, a system of quantities can’t define the following quantities as [ISO/IEC 80000] defines them:

Quantity
Item
[ISO/IEC 80000] definition
With [P3045R11] alone
mechanical work 4-28.4 line integral of F⋅drF \cdot dr force * displacement is ill-formed
mechanical power 4-27 P=F⋅vP = F \cdot v force * velocity is ill-formed
moment of force 4-12.1 M=r×FM = r \times F position_vector * force is ill-formed
angular momentum 4-11 L=r×pL = r \times p position_vector * momentum is ill-formed
moment of inertia 4-7 L=JωL = J\omega angular_momentum / angular_velocity is ill-formed
torque 4-12.2 T=M⋅eQT = M \cdot e_Q magnitude(moment_of_force) has no sign
pressure 4-14.1 p=en⋅F/Ap = e_n \cdot F / A magnitude(force) / area counts the whole force
Poynting vector 6-34 S=E×HS = E \times H electric_field_strength * magnetic_field_strength is ill-formed
complex power 6-59 S=UI*S = U I^* no complex conjugate
reactive power 6-60 Q=Im⁡SQ = \operatorname{Im} S no imaginary part of a quantity specification

Declared without these equations, or with magnitudes in place of the vectors, the quantities compile, but they lose the checks or the meaning. A mechanical work declared only as a child of energy becomes any energy after an explicit conversion. A torque defined as magnitude(force) * length has no sign, and the magnitude of a force times any length, a height for example, converts to it implicitly. A pressure defined as magnitude(force) / area, the example of a quantity equation in [P3045R11], counts the whole force instead of its component normal to the surface.

vec in the examples is cartesian_vector<double> and tens is cartesian_tensor<double>, both from the utility library of [mp-units]. This paper doesn’t propose them. The examples use the ISQ definitions of [mp-units], with the changes listed in ISQ definitions.

4 Proposed design

4.1 The operations

The following operations are proposed for quantity specifications:

Operation
Operands
Result
scalar_product(a, b) two vectors or two tensors scalar 2-18.11, 2-18.25
vector_product(a, b) two vectors vector 2-18.12
tensor_product(a, b) two vectors tensor 2-18.21
inner_product(T, a) a tensor and a vector or a tensor the order of a 2-18.24, 2-18.23
inner_quotient(a, b) two vectors the tensor that maps b to a (see below)
unit_vector(a) a vector dimensionless vector 2-18.6
real(z), imag(z) a complex quantity real quantity of the order of z 2-15.2, 2-15.3
modulus(z) a complex scalar real scalar 2-15.4
conj(z) a complex quantity the complex conjugate of z 2-15.6
make_complex(re, im) two real quantities of one dimension complex quantity 2-15.2

The field of a product is complex when an operand is complex. The dimension of a product is the product of the dimensions of its operands, the dimension of inner_quotient(a, b) is their quotient, unit_vector(a) is dimensionless, and the other operations keep the dimension of their operand (of re for make_complex).

An operand that isn’t declared a scalar may be a quantity kind, which admits every character ([P3045R11] writes that a kind has no character of its own). A product operation, or inner_quotient, of kind operands keeps the operation and the order of its operands: vector_product(kind_of<isq::length>, kind_of<isq::force>) is a vector product of two kinds, and isn’t a kind. kind_of<> doesn’t change. real, imag, modulus, conj, and unit_vector of a kind, and make_complex of two kinds, give a kind (see Operations on quantities).

None of the operations takes a point, as a point has no products and no complex parts.

inner_quotient(a, b) has no item of its own in [ISO/IEC 80000]. It names the tensor T for which inner_product(T, b) is a, which is how part 4 defines moment of inertia (item 4-7 writes L=JωL = J\omega and calls it a tensor product; in the terms of part 2, it is an inner product). Many tensors map one given vector to another, so two quantities have no such quotient, and inner_quotient exists for quantity specifications only.

4.2 Quantity equations

An operation is one factor of a quantity equation, as a named quantity is. A named quantity may be defined with an operation, alone or inside a product:

inline constexpr struct mechanical_work : named_quantity_spec<energy, scalar_product(force, displacement)> {} mechanical_work;
inline constexpr struct mechanical_power : named_quantity_spec<power, scalar_product(force, velocity)> {} mechanical_power;
inline constexpr struct moment_of_force : named_quantity_spec<vector_product(position_vector, force)> {} moment_of_force;
inline constexpr struct angular_momentum : named_quantity_spec<vector_product(position_vector, momentum)> {} angular_momentum;
inline constexpr struct moment_of_inertia :
    named_quantity_spec<inner_quotient(angular_momentum, angular_velocity)> {} moment_of_inertia;

The character of such a quantity comes from the operation: mechanical work is a real scalar, moment of force a real vector, and moment of inertia a real tensor, and none of them needs a quantity_tensor_order argument.

4.3 Operations on quantities

The same functions, except inner_quotient, take quantities. The quantity specifications decide whether the operation exists and which quantity the result is, the representation types compute the value (see Customization points for representation types), and the unit of a product is the product of the units:

quantity force = isq::force(vec{1., 2., 3.} * N);
quantity displacement = isq::displacement(vec{3., 2., 1.} * m);
quantity lever = isq::position_vector(vec{0., 1., 0.} * m);

quantity<isq::mechanical_work[J]> work = scalar_product(force, displacement);       // 10 J
quantity<isq::moment_of_force[N * m], vec> moment = vector_product(lever, force);   // {3, 0, -1} m N

// auto q1 = force * displacement;                                                  // Compile-time error
// quantity<isq::mechanical_work[J]> q2 = vector_product(lever, force).magnitude(); // Compile-time error
// quantity<isq::moment_of_force[N * m], vec> q3 = vector_product(force, lever);    // Compile-time error

q2 is the magnitude of a vector product, which isn’t a scalar product (see The quantity of a result). q3 has its operands in the wrong order. Neither converts, even with quantity_cast.

The result of scalar_product(force, displacement) is a quantity<scalar_product(isq::force, isq::displacement)[N * m], double>. It is a quantity of its own, which converts implicitly to isq::mechanical_work, as the operation is the equation of mechanical work.

The library takes two real scalar representations to be components along the same axis, so two real scalars have a scalar product, which is their plain product:

quantity w = scalar_product(isq::force(10. * N), isq::displacement(-5. * m));  // -50 m N

They have no vector product, as a scalar representation can’t hold a result perpendicular to that axis.

A product operation on quantity kinds keeps the operation and the order of its operands (see The operations), and its result converts implicitly to a quantity defined with the same operation on quantities of those kinds:

quantity sp = scalar_product(vec{1., 2., 3.} * N, vec{3., 2., 1.} * m);   // 10 m N
quantity vp = vector_product(vec{3., 2., 1.} * m, vec{1., 2., 3.} * N);   // {4, -8, 4} m N

quantity<isq::mechanical_work[J]> work3 = sp;                             // OK
quantity<isq::moment_of_force[N * m], vec> moment2 = vp;                  // OK
// quantity<isq::moment_of_force[N * m], vec> q5 = vector_product(vec{1., 2., 3.} * N, vec{3., 2., 1.} * m);  // Compile-time error

q5 has its operands in the wrong order and doesn’t convert even with quantity_cast. sp converts to isq::energy explicitly, and never to isq::moment_of_force or isq::torque. sp, vp, and the result of q5 convert implicitly to a quantity of N * m, the kind of the plain product, and a plain * of kinds doesn’t change: 5. * N * (2. * m) is a quantity<N * m, double>, which converts implicitly to mechanical work, to moment of force, and to torque. The type of sp spells out the operation in its reference, which isn’t a bare unit, but a quantity prints only its unit, as in 10 m N.

4.4 The quantity of a result

The following rules decide how the results convert. They hold for quantity specifications and for quantities alike.

4.5 abs() and hypot() of a vector

[P3045R11] doesn’t propose abs() or hypot(). The paper that proposes them should make them scalar operations, like pow<>() and sqrt() in [P3045R11]: they are ill-formed for a quantity of a vector or a tensor specification, whatever its representation, as the size of a vector is its magnitude(). abs() and hypot() stay well-formed for a scalar and for a quantity kind, and [mp-units] implements this rule.

4.6 The parts of a complex quantity

[P3045R11] doesn’t let a complex quantity convert to a real one. The definition of a complex quantity names its real parts with the properties real_part<>, imaginary_part<>, and complex_modulus<>:

inline constexpr struct complex_power :
    named_quantity_spec<voltage_phasor * conj(electric_current_phasor), real_part<struct active_power>,
                        imaginary_part<struct reactive_power>, complex_modulus<struct apparent_power>> {} complex_power;
inline constexpr struct active_power : named_quantity_spec<power, inverse(period) * (instantaneous_power * time)> {} active_power;
inline constexpr struct apparent_power : named_quantity_spec<complex_power, quantity_field::real, non_negative> {} apparent_power;
inline constexpr struct reactive_power : named_quantity_spec<mass * pow<2>(length) / pow<3>(duration)> {} reactive_power;

[ISO/IEC 80000] part 6 defines complex power with the complex conjugate of the electric current phasor, S=UI*S = U I^* (item 6-59). conj(QS) is a quantity specification of its own, and conj(conj(QS)) is QS, so isq::voltage_phasor * conj(isq::electric_current_phasor) converts implicitly to isq::complex_power, and isq::voltage_phasor * isq::electric_current_phasor doesn’t convert to it, even with quantity_cast.

real(QS), imag(QS), and modulus(QS) of such a quantity are the quantities it names. A part is passed as a type, so it may be defined after the complex quantity and be its child, as apparent power is. It has to be complete before real(), imag(), or modulus() is called for the complex quantity, or before a result of make_complex() is converted to it. A child of a complex quantity has the parts of its parent.

quantity power = isq::complex_power(std::complex{800., 600.} * (V * A));

quantity<isq::active_power[W]> active = real(power);              // 800 W
quantity reactive = imag(power);                                  // 600 V A of isq::reactive_power
quantity<isq::apparent_power[V * A]> apparent = modulus(power);   // 1000 V A
quantity<isq::complex_power[V * A], std::complex<double>> rebuilt = make_complex(active, reactive);

A part is in the unit of the complex quantity. make_complex(re, im) takes the real part first and the imaginary part second, and its value is in the unit of re. Its result converts implicitly to a complex quantity that names quantities to which re and im convert implicitly as its real and its imaginary part. make_complex(reactive, active) doesn’t become a complex power, even explicitly.

For a complex quantity that names no parts, real(QS) is a quantity specification of its own, spelled real(QS), and the same holds for imag(QS) and modulus(QS). conj(q) of a quantity of QS is a quantity of conj(QS), and conj of a quantity kind is the kind.

4.7 Unit vectors

[ISO/IEC 80000] part 2 defines the unit vector in the direction of a vector aa as ea=a/|a|e_a = a/|a|, for a≠0a \neq 0 (item 2-18.6). unit_vector(QS) is a dimensionless vector, real or complex as QS is, with the quantity equation QS / magnitude(QS). It converts implicitly to QS / magnitude(QS), and the reverse is explicit, as the quotient isn’t known to have a magnitude of one. Its operand has to admit a vector, as a tensor has no unit vector. unit_vector of a quantity kind is kind_of<dimensionless>.

unit_vector(q) computes the unit vector of a quantity through its representation (see Customization points for representation types) and gives a quantity of unit_vector(QS) in one. A zero vector has no direction, so a non-zero q is a precondition, as a non-zero divisor is for a division. The representation has to be a type for which treat_as_floating_point is true.

The magnitude of a unit vector is one, which the type can’t check, so the value is checked where it enters:

Part 4 uses unit vectors in two definitions. Torque is T=M⋅eQT = M \cdot e_Q, the scalar product of the moment of force and the unit vector eQe_Q of the direction with respect to which the torque is considered (item 4-12.2), so a torque is signed. Pressure is the quotient of the component of a force normal to a surface and its area, written with the unit vector ene_n of the surface normal (item 4-14.1). [mp-units] names the two directions isq::torque_direction and isq::surface_normal_direction (see ISQ definitions). With moment of Operations on quantities:

quantity axis = unit_vector(isq::displacement(vec{0., 0., 2.} * m));                         // {0, 0, 1}
quantity<isq::torque[N * m]> torque = scalar_product(moment, axis);                          // -1 m N

quantity load = isq::force(vec{3., 0., -10.} * N);
quantity normal = vec{0., 0., -1.} * isq::surface_normal_direction[one];                     // into the surface
quantity<isq::pressure[Pa]> pressure = scalar_product(load, normal) / isq::area(2. * m2);    // 5 Pa

The normal of the example points into the surface, along the load. [ISO/IEC 80000] doesn’t fix the orientation of ene_n, so the sign of the pressure follows the choice of the user.

A torque isn’t a magnitude. magnitude(isq::moment_of_force) and isq::torque convert to each other only explicitly. magnitude(isq::force) / isq::area converts to isq::pressure only explicitly, and isq::force / isq::area doesn’t convert to it.

The magnitude of a unit vector is checked only where a value enters a unit-vector specification, so only a quantity typed as a unit vector makes a torque implicitly:

quantity d = isq::displacement(vec{0., 0., 7.} * m);
quantity<isq::torque[N * m]> t1 = scalar_product(moment, unit_vector(d));                    // -1 m N
// quantity<isq::torque[N * m]> t2 = scalar_product(moment, vec{0., 0., 5.} * one);         // Compile-time error
// quantity<isq::torque[N * m]> t3 = scalar_product(moment, d / d.magnitude());             // Compile-time error
quantity<isq::torque[N * m]> t4(scalar_product(moment, vec{0., 0., 5.} * one));             // -5 m N

t2 and t3 convert only explicitly. That conversion is of the result, so it checks no magnitude, and t4 is five times too large. The unit vector of a quantity kind, such as unit_vector(vec{0., 0., 2.} * m), is a kind_of<dimensionless>, so it makes a torque only explicitly as well.

4.8 ISQ definitions

[mp-units] defines the ISQ with these operations. Besides the definitions in Quantity equations (items 4-28.4, 4-27, 4-12.1, 4-11, and 4-7 of [ISO/IEC 80000] part 4) and the definition of isq::complex_power with conj in The parts of a complex quantity, the following show the other patterns:

inline constexpr struct Poynting_vector :
    named_quantity_spec<vector_product(electric_field_strength, magnetic_field_strength)> {} Poynting_vector;
inline constexpr struct impedance :
    named_quantity_spec<electric_potential / electric_current, quantity_field::complex,
                        real_part<struct resistance_to_alternating_current>, imaginary_part<struct reactance>,
                        complex_modulus<struct apparent_impedance>> {} impedance;

Before this change, [mp-units] defined such quantities with operator* and operator/ and an explicit quantity_tensor_order, which made, for example, a moment of force convert to mechanical work.

Torque and pressure follow items 4-12.2 and 4-14.1 of [ISO/IEC 80000] part 4 (see Unit vectors). Their directions, isq::torque_direction (eQe_Q) and isq::surface_normal_direction (ene_n), are new, and their names are invented, as [ISO/IEC 80000] has no item for them:

inline constexpr struct torque_direction : named_quantity_spec<dimensionless, unit_vector(displacement)> {} torque_direction;
inline constexpr struct torque : named_quantity_spec<scalar_product(moment_of_force, torque_direction)> {} torque;
inline constexpr struct surface_normal_direction :
    named_quantity_spec<dimensionless, unit_vector(displacement)> {} surface_normal_direction;
inline constexpr struct pressure : named_quantity_spec<scalar_product(force, surface_normal_direction) / area> {} pressure;

The changes to the ISQ of [mp-units] that the examples rely on are the definitions of this section and of Quantity equations, the parts named by the complex quantities, and the two directions.

4.9 Customization points for representation types

A quantity computes an operation through its representation types. For the products, the conjugate, and the unit vector, the library looks up a function under the name of [ISO/IEC 80000] or, for the scalar and the vector product, under the name that linear algebra libraries use. The first one found is used:

A free function is found by argument-dependent lookup only. real, imag, and modulus of a quantity use math::real, math::imag, and math::modulus of [P3045R11] for its representation.

These are exposition-only helpers, not new customization point objects. Generic code multiplies two representations through the quantities that hold them, and a function of a quantity is found by argument-dependent lookup. Open questions asks whether they should be public.

A quantity operation exists only if the quantity specifications have it, the representation helper can be called for the two representations, and its result, with representation_canonical_type of [P3045R11] applied, satisfies representation_of for the specification of the result. The quantity specifications decide first: two double quantities of isq::mass and isq::force have no scalar product. Then the representations decide: two double quantities of isq::position_vector and isq::force have no vector product.

4.10 Proposed interface

The declarations below are additions to the synopsis of [P3045R11]. EquationSpec of [P3045R11], which holds for a derived quantity specification and for magnitude-t<V>, holds for each exposition-only specification below as well.

namespace std {

// quantity specification properties
template<typename Q>
struct real-part-t {};             // exposition only
template<typename Q>
struct imaginary-part-t {};        // exposition only
template<typename Q>
struct complex-modulus-t {};       // exposition only

template<typename Q>
constexpr real-part-t<Q> real_part{};
template<typename Q>
constexpr imaginary-part-t<Q> imaginary_part{};
template<typename Q>
constexpr complex-modulus-t<Q> complex_modulus{};

// the specifications of the operations (also of a product operation of quantity kinds; the other operations of a kind give a kind)
template<quantity_spec L, quantity_spec R> struct scalar-product-t : quantity-spec-interface {};  // exposition only
template<quantity_spec L, quantity_spec R> struct vector-product-t : quantity-spec-interface {};  // exposition only
template<quantity_spec L, quantity_spec R> struct tensor-product-t : quantity-spec-interface {};  // exposition only
template<quantity_spec L, quantity_spec R> struct inner-product-t : quantity-spec-interface {};   // exposition only
template<quantity_spec L, quantity_spec R> struct inner-quotient-t : quantity-spec-interface {};  // exposition only
template<quantity_spec Q> struct real-t : quantity-spec-interface {};                              // exposition only
template<quantity_spec Q> struct imag-t : quantity-spec-interface {};                              // exposition only
template<quantity_spec Q> struct modulus-t : quantity-spec-interface {};                           // exposition only
template<quantity_spec Q> struct conj-t : quantity-spec-interface {};                              // exposition only
template<quantity_spec V> struct unit-vector-t : quantity-spec-interface {};                       // exposition only
template<quantity_spec Re, quantity_spec Im> struct complex-t : quantity-spec-interface {};        // exposition only

struct quantity-spec-interface {  // exposition only
  // ... as in P3045R11

  // ScalarProductOperands<L, R>: both may be vectors, or both may be tensors
  // VectorOperands<L, R>: both may be vectors; VectorOperand<Q>: Q may be a vector
  // InnerProductOperands<L, R>: L may be a tensor, R may be a vector or a tensor
  // ComplexOperand<Q>: Q may be complex; ComplexScalarOperand<Q>: Q may be a complex scalar
  // ComplexParts<Re, Im>: both may be real, their orders overlap, and their dimensions are the same
  // ("may be" is true for a quantity kind, which admits every character)

  template<quantity_spec Lhs, quantity_spec Rhs>
    requires(!PointQuantitySpec<Lhs>) && (!PointQuantitySpec<Rhs>) && ScalarProductOperands<Lhs, Rhs>
  friend consteval quantity_spec auto scalar_product(Lhs lhs, Rhs rhs);

  template<quantity_spec Lhs, quantity_spec Rhs>
    requires(!PointQuantitySpec<Lhs>) && (!PointQuantitySpec<Rhs>) && VectorOperands<Lhs, Rhs>
  friend consteval quantity_spec auto vector_product(Lhs lhs, Rhs rhs);

  template<quantity_spec Lhs, quantity_spec Rhs>
    requires(!PointQuantitySpec<Lhs>) && (!PointQuantitySpec<Rhs>) && VectorOperands<Lhs, Rhs>
  friend consteval quantity_spec auto tensor_product(Lhs lhs, Rhs rhs);

  template<quantity_spec Lhs, quantity_spec Rhs>
    requires(!PointQuantitySpec<Lhs>) && (!PointQuantitySpec<Rhs>) && InnerProductOperands<Lhs, Rhs>
  friend consteval quantity_spec auto inner_product(Lhs lhs, Rhs rhs);

  template<quantity_spec Lhs, quantity_spec Rhs>
    requires(!PointQuantitySpec<Lhs>) && (!PointQuantitySpec<Rhs>) && VectorOperands<Lhs, Rhs>
  friend consteval quantity_spec auto inner_quotient(Lhs lhs, Rhs rhs);

  template<quantity_spec Q>
    requires(!PointQuantitySpec<Q>) && ComplexOperand<Q>
  friend consteval quantity_spec auto real(Q q);

  template<quantity_spec Q>
    requires(!PointQuantitySpec<Q>) && ComplexOperand<Q>
  friend consteval quantity_spec auto imag(Q q);

  template<quantity_spec Q>
    requires(!PointQuantitySpec<Q>) && ComplexScalarOperand<Q>
  friend consteval quantity_spec auto modulus(Q q);

  template<quantity_spec Q>
    requires(!PointQuantitySpec<Q>) && ComplexOperand<Q>
  friend consteval quantity_spec auto conj(Q q);

  template<quantity_spec Q>
    requires(!PointQuantitySpec<Q>) && VectorOperand<Q>
  friend consteval quantity_spec auto unit_vector(Q q);

  template<quantity_spec Re, quantity_spec Im>
    requires(!PointQuantitySpec<Re>) && (!PointQuantitySpec<Im>) && ComplexParts<Re, Im>
  friend consteval quantity_spec auto make_complex(Re re, Im im);
};

struct quantity-interface {  // exposition only
  // ... as in P3045R11

  // ProductOf<Op, RepOp, R1, Rep1, R2, Rep2>: neither R1 nor R2 is a point reference, Op exists for
  // their quantity specifications, RepOp (an exposition-only helper of [Customization points for
  // representation types]) can be called for Rep1 and Rep2, and its result represents the result
  // of Op; PartOf<Op, RepOp, R, Rep> likewise for one operand; UnitVectorOf<R, Rep> likewise for
  // unit_vector and rep-unit-vector, and treat_as_floating_point<Rep> is true
  template<auto R1, typename Rep1, auto R2, typename Rep2>
    requires ProductOf<scalar-product, rep-scalar-product, R1, Rep1, R2, Rep2>
  friend constexpr delta_quantity auto scalar_product(const quantity<R1, Rep1>& lhs, const quantity<R2, Rep2>& rhs);

  template<auto R1, typename Rep1, auto R2, typename Rep2>
    requires ProductOf<vector-product, rep-vector-product, R1, Rep1, R2, Rep2>
  friend constexpr delta_quantity auto vector_product(const quantity<R1, Rep1>& lhs, const quantity<R2, Rep2>& rhs);

  template<auto R1, typename Rep1, auto R2, typename Rep2>
    requires ProductOf<tensor-product, rep-tensor-product, R1, Rep1, R2, Rep2>
  friend constexpr delta_quantity auto tensor_product(const quantity<R1, Rep1>& lhs, const quantity<R2, Rep2>& rhs);

  template<auto R1, typename Rep1, auto R2, typename Rep2>
    requires ProductOf<inner-product, rep-inner-product, R1, Rep1, R2, Rep2>
  friend constexpr delta_quantity auto inner_product(const quantity<R1, Rep1>& lhs, const quantity<R2, Rep2>& rhs);

  template<auto R, typename Rep>
    requires PartOf<real, math::real, R, Rep>
  friend constexpr delta_quantity auto real(const quantity<R, Rep>& q);

  template<auto R, typename Rep>
    requires PartOf<imag, math::imag, R, Rep>
  friend constexpr delta_quantity auto imag(const quantity<R, Rep>& q);

  template<auto R, typename Rep>
    requires PartOf<modulus, math::modulus, R, Rep>
  friend constexpr delta_quantity auto modulus(const quantity<R, Rep>& q);

  template<auto R, typename Rep>
    requires PartOf<conj, rep-conj, R, Rep>
  friend constexpr delta_quantity auto conj(const quantity<R, Rep>& q);

  // the result is in one
  // Preconditions: q isn't a zero vector
  template<auto R, typename Rep>
    requires UnitVectorOf<R, Rep>
  friend constexpr delta_quantity auto unit_vector(const quantity<R, Rep>& q);

  // hosted only; the result is in the unit of re and has the representation complex<common_type_t<Rep1, Rep2>>
  // Constraints: common_type_t<Rep1, Rep2> is a floating-point type
  template<auto R1, typename Rep1, auto R2, typename Rep2>
    requires ComplexOf<R1, Rep1, R2, Rep2>
  friend constexpr delta_quantity auto make_complex(const quantity<R1, Rep1>& re, const quantity<R2, Rep2>& im);
};

}

The result of scalar_product(q1, q2) has the quantity specification scalar_product(get_quantity_spec(R1), get_quantity_spec(R2)) and the unit get_unit(R1) * get_unit(R2), and likewise for the other products. The result of real(q), imag(q), modulus(q), and conj(q) has the unit of q.

5 Relation to P3045R11

Nothing that [P3045R11] proposes is removed or changes meaning, and its wording changes only where this paper extends EquationSpec and unit_of:

The ISQ definitions in ISQ definitions are examples. [P3045R11] doesn’t propose a system of quantities, so they belong to the paper that proposes one.

The examples use the spelling of [P3045R11]. Under [P4185R3], proposed in the same mailing, a plain QS[U] of a vector specification is ill-formed, a vector is written delta<QS>[U], and a vector of a quantity kind delta<U>(v).

[P4411R1] proposes non-negative quantity specifications. [mp-units] makes magnitude(QS) and modulus(QS) non-negative, and declares isq::apparent_power, isq::apparent_impedance, and isq::apparent_admittance non_negative. A scalar product and a real or an imaginary part are signed. If both papers are adopted, a revision of the later one will say so.

6 Implementation experience

The design is implemented in a branch of [mp-units] off its version 3 line, not yet public. The branch defines the ISQ of [mp-units] with the operations and has compile-time tests of every rule in The quantity of a result. The examples in this paper were compiled with GCC 15 against that branch, and the representation lookup was checked with a user vector type that provides only dot() and cross() member functions.

[mp-units quantity safety] describes the motivation for these operations in more detail.

7 Open questions

7.1 The directions of torque and pressure

isq::torque_direction and isq::surface_normal_direction (see ISQ definitions) are both defined as unit_vector(displacement), so the unit vectors of many vectors convert implicitly to either of them. For example, those of force, velocity, acceleration, momentum, and electric field strength do. Others convert only explicitly (for example, those of angular velocity, magnetic flux density, and magnetic moment), only with quantity_cast (for example, that of position vector), or not at all (for example, those of moment of force, angular momentum, and the Poynting vector). Should a direction be tied to a named vector quantity, such as the axis of rotation or the normal of a surface? [P4407R0] proposes typed components along declared axes, and may be the better home for the axis.

7.2 Conversions through a kind

A quantity of isq::moment_of_force converts implicitly to a quantity<N * m>, as every quantity converts to its kind, and that kind converts implicitly to isq::mechanical_work (see The quantity of a result). The two steps make a moment of force a mechanical work, which the direct conversion refuses even explicitly. A product operation on kinds keeps the operation, but N * m is the kind of a plain product. This paper doesn’t close that path.

The same path opens in other places. magnitude(isq::moment_of_force) converts implicitly to kind_of<isq::energy>, through the product of the magnitudes of its operands, and from there to isq::mechanical_work. A product operation converts only to a quantity whose equation names it, so another equation of a quantity reaches it only through the kind. isq::electric_charge * vector_product(isq::velocity, isq::magnetic_flux_density) (F=qv×BF = q v \times B, item 6-21 of [ISO/IEC 80000] part 6) doesn’t convert to isq::force, and vector_product(isq::magnetic_moment, isq::magnetic_flux_density) (τ=m×B\tau = m \times B) doesn’t convert to isq::moment_of_force, even with quantity_cast, while each converts implicitly to its kind and from there to the target. isq::torque * isq::rotational_frequency, which lacks the 2π2\pi of P=TωP = T\omega, doesn’t convert to isq::mechanical_power directly, but it converts implicitly to a quantity<W> and from there to isq::mechanical_power. A torque of 5 N m at isq::rotational_frequency(10. / s) gives 50 W, where the power is about 314 W. isq::torque * isq::angular_frequency, P=TωP = T\omega, converts to isq::power only explicitly.

A rule that a direct conversion is never stricter than the path through the kind would remove these differences. This paper doesn’t adopt such a rule, as it would make vector_product(isq::force, isq::position_vector) and scalar_product(isq::force, isq::displacement) convert implicitly to isq::moment_of_force and isq::torque, which they reach through their kinds as well, though Operations on quantities and The quantity of a result say that they never convert.

7.3 Power and energy of a rotation

W=TθW = T\theta and P=TωP = T\omega convert only explicitly (see The quantity of a result), as no named quantity has these equations. Should a system of quantities add a rotational work, power, and kinetic energy, so that these conversions become implicit?

7.4 The scalar product of complex operands

Item 2-18.11 of [ISO/IEC 80000] part 2 writes the scalar product as a⋅b=∑iaibia \cdot b = \sum_i a_i b_i, which is bilinear, and the vector items of part 2 don’t mention complex coordinates. scalar_product() of cartesian_vector of [mp-units] conjugates its first operand. The type of the result is the same either way, so for complex operands scalar_product(a, b) and scalar_product(b, a) may differ in value. Should the library require one of the two definitions from a representation?

7.5 Implicit conversions that aren’t transitive

scalar_product(isq::force, isq::displacement) converts implicitly to isq::mechanical_work, and isq::mechanical_work converts implicitly to its parent isq::energy, but the scalar product converts to isq::energy only explicitly. The engine compares it with the equation of isq::energy (a product of scalars), so it relaxes the scalar product to a product of magnitudes, which is an explicit step. The same holds for scalar_product(isq::force, isq::velocity) and isq::power. Also, isq::mass * pow<2>(isq::length) / pow<2>(isq::time) converts implicitly to isq::mechanical_energy, a child of isq::mechanical_work, but not to isq::mechanical_work. Should the conversion find the named quantity defined with the operation and go through it?

7.6 Comparing the two orders of a vector product

vector_product(lever, force) == -vector_product(force, lever) compiles and is true, while vector_product(lever, force) + vector_product(force, lever) doesn’t compile. The common quantity specification of the two is force * length, to which neither converts implicitly. For operands that are quantity kinds of m and N, as in vector_product(vec{3., 2., 1.} * m, vec{1., 2., 3.} * N), neither == nor + compiles: the kind of N is that of the scalar mass * length / pow<2>(time), and the two orders have no common quantity specification. Should the two orders of named operands have none either, so that they can’t be compared, or should the comparison stay because the negated value is the same physical vector?

7.7 Public customization point objects

Customization points for representation types specifies exposition-only helpers. Making them public objects in std::math, next to math::real and math::magnitude, would let generic code compute a scalar product of two representations that provide only member functions. Their names need a decision either way: inner_product is also an algorithm of <numeric>, and the fallbacks to dot and cross accept functions of those names that may mean something else for a user-defined type.

7.8 The names of the part properties

“Names that need a decision” of [P3045R11] lists renaming math::real, math::imag, and math::modulus to real_part, imag_part, and complex_modulus as one option. That would collide with the properties real_part<> and complex_modulus<> of this paper.

7.9 The representation of make_complex()

make_complex() always returns a complex<common_type_t<Rep1, Rep2>>, so it is available only in a hosted implementation, and a complex representation of another library can’t be the result. [P3045R11] requires a complex representation to be constructible from its real and its imaginary part, which could choose the result type instead. How the user would name that type isn’t settled.

7.10 Scalar products if absolute quantities are adopted

If absolute quantities [P4185R3] are adopted, a vector quantity that isn’t a point is a delta, and a product with a delta operand is a delta, so every scalar product of two vectors is a delta. For isq::mechanical_work, scalar_product(force, displacement), that is the intent: the work of a force is a change of energy. The ISQ definitions of this paper (see ISQ definitions) also make isq::mechanical_power scalar_product(force, velocity), isq::torque scalar_product(moment_of_force, torque_direction), and isq::pressure scalar_product(force, surface_normal_direction) / area, where each direction is a unit vector, a vector and so a delta. All three results are deltas. A pressure is a textbook absolute, though: absolute pressure is measured from vacuum, a true zero. A torque is signed relative to its axis, as an electric current is to its reference direction, and is an amount as well. With a force F, a unit normal n, and an area A:

quantity<isq::pressure[Pa]> p1 = scalar_product(F, n) / A;               // ill-formed: a delta
quantity<isq::pressure[Pa]> p2 = (scalar_product(F, n) / A).absolute();  // OK

The options are:

7.11 Operations left out

The tensor product of two tensors (a tensor of the fourth order, item 2-18.22 of [ISO/IEC 80000] part 2), the argument of a complex quantity (item 2-15.5), a free function magnitude(q) for quantities, and the member functions q.real(), q.imag(), and q.modulus() of quantity are not proposed.

8 Proposed polls

8.1 SG6

  1. SG6 wants the character-specific operations of this paper (scalar_product, vector_product, tensor_product, inner_product, inner_quotient, real, imag, modulus, conj, make_complex) on quantity specifications, with a result that records the operation and its operands.
  2. SG6 wants the same operations, except inner_quotient, on quantities.
  3. SG6 wants a complex quantity specification to name its real part, its imaginary part, and its modulus with real_part<>, imaginary_part<>, and complex_modulus<>.
  4. SG6 wants the vector product, the tensor product, and the inner product to give a different quantity for each order of their operands, with no conversion between the two, even explicit.
  5. SG6 wants the conversion between a scalar product and the product of the magnitudes of its operands to be explicit.
  6. SG6 wants a unit_vector operation on quantity specifications and on quantities, so that a system of quantities can define torque and pressure with a scalar product, as [ISO/IEC 80000] does.

9 Acknowledgements

Special thanks and recognition goes to The C++ Alliance for supporting Mateusz’s membership in the ISO C++ Committee and the production of this proposal.

10 References

[Au Eigen] Au: Eigen compatibility (au/compatibility/eigen.hh).
https://github.com/aurora-opensource/au/blob/main/au/compatibility/eigen.hh
[ISO/IEC 80000] ISO/IEC 80000: Quantities and units.
https://www.iso.org/standard/76921.html
[mp-units] mp-units - A Physical Quantities and Units library for C++.
https://mpusz.github.io/mp-units
[mp-units quantity safety] Mateusz Pusz. 2025. Bringing Quantity-Safety To The Next Level.
https://mpusz.github.io/mp-units/latest/blog/2025/01/15/bringing-quantity-safety-to-the-next-level
[nholthaus/units Eigen] nholthaus/units: Eigen interoperability (include/units/eigen.h).
https://github.com/nholthaus/units/blob/main/include/units/eigen.h
[P3045R11] Mateusz Pusz. Quantities and units library.
https://wg21.link/p3045r11
[P4185R3] Mateusz Pusz. Absolute Quantities.
https://wg21.link/p4185r3
[P4407R0] Mateusz Pusz. Component Vectors of Vector Quantities.
https://wg21.link/p4407r0
[P4411R1] Mateusz Pusz. Non-Negative Quantity Specifications.
https://wg21.link/p4411r1