# blaze / Subtraction

## Vector/Vector Subtraction

The subtraction of vectors works exactly as intuitive as the addition, but with the subtraction operator (i.e. `operator-()`). It also enables the subtraction of dense and sparse vectors:

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

// ... Initializing the vectors

v3 = v1 - v2;  // Subtraction of a dense and a sparse column vector 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:

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

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

Also note that the subtraction 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 subtraction of two double precision vectors
```

## Outer Difference

The subtraction between a column vector and a row vector results in the outer difference of the two vectors:

```blaze::StaticVector<int,3UL,columnVector> v1{ 2, 5, -1 };
blaze::DynamicVector<int,rowVector> v2{ -1, 3, -2, 4 };

// Results in the matrix
//
//       ( 3 -1  4 -2 )
//   A = ( 6  2  7  1 )
//       ( 0 -4  1 -5 )
//
blaze::StaticMatrix<int,3UL,4UL> M1 = v1 - v2;
```

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

```blaze::StaticVector<int,2UL,rowVector> v1{ 2, 5, -1 };
blaze::DynamicVector<int,rowVector> v2{ -1, 3, -2, 4 };

blaze::StaticMatrix<int,3UL,4UL> M1 = trans( v1 ) - v2;
```

## Matrix/Matrix Subtraction

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

```blaze::DynamicMatrix<float,rowMajor>        M1( 7UL, 3UL );
blaze::CompressedMatrix<size_t,columnMajor> M2( 7UL, 3UL ), M3;

// ... 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. 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 matrices with the same element type are subtracted, 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 subtraction of two row-major, single precision dense matrices
```

## Scalar Subtraction

For convenience it is also possible to subtract a scalar value from a dense vector or dense matrix, which has the same effect as subtracting 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 { 1, 0, 3, -6, -1, 4 }
blaze::CompressedVector<int> v3 = 3 - v1;  // Results in { 0, 1, -2, 7, 2, -3 }
```
```blaze::StaticMatrix<int,2UL,3UL> M1{ {  3, 2, 5 },
{ -4, 1, 6 } };

blaze::DynamicMatrix<int>    M2 = M1 - 2;  // Results in { { 1, 0, 3 }, { -6, -1, 4 } }
blaze::CompressedMatrix<int> M3 = 3 - M1;  // Results in { { 0, 1, -2 }, { 7, 2, -3 } }
```

Previous: Addition ---- Next: Scalar Multiplication

Updated