Clone wiki

blaze / Addition

The addition of vectors and matrices is as intuitive as the addition of scalar values. For both the vector addition as well as the matrix addition the addition operator can be used. It even enables the addition of dense and sparse vectors as well as the addition of dense and sparse matrices:

blaze::DynamicVector<int> v1( 5UL ), v3;
blaze::CompressedVector<float> v2( 5UL );

// ... Initializing the vectors

v3 = v1 + v2;  // Addition of a two column vectors of different data type
blaze::DynamicMatrix<float,rowMajor> M1( 7UL, 3UL );
blaze::CompressedMatrix<size_t,columnMajor> M2( 7UL, 3UL ), M3;

// ... Initializing the matrices

M3 = M1 + M2;  // Addition of a row-major and a column-major matrix of different data type

Note that it is necessary that both operands have exactly the same dimensions. Violating this precondition results in an exception. Also note that in case of vectors it is only possible to add vectors with the same transpose flag:

blaze::DynamicVector<int,columnVector> v1( 5UL );
blaze::CompressedVector<float,rowVector> v2( 5UL );

v1 + v2;           // Compilation error: Cannot add a column vector and a row vector
v1 + trans( v2 );  // OK: Addition of two column vectors

In case of matrices, however, it is possible to add row-major and column-major matrices. Note however that in favor of performance the addition of two matrices with the same storage order is favorable. The same argument holds for the element type: In case two vectors or matrices with the same element type are added, the performance can be much higher due to vectorization of the operation.

blaze::DynamicVector<double>v1( 100UL ), v2( 100UL ), v3;

// ... Initialization of the vectors

v3 = v1 + v2;  // Vectorized addition of two double precision vectors
blaze::DynamicMatrix<float> M1( 50UL, 70UL ), M2( 50UL, 70UL ), M3;

// ... Initialization of the matrices

M3 = M1 + M2;  // Vectorized addition of two row-major, single precision dense matrices

Previous: Arithmetic Operations ---- Next: Subtraction