operator%, mod and fmod
Currently, blaze does not support the integral modulus ( %
) and floating point modulus (fmod: v1 % v2 = v1 - v2 * trunc(v1 / v2
). Furthermore, the common modulus operator % is used for the cross product instead. Could the latter still be revised?
Comments (3)
-
reporter -
reporter Example code snippet for scalars, dense vectors and matrices:
//------------------------------------------------------------------------- // Mod //------------------------------------------------------------------------- template< std::is_floating_point ScalarT > [[nodiscard]] inline ScalarT Mod(ScalarT s1, ScalarT s2) noexcept { return s1 - s2 * std::trunc(s1 / s2); } template< std::integral ScalarT > [[nodiscard]] constexpr ScalarT Mod(ScalarT s1, ScalarT s2) noexcept { return s1 - s2 * (s1 / s2); } template< FloatDenseVector VectorT > [[nodiscard]] inline VectorT Mod(const VectorT& v1, const VectorT& v2) noexcept { return v1 - v2 * blaze::trunc(v1 / v2); } template< IntegralDenseVector VectorT > [[nodiscard]] inline VectorT Mod(const VectorT& v1, const VectorT& v2) noexcept { return v1 - v2 * (v1 / v2); } template< FloatDenseVector VectorT > [[nodiscard]] inline VectorT Mod(const VectorT& v, typename VectorT::ElementType s) noexcept { return v - s * blaze::trunc(v / s); } template< IntegralDenseVector VectorT > [[nodiscard]] inline VectorT Mod(const VectorT& v, typename VectorT::ElementType s) noexcept { return v - s * (v / s); } template< FloatDenseMatrix MatrixT > [[nodiscard]] inline MatrixT Mod(const MatrixT& m1, const MatrixT& m2) noexcept { return m1 - m2 * blaze::trunc(m1 / m2); } template< IntegralDenseMatrix MatrixT > [[nodiscard]] inline MatrixT Mod(const MatrixT& m1, const MatrixT& m2) noexcept { return m1 - m2 * (m1 / m2); } template< FloatDenseMatrix MatrixT > [[nodiscard]] inline MatrixT Mod(const MatrixT& m, typename MatrixT::ElementType s) noexcept { return m - s * blaze::trunc(m / s); } template< IntegralDenseMatrix MatrixT > [[nodiscard]] inline MatrixT Mod(const MatrixT& m, typename MatrixT::ElementType s) noexcept { return m - s * (m / s); } //------------------------------------------------------------------------- // Frac //------------------------------------------------------------------------- template< std::is_floating_point ScalarT > [[nodiscard]] inline ScalarT Frac(const ScalarT& s) noexcept { return s - std::trunc(s); } template< FloatDenseVector VectorT > [[nodiscard]] inline VectorT Frac(const VectorT& v) noexcept { return v - blaze::trunc(v); } template< FloatDenseMatrix MatrixT > [[nodiscard]] inline MatrixT Frac(const MatrixT& m) noexcept { return m - blaze::trunc(m); }
-
Hi Matthias!
Thanks a lot for proposing this feature and for providing the sample code. Both
mod
andfmod
sound like reasonable extensions and we’ll definitely consider them for the next releases of Blaze. Unfortunately it will not be possible to change the meaning ofoperator%
for both vectors (cross product) and matrices (Schur product) as this would be a breaking change. Also, these operations are required more frequently and it is desirable to be able to use infix notation for them. However, the namesmod
andfmod
should also be applicable for vectors and matrices. Thanks again,Best regards,
Klaus!
- Log in to comment
Related enhancement:
frac: v - trunc(v)