rowwise `norm` and `argsort`?

Issue #376 resolved
Ecolss Logan created an issue

I have 2 noob questions, and can’t seem to find answers,

  1. From the wiki, there is a [norm ](https://www.fburl.com/ss6grkx5) function, but looks like it cannot compute rowwise or columnwise norm of a matrix. Although I can do a elementwise multiplication and then sum in rowwise or columnwise, do we have a better way?
  2. By argsort, I want to get the indices of elements in each row of a matrix, in a descending order for example. What’s the best approach to do so, given consideration of padding or alignment in .data()?

Comments (4)

  1. Klaus Iglberger

    Hi Ecolss!

    From the wiki, there is a [norm ](https://www.fburl.com/ss6grkx5) function, but looks like it cannot compute rowwise or columnwise norm of a matrix. Although I can do a elementwise multiplication and then sum in rowwise or columnwise, do we have a better way?

    Although it appears like an obvious feature, unfortunately we don’t provide row-/column-wise norm computations yet. You are the first user to ask for this feature. However, Blaze provides everything to implement these functions yourself. In the following you’ll find the according wrapper functions for the l1Norm(), l2Norm(), l3Norm(), l4Norm() and maxNorm().

    namespace blaze {
    
    template< ReductionFlag RF, typename MT, bool SO >
    decltype(auto) l1Norm( const Matrix<MT,SO>& mat )
    {
       return abs( sum<RF>( *mat ) );
    }
    
    template< ReductionFlag RF, typename MT, bool SO >
    decltype(auto) l2Norm( const Matrix<MT,SO>& mat )
    {
       return sqrt( sum<RF>( map( *mat, SqrAbs{} ) ) );
    }
    
    template< ReductionFlag RF, typename MT, bool SO >
    decltype(auto) l3Norm( const Matrix<MT,SO>& mat )
    {
       return cbrt( sum<RF>( abs( *mat % *mat % *mat ) ) );
    }
    
    template< ReductionFlag RF, typename MT, bool SO >
    decltype(auto) l4Norm( const Matrix<MT,SO>& mat )
    {
       return qdrt( sum<RF>( map( *mat % *mat, SqrAbs{} ) ) );
    }
    
    template< ReductionFlag RF, typename MT, bool SO >
    decltype(auto) maxNorm( const Matrix<MT,SO>& mat )
    {
       return max<RF>( abs( *mat ) );
    }
    
    } // namespace blaze
    
    blaze::DynamicMatrix<double> A( 4UL, 4UL );
    // ... Initialization of A
    blaze::DynamicVector<double,columnVector> n( 4UL, 0.0 );
    
    n = l2Norm<rowwise>( A );  // Row-wise L2 norm computation
    

    By argsort, I want to get the indices of elements in each row of a matrix, in a descending order for example. What’s the best approach to do so, given consideration of padding or alignment in .data()?

    So far the only “index” functions that Blaze provides are argmin() and argmax() for dense vectors. Also here you’re the first user to ask for this feature.

    Could you please give an example what exactly you expect the argsort() function to do?

    Best regards,

    Klaus!

  2. Ecolss Logan reporter

    Hi Klaus,

    Thanks for the advice about how to implement rowwise norm, will try that.

    To answer your question about argsort(), I just want to be able to find, for example, the topK similar vectors to an input one, and the similarity score is computed by matrix/vector multiplication.

  3. Log in to comment