All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Subtraction
Previous: Addition     Next: Scalar Multiplication


The subtraction of vectors and matrices works exactly as intuitive as the addition, but with the subtraction operator. For both the vector subtraction as well as the matrix subtraction the subtraction operator can be used. It also enables the subtraction of dense and sparse vectors as well as the subtraction of dense and sparse matrices:

// ... Initializing the vectors
v3 = v1 - v2; // Subtraction of a two column vectors of different data type
// ... Initializing the matrices
M3 = M1 - M2; // Subtraction 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 subtract vectors with the same transpose flag:

v1 - v2; // Compilation error: Cannot subtract a row vector from a column vector
v1 - trans( v2 ); // OK: Subtraction of two column vectors

In case of matrices, however, it is possible to subtract row-major and column-major matrices. Note however that in favor of performance the subtraction 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 subtraction of two double precision vectors
blaze::DynamicMatrix<float> M1( 50UL, 70UL ), M2( 50UL, 70UL ), M3;
// ... Initialization of the matrices
M3 = M1 - M2; // Vectorized subtraction of two row-major, single precision dense matrices


Previous: Addition     Next: Scalar Multiplication