operator%, mod and fmod

Issue #312 new
Matthias Moulin created an issue

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)

  1. Matthias Moulin 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);
        }
    

  2. Klaus Iglberger

    Hi Matthias!

    Thanks a lot for proposing this feature and for providing the sample code. Both mod and fmod 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 of operator% 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 names mod and fmod should also be applicable for vectors and matrices. Thanks again,

    Best regards,

    Klaus!

  3. Log in to comment