- edited description
Matrix/vector assignment without resizing
When DynamicVector
s 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)
-
reporter -
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!
-
reporter Maybe
noresize()
is a better name. -
Hi Mikhail!
Agreed,
noresize()
is a better name thanfull()
. We have not forgotten this issue and still have it on our agenda.Best regards,
Klaus!
-
-
assigned issue to
-
assigned issue to
-
- changed status to open
-
- changed status to resolved
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
, andCompressedVector
and resizable matrices such asDynamicMatrix
,HybridMatrix
, andCompressedMatrix
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 astd::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!
- Log in to comment