Blaze 3.9
Addition


Vector/Vector Addition


The addition of vectors is as intuitive as the addition of scalar values. For the addition of any two vectors the addition operator (i.e. operator+()) can be used. It even enables the addition of dense and sparse vectors:

// ... Initializing the vectors
v3 = v1 + v2; // Addition of a dense and a sparse column vector of different data type
v3 = add( v1, v2 ); // Alternative syntax
Efficient implementation of an arbitrary sized sparse vector.
Definition: CompressedVector.h:220
Efficient implementation of an arbitrary sized vector.
Definition: DynamicVector.h:223
BLAZE_ALWAYS_INLINE constexpr decltype(auto) add(T1 &&lhs, T2 &&rhs) noexcept(noexcept(lhs+rhs))
Addition of the two given arguments.
Definition: Add.h:66

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

v1 + v2; // Compilation error: Cannot add a column vector and a row vector
v1 + trans( v2 ); // OK: Addition of two column vectors
decltype(auto) trans(const DenseMatrix< MT, SO > &dm)
Calculation of the transpose of the given dense matrix.
Definition: DMatTransExpr.h:766
constexpr bool columnVector
Transpose flag for column vectors.
Definition: TransposeFlag.h:58
constexpr bool rowVector
Transpose flag for row vectors.
Definition: TransposeFlag.h:73

Also note that the addition of two vectors with the same element type is favorable due to possible 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


Outer Sum


The addition between a column vector and a row vector results in the outer sum of the two vectors:

// Results in the matrix
//
// ( 1 5 0 6 )
// A = ( 4 8 3 9 )
// ( -2 2 -3 3 )
//
blaze::StaticMatrix<int,3UL,4UL> M1 = v1 + v2; // Outer sum
blaze::StaticMatrix<int,3UL,4UL> M2 = add( v1, v2 ); // Alternative syntax
Efficient implementation of a fixed-sized matrix.
Definition: StaticMatrix.h:249
Efficient implementation of a fixed-sized vector.
Definition: StaticVector.h:230

The trans() function can be used to transpose a vector as necessary:


Matrix/Matrix Addition


For the addition of any two matrices the addition operator (i.e. operator+()) can be used. It even enables the addition of dense and sparse matrices:

// ... Initializing the matrices
M3 = M1 + M2; // Addition of a sparse column-major and a dense row-major matrix of different data type
M3 = add( M1, M2 ); // Alternative syntax
Efficient implementation of a compressed matrix.
Definition: CompressedMatrix.h:239
Efficient implementation of a dynamic matrix.
Definition: DynamicMatrix.h:242
constexpr bool rowMajor
Storage order flag for row-major matrices.
Definition: StorageOrder.h:71
constexpr bool columnMajor
Storage order flag for column-major matrices.
Definition: StorageOrder.h:99

Note that it is necessary that both operands have exactly the same dimensions. Violating this precondition results in an exception. 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 matrices with the same element type are added, the performance can be much higher due to vectorization of the operation.

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


Scalar Addition


For convenience it is also possible to add a scalar value to a dense vector or dense matrix, which has the same effect as adding a uniform vector or matrix. In Blaze it is possible to use all built-in/fundamental data types except bool as scalar values. Additionally, it is possible to use std::complex values with the same built-in data types as element type. Examples:

blaze::StaticVector<int,3UL> v1{ 3, 2, 5, -4, 1, 6 };
blaze::DynamicVector<int> v2 = v1 + 2; // Results in { 5, 4, 7, -2, 3, 8 }
blaze::CompressedVector<int> v3 = 3 + v1; // Results in { 6, 5, 8, -1, 4, 9 }
blaze::DynamicVector<int> v4 = add( v1, 2 ); // Alternative syntax
{ -4, 1, 6 } };
blaze::DynamicMatrix<int> M2 = M1 + 2; // Results in { { 5, 4, 7 }, { -2, 3, 8 } }
blaze::CompressedMatrix<int> M3 = 3 + M1; // Results in { { 6, 5, 8 }, { -1, 4, 9 } }
blaze::DynamicMatrix<int> M4 = add( M1, 2 ); // Alternative syntax


Previous: Arithmetic Operations     Next: Subtraction