Element-wise min and max functions

Issue #82 resolved
Mikhail Katliar created an issue

Implement min and max functions, so that the following expressions are valid:

max(v1, v2);   // element-wise maximum of two vectors
max(v, scalar);   // element-wise maximum of a vector and a scalar
max(scalar, v);   // element-wise maximum of a vector and a scalar
max(m1, m2);   // element-wise maximum of two matrices
max(m, scalar);   // element-wise minimum of a matrix and a scalar
max(scalar, m);   // element-wise minimum of a matrix and a scalar
// and the same for min()

Comments (5)

  1. Klaus Iglberger

    Hi Mikhail!

    Thanks a lot for the proposal. We agree that an element-wise min() and max() is a desirable feature. We will try our best to provide the feature as quickly as possible.

    Best regards,

    Klaus!

  2. snow-abstraction

    Great! I just started trying out Blaze with a new little hobby project where I wanted max(min(u, x), l) where u, x and l are vectors.

    I was going to switch to Eigen since it has this functionality, but I guess I'll iterate manually for now and wait for this.

  3. Klaus Iglberger

    Summary

    The feature has been implemented, tested, optimized (including parallelization) and documented as required. It is immediately available via cloning the Blaze repository and will be officially released in Blaze 3.2.

    min() / max()

    The min() and max() functions can be used for a single vector or multiple vectors. If passed a single vector, the functions return the smallest and largest element of the given dense or sparse vector, respectively:

    blaze::StaticVector<int,4UL,rowVector> a{ -5, 2,  7, -4 };
    
    min( a );  // Returns -5
    max( a );  // Returns 7
    

    In case the vector currently has a size of 0, both functions return 0. Additionally, in case a given sparse vector is not completely filled, the zero elements are taken into account. For example, the following compressed vector has only two non-zero elements. However, the minimum of this vector is 0:

    blaze::CompressedVector<int> b( 4UL, 2UL );
    b[0] = 1;
    b[2] = 3;
    
    min( b );  // Returns 0
    

    If passed two or more dense vectors, the min() and max() functions compute the componentwise minimum or maximum of the given vectors, respectively:

    blaze::StaticVector<int,4UL,rowVector> c{ -5, 1, -7, 4 };
    blaze::StaticVector<int,4UL,rowVector> d{ -5, 3,  0, 2 };
    
    min( a, c );     // Results in the vector ( -5, 1, -7, -4 )
    max( a, c, d );  // Results in the vector ( -5, 3,  7,  4 )
    

    Please note that sparse vectors can only be used in the unary min() and max() functions. Also note that all forms of the min() and max() functions can be used to compute the smallest and largest element of a vector expression:

    min( a + b + c );  // Returns -9, i.e. the smallest value of the resulting vector
    max( a - b - c );  // Returns 11, i.e. the largest value of the resulting vector
    
    min( a + c, c - d );  // Results in ( -10 -2 -7 0 )
    max( a - c, c + d );  // Results in ( 0 4 14 6 )
    
  4. Log in to comment