Skip to content

mp_units::tensor_order

Customization point: the tensor order of a representation type, 0 for a scalar, 1 for a vector, and 2 for a tensor

Synopsis

Declared in <mp-units/framework/customization_points.h>

template<typename T>
constexpr utility::unspecified_t tensor_order;

Description

The order is detected from the element access a type provides. t(i, j) or t[i, j] is order 2, t[i] is order 1, and neither is order 0. A type that provides both, as an N×1 matrix does, has no order, and isn't a representation, until it is specialized. Only its compile-time extents tell a vector from a matrix, so a specialization for such a type reads them:

// an Eigen column vector is an N×1 matrix, which provides both `v[i]` and `v(i, j)`
template<typename T>
  requires requires { typename T::PlainObject; } && std::derived_from<T, Eigen::EigenBase<T>>
constexpr std::size_t mp_units::tensor_order<T> =
  (T::RowsAtCompileTime == 1 || T::ColsAtCompileTime == 1) ? 1 : 2;

A scalar class that provides operator[], as one giving access to its digits might, is specialized to 0 the same way.

Generic code reads it to handle a vector's components:

template<Quantity Q>
  requires(tensor_order<typename Q::rep> == 1)
void print(std::string_view name, const Q& q)
{
  const auto& components = q.numerical_value_in(q.unit);
  std::cout << name << " = [" << components[0] << ", " << components[1] << ", "
            << components[2] << "] " << unit_symbol(Q::unit) << "\n";
}

Template Parameters

NameDescription
Ta representation type

See Also