53#if defined __has_include
54#if __has_include(<Eigen/Core>)
70 template<units::UnitType U>
71 struct NumTraits<U> : NumTraits<typename units::traits::unit_traits<U>::underlying_type>
73 using T =
typename units::traits::unit_traits<U>::underlying_type;
82 IsInteger = NumTraits<T>::IsInteger,
83 IsSigned = NumTraits<T>::IsSigned,
84 RequireInitialization = 1,
98 template<units::UnitType U,
class X>
99 struct ScalarBinaryOpTraits<U, X, internal::scalar_product_op<U, X>>
101 using ReturnType = U;
109 template<
class X, units::UnitType U>
110 struct ScalarBinaryOpTraits<X, U, internal::scalar_product_op<X, U>>
112 using ReturnType = U;
124 template<units::UnitType U,
class X>
125 struct ScalarBinaryOpTraits<U, X, internal::scalar_quotient_op<U, X>>
127 using ReturnType = U;
144 template<
class DerivedA,
class DerivedB>
145 auto unit_dot(
const Eigen::MatrixBase<DerivedA>& lhs,
const Eigen::MatrixBase<DerivedB>& rhs)
147 using UnitA =
typename DerivedA::Scalar;
148 using UnitB =
typename DerivedB::Scalar;
149 using Product =
decltype(std::declval<UnitA>() * std::declval<UnitB>());
151 Product accumulator = Product(0);
152 for (Eigen::Index i = 0; i < lhs.size(); ++i)
153 accumulator += lhs(i) * rhs(i);
164 template<
class Derived>
165 auto unit_squared_norm(
const Eigen::MatrixBase<Derived>& v)
167 return unit_dot(v, v);
180 template<
class Derived>
181 auto unit_norm(
const Eigen::MatrixBase<Derived>& v)
183 using Unit =
typename Derived::Scalar;
184 using Underlying =
typename traits::unit_traits<Unit>::underlying_type;
186 Underlying accumulator = Underlying(0);
187 for (Eigen::Index i = 0; i < v.size(); ++i)
189 const Underlying raw = v(i).template to<Underlying>();
190 accumulator += raw * raw;
192 return Unit(std::sqrt(accumulator));
203 template<
class Derived>
204 auto unit_normalized(
const Eigen::MatrixBase<Derived>& v)
206 using Unit =
typename Derived::Scalar;
207 using Underlying =
typename traits::unit_traits<Unit>::underlying_type;
209 Underlying accumulator = Underlying(0);
210 for (Eigen::Index i = 0; i < v.size(); ++i)
212 const Underlying raw = v(i).template to<Underlying>();
213 accumulator += raw * raw;
215 const Underlying magnitude = std::sqrt(accumulator);
217 Eigen::Matrix<Underlying, Derived::RowsAtCompileTime, 1> direction;
218 for (Eigen::Index i = 0; i < v.size(); ++i)
219 direction(i) = v(i).template to<Underlying>() / magnitude;
234 template<
class DerivedA,
class DerivedB>
235 auto unit_cross(
const Eigen::MatrixBase<DerivedA>& lhs,
const Eigen::MatrixBase<DerivedB>& rhs)
237 using UnitA =
typename DerivedA::Scalar;
238 using UnitB =
typename DerivedB::Scalar;
239 using Product =
decltype(std::declval<UnitA>() * std::declval<UnitB>());
241 Eigen::Matrix<Product, 3, 1> result;
242 result(0) = lhs(1) * rhs(2) - lhs(2) * rhs(1);
243 result(1) = lhs(2) * rhs(0) - lhs(0) * rhs(2);
244 result(2) = lhs(0) * rhs(1) - lhs(1) * rhs(0);
261 template<
class MatrixDerived,
class VectorDerived>
262 auto unit_transform(
const Eigen::MatrixBase<MatrixDerived>& matrix,
const Eigen::MatrixBase<VectorDerived>& vector)
264 using Unit =
typename VectorDerived::Scalar;
265 using Underlying =
typename traits::unit_traits<Unit>::underlying_type;
267 Eigen::Matrix<Underlying, VectorDerived::RowsAtCompileTime, 1> raw;
268 for (Eigen::Index i = 0; i < vector.size(); ++i)
269 raw(i) = vector(i).template to<Underlying>();
271 const Eigen::Matrix<Underlying, MatrixDerived::RowsAtCompileTime, 1> product = matrix * raw;
273 Eigen::Matrix<Unit, MatrixDerived::RowsAtCompileTime, 1> result;
274 for (Eigen::Index i = 0; i < product.size(); ++i)
275 result(i) = Unit(product(i));
unit, dimensional analysis, generic cmath functions, traits (not dimension-specific),...
Unit Conversion Library namespace.
Definition units.h:106