Arithmetics with scalar

Issue #70 wontfix
Erik created an issue

Hi,

I would like to do arithmetics with a scalar b on a blaze matrix A, add a scalar:

A = A+b or A += b

and likewise for subtraction, division and multiplication.

Are these operations implemented? And in case they are, what is the syntax? Or what is your recommendation how to do the arithmetics?

Best regards, Erik

Comments (5)

  1. Klaus Iglberger

    Hi Erik!

    Both the multiplication and division of a vector or matrix with a scalar are available (see the according wiki page). Addition and subtraction with a scalar value have not been implemented yet and it is not clear yet if it will ever be available. However, we will leave the issue open as a reminder. Thanks a lot for the proposal,

    Best regards,

    Klaus!

  2. Klaus Iglberger

    Hi Erik!

    Thanks again for the proposal. We have discussed it in detail and finally decided to not generally support additions and subtractions of vectors and matrices with scalar values. From our point of view, the addition and subtraction of a vector or matrix and a scalar is mathematically not well defined and it is not intuitively clear what such an operation would do (especially if the vector or matrix is sparse).

    On the bright side, Blaze already provides all the features to enable you to add these operations with just a few lines of code. The following code snippets provide you with the implementation of the scalar addition and addition assignment operators for vectors and matrices:

    //** Operators for Vector-Scalar Additions ***************************************
    
    template< typename VT, bool TF, typename Scalar, typename = EnableIf_< IsNumeric<Scalar> > >
    auto operator+( const Vector<VT,TF>& vec, Scalar scalar )
    {
       return forEach( ~vec, [scalar=scalar]( const auto& value ){ return value + scalar; } );
    }
    
    template< typename Scalar, typename VT, bool TF, typename = EnableIf_< IsNumeric<Scalar> > >
    auto operator+( Scalar scalar, const Vector<VT,TF>& vec )
    {
       return forEach( ~vec, [scalar=scalar]( const auto& value ){ return value + scalar; } );
    }
    
    template< typename VT, bool TF, typename Scalar, typename = EnableIf_< IsNumeric<Scalar> > >
    VT& operator+=( Vector<VT,TF>& vec, Scalar scalar )
    {
       (~vec) = (~vec) + scalar;
       return ~vec;
    }
    
    //** Operators for Matrix-Scalar Additions ***************************************
    
    template< typename MT, bool SO, typename Scalar, typename = EnableIf_< IsNumeric<Scalar> > >
    auto operator+( const Matrix<MT,SO>& mat, Scalar scalar )
    {
       return forEach( ~mat, [scalar=scalar]( const auto& value ){ return value + scalar; } );
    }
    
    template< typename Scalar, typename MT, bool SO, typename = EnableIf_< IsNumeric<Scalar> > >
    auto operator+( Scalar scalar, const Matrix<MT,SO>& mat )
    {
       return forEach( ~mat, [scalar=scalar]( const auto& value ){ return value + scalar; } );
    }
    
    template< typename MT, bool SO, typename Scalar, typename = EnableIf_< IsNumeric<Scalar> > >
    MT& operator+=( Matrix<MT,SO>& mat, Scalar scalar )
    {
       (~mat) = (~mat) + scalar;
       return ~mat;
    }
    

    Please note that it is possible to use these operators with sparse vectors and sparse matrices, but in contrast to dense vectors and matrices only the non-zero entries are modified.

    We hope this helps,

    Best regards,

    Klaus!

  3. Log in to comment