Matrix/vector assignment without resizing

Issue #93 resolved
Mikhail Katliar created an issue

When DynamicVectors are assigned to each other, the one on the left is automatically resized. For example, the following code works:

using namespace blaze;
DynamicVector<double> a({1., 2., 3.});
DynamicVector<double> b({1., 2., 3., 4.});
a = b;

However, it is sometimes necessary to make sure that the vector/matrix sizes on both sides of the assignment are the same. For this purpose, one could write

subvector(a, 0, size(a)) = b;
submatrix(m, 0, 0, rows(m), columns(m)) = m1;

To make the code above shorter and cleaner, it would be convenient to have a function returning a Submatrix/Subvector corresponding to the entire vector, so that the code would look like

full(a) = b;
full(m) = m1;

Comments (7)

  1. Klaus Iglberger

    Hi Mikhail!

    This is an interesting issue that we have not considered before. Thanks for the proposal, we'll definitely consider the idea.

    Best regards,

    Klaus!

  2. Klaus Iglberger

    Hi Mikhail!

    Agreed, noresize() is a better name than full(). We have not forgotten this issue and still have it on our agenda.

    Best regards,

    Klaus!

  3. Klaus Iglberger

    Summary

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

    The fix() function

    By default, resizable vectors such as DynamicVector, HybridVector, and CompressedVector and resizable matrices such as DynamicMatrix, HybridMatrix, and CompressedMatrix can adapt their size during an assignment:

    blaze::DynamicVector<int> a{ 1, 2 };        // Setup of a vector with 2 elements
    blaze::DynamicVector<int> b{ 1, 2, 3, 4 };  // Setup of a vector with 4 elements
    
    a = b;  // Resizes vector 'a' to 4 elements
    
    blaze::DynamicMatrix<int> A{ { 1, 2 } };                   // Setup of a 1x2 matrix
    blaze::DynamicVector<int> B{ { 1, 2, 3 }, { 4, 5, 6, } };  // Setup of a 2x3 matrix
    
    A = B;  // Resizes matrix 'A' to a 2x3 matrix
    

    Via the fix() operation it is possible to fix the size of a resizable vector or matrix. If a vector or matrix with a different size is assigned, instead of resizing the vector or matrix the operation fails by throwing a std::invalid_argument exception:

    blaze::DynamicVector<int> a{ 1, 2 };        // Setup of a vector with 2 elements
    blaze::DynamicVector<int> b{ 1, 2, 3, 4 };  // Setup of a vector with 4 elements
    
    fix( a ) = b;  // Throws an exception: Vector cannot be resized!
    
    blaze::DynamicMatrix<int> A{ { 1, 2 } };                   // Setup of a 1x2 matrix
    blaze::DynamicVector<int> B{ { 1, 2, 3 }, { 4, 5, 6, } };  // Setup of a 2x3 matrix
    
    fix( A ) = B;  // Throws an exception: Matrix cannot be resized!
    
  4. Log in to comment