Wiki

Clone wiki

blaze / Matrix Operations


Constructors

Matrices are just as easy and intuitive to create as vectors. Still, there are a few rules to be aware of:

  • In case the last template parameter (the storage order) is omitted, the matrix is per default stored in row-major order.
  • The elements of a StaticMatrix or HybridMatrix are default initialized (i.e. built-in data types are initialized to 0, class types are initialized via the default constructor).
  • Newly allocated elements of a DynamicMatrix or CompressedMatrix remain uninitialized if they are of built-in type and are default constructed if they are of class type.

Default Construction

using blaze::StaticMatrix;
using blaze::DynamicMatrix;
using blaze::CompressedMatrix;

// All matrices can be default constructed. Whereas the size of a StaticMatrix is fixed via the second and
// third template parameter, the initial size of a constructed DynamicMatrix or CompressedMatrix is 0.
StaticMatrix<int,2UL,2UL> M1;             // Instantiation of a 2x2 integer row-major
                                          // matrix. All elements are initialized to 0.
DynamicMatrix<float> M2;                  // Instantiation of a single precision dynamic
                                          // row-major matrix with 0 rows and 0 columns.
DynamicMatrix<double,columnMajor> M3;     // Instantiation of a double precision dynamic
                                          // column-major matrix with 0 rows and 0 columns.
CompressedMatrix<int> M4;                 // Instantiation of a compressed integer
                                          // row-major matrix of size 0x0.
CompressedMatrix<double,columnMajor> M5;  // Instantiation of a compressed double precision
                                          // column-major matrix of size 0x0.

Construction with Specific Size

The DynamicMatrix, HybridMatrix and CompressedMatrix classes offer a constructor that allows to immediately give the matrices a specific number of rows and columns:

DynamicMatrix<int> M6( 5UL, 4UL );                   // Instantiation of a 5x4 dynamic row-major
                                                     // matrix. The elements are not initialized.
HybridMatrix<double,5UL,9UL> M7( 3UL, 7UL );         // Instantiation of a 3x7 hybrid row-major
                                                     // matrix. The elements are not initialized.
CompressedMatrix<float,columnMajor> M8( 8UL, 6UL );  // Instantiation of an empty 8x6 compressed
                                                     // column-major matrix.

Note that dense matrices (in this case DynamicMatrix and HybridMatrix) immediately allocate enough capacity for all matrix elements. Sparse matrices on the other hand (in this example CompressedMatrix) merely acquire the size, but don't necessarily allocate memory.

Initialization Constructors

All dense matrix classes offer a constructor for a direct, homogeneous initialization of all matrix elements. In contrast, for sparse matrices the predicted number of non-zero elements can be specified.

StaticMatrix<int,4UL,3UL,columnMajor> M9( 7 );  // Instantiation of a 4x3 integer column-major
                                                // matrix. All elements are initialized to 7.
DynamicMatrix<float> M10( 2UL, 5UL, 2.0F );     // Instantiation of a 2x5 single precision row-major
                                                // matrix. All elements are initialized to 2.0F.
CompressedMatrix<int> M11( 3UL, 4UL, 4 );       // Instantiation of a 3x4 integer row-major
                                                // matrix with capacity for 4 non-zero elements.

Array Construction

Alternatively, all dense matrix classes offer a constructor for an initialization with a dynamic or static array, or with a std::array. If the matrix is initialized from a dynamic array, the constructor expects the dimensions of values provided by the array as first and second argument, the array as third argument. In case of a static array or std::array, the fixed size of the array is used:

const std::unique_ptr<double[]> array1( new double[6] );
// ... Initialization of the dynamic array
blaze::StaticMatrix<double,2UL,3UL> M12( 2UL, 3UL, array1.get() );

int array2[2][2] = { { 4, -5 }, { -6, 7 } };
blaze::StaticMatrix<int,2UL,2UL,rowMajor> M13( array2 );

const std::array<std::array<float,3UL>,2UL> array3{ { { 1, 2, 3 }, { 4, 5, 6 } } };
blaze::StaticMatrix<int,2UL,3UL> M14( array3 );

Initializer List Construction

In addition, all dense and sparse matrix classes can be directly initialized by means of an initializer list:

blaze::DynamicMatrix<float,columnMajor> M15{ {  3.1F,  6.4F },
                                             { -0.9F, -1.2F },
                                             {  4.8F,  0.6F } };
blaze::CompressedMatrix<int,rowMajor> M16{ { 3 },
                                           { 1 },
                                           { 0, 2 } };

Dynamically sized matrices (such as e.g. HybridMatrix, DynamicMatrix or CompressedMatrix) are sized according to the size of the initializer list and all their elements are (copy) assigned the values of the list. For fixed size matrices (such as e.g. StaticMatrix) missing values are initialized as default and in case the size of the top-level initializer list does not match the number of rows of the matrix or the size of any nested list exceeds the number of columns, a std::invalid_argument exception is thrown. In case of sparse matrices, only the non-zero elements are used to initialize the matrix.

Copy Construction

All dense and sparse matrices can be created as a copy of another dense or sparse matrix.

StaticMatrix<int,5UL,4UL,rowMajor> M17( M6 );    // Instantiation of the dense row-major matrix M16
                                                 // as copy of the dense row-major matrix M6.
DynamicMatrix<float,columnMajor> M18( M8 );      // Instantiation of the dense column-major matrix M17
                                                 // as copy of the sparse column-major matrix M8.
CompressedMatrix<double,columnMajor> M19( M7 );  // Instantiation of the compressed column-major matrix
                                                 // M18 as copy of the dense row-major matrix M7.
CompressedMatrix<float,rowMajor> M20( M8 );      // Instantiation of the compressed row-major matrix
                                                 // M19 as copy of the compressed column-major matrix M8.

Note that it is not possible to create a StaticMatrix as a copy of a matrix with a different number of rows and/or columns:

StaticMatrix<int,4UL,5UL,rowMajor> M21( M6 );     // Runtime error: Number of rows and columns does not match!
StaticMatrix<int,4UL,4UL,columnMajor> M22( M9 );  // Compile time error: Number of columns does not match!

Assignment

There are several types of assignment to dense and sparse matrices: Homogeneous Assignment, Array Assignment, Copy Assignment, and Compound Assignment.

Homogeneous Assignment

It is possible to assign the same value to all elements of a dense matrix. All dense matrix classes provide an according assignment operator:

blaze::StaticMatrix<int,3UL,2UL> M1;
blaze::DynamicMatrix<double> M2;

// Setting all integer elements of the StaticMatrix to 4
M1 = 4;

// Setting all double precision elements of the DynamicMatrix to 3.5
M2 = 3.5

Array Assignment

Dense matrices can also be assigned a static array:

blaze::StaticMatrix<int,2UL,2UL,rowMajor> M1;
blaze::StaticMatrix<int,2UL,2UL,columnMajor> M2;
blaze::DynamicMatrix<double> M3;

int array1[2][2] = { { 1, 2 }, { 3, 4 } };
double array2[3][2] = { { 3.1, 6.4 }, { -0.9, -1.2 }, { 4.8, 0.6 } };

M1 = array1;
M2 = array1;
M3 = array2;

Note that the dimensions of the static array have to match the size of a StaticMatrix, whereas a DynamicMatrix is resized according to the array dimensions:

     (  3.1  6.4 )
M3 = ( -0.9 -1.2 )
     (  4.8  0.6 )

Initializer List Assignment

Alternatively, it is possible to directly assign an initializer list to a dense or sparse matrix:

blaze::DynamicMatrix<double> M1;
blaze::CompressedMatrix<int> M2;

M1 = { { 3.1, 6.4 }, { -0.9, -1.2 }, { 4.8, 0.6 } };
M2 = { { 1, 0 }, {}, { 0, 1 }, { 2 } };

Dynamically sized matrices (such as e.g. HybridMatrix, DynamicMatrix or CompressedMatrix) are resized according to the size of the initializer list and all their elements are (copy) assigned the values of the list. For fixed size matrices (such as e.g. StaticMatrix) missing values are reset to their default value and in case the size of the top-level initializer list does not match the number of rows of the matrix or the size of any nested list exceeds the number of columns, a std::invalid_argument exception is thrown. In case of sparse matrices, only the non-zero elements are considered.

Copy Assignment

All kinds of matrices can be assigned to each other. The only restriction is that since a StaticMatrix cannot change its size, the assigned matrix must match both in the number of rows and in the number of columns.

blaze::StaticMatrix<int,3UL,2UL,rowMajor> M1;
blaze::DynamicMatrix<int,rowMajor> M2( 3UL, 2UL );
blaze::DynamicMatrix<float,rowMajor> M3( 5UL, 2UL );
blaze::CompressedMatrix<int,rowMajor> M4( 3UL, 2UL );
blaze::CompressedMatrix<float,columnMajor> M5( 3UL, 2UL );

// ... Initialization of the matrices

M1 = M2;  // OK: Assignment of a 3x2 dense row-major matrix to another 3x2 dense row-major matrix
M1 = M4;  // OK: Assignment of a 3x2 sparse row-major matrix to a 3x2 dense row-major matrix
M1 = M3;  // Runtime error: Cannot assign a 5x2 matrix to a 3x2 static matrix
M1 = M5;  // OK: Assignment of a 3x2 sparse column-major matrix to a 3x2 dense row-major matrix

Compound Assignment

Compound assignment is also available for matrices: addition assignment, subtraction assignment, and multiplication assignment. In contrast to plain assignment, however, the number of rows and columns of the two operands have to match according to the arithmetic operation.

blaze::StaticMatrix<int,2UL,3UL,rowMajor> M1;
blaze::DynamicMatrix<int,rowMajor> M2( 2UL, 3UL );
blaze::CompressedMatrix<float,columnMajor> M3( 2UL, 3UL );
blaze::CompressedMatrix<float,rowMajor> M4( 2UL, 4UL );
blaze::StaticMatrix<float,2UL,4UL,rowMajor> M5;
blaze::CompressedMatrix<float,rowMajor> M6( 3UL, 2UL );

// ... Initialization of the matrices

M1 += M2 ; // OK: Addition assignment between two row-major matrices of the same dimensions
M1 -= M3;  // OK: Subtraction assignment between between a row-major and a column-major matrix
M1 += M4 ; // Runtime error: No compound assignment between matrices of different size
M1 -= M5;  // Compilation error: No compound assignment between matrices of different size
M2 *= M6;  // OK: Multiplication assignment between two row-major matrices

Note that the multiplication assignment potentially changes the number of columns of the target matrix:

( 2 0 1 )     ( 4 0 )     ( 8 3 )
( 0 3 2 )  *  ( 1 0 )  =  ( 3 6 )
              ( 0 3 )

Since a StaticMatrix cannot change its size, only a square StaticMatrix can be used in a multiplication assignment with other square matrices of the same dimensions.


Element Access

Function Call Operator

The easiest way to access a specific dense or sparse matrix element is via the function call operator. The indices to access a matrix are zero-based:

blaze::DynamicMatrix<int> M1( 4UL, 6UL );
M1(0,0) = 1;
M1(0,1) = 3;
// ...

blaze::CompressedMatrix<double> M2( 5UL, 3UL );
M2(0,2) =  4.1;
M2(1,1) = -6.3;

Since dense matrices allocate enough memory for all contained elements, using the function call operator on a dense matrix directly returns a reference to the accessed value. In case of a sparse matrix, if the accessed value is currently not contained in the matrix, the value is inserted into the matrix prior to returning a reference to the value, which can be much more expensive than the direct access to a dense matrix. Consider the following example:

blaze::CompressedMatrix<int> M1( 4UL, 4UL );

for( size_t i=0UL; i<M1.rows(); ++i ) {
   for( size_t j=0UL; j<M1.columns(); ++j ) {
      ... = M1(i,j);
   }
}

Although the compressed matrix is only used for read access within the for loop, using the function call operator temporarily inserts 16 non-zero elements into the matrix. Therefore the preferred way to traverse the non-zero elements of a sparse matrix is to use iterators.

Iterators

An alternate way to traverse the elements contained in a dense or sparse matrix is by means of iterators. For that purpose, all matrices provide the begin(), cbegin(), end(), and cend() members functions. Note that it is not possible to traverse all elements of the matrix, but that it is only possible to traverse elements in a row-wise fashion (in case of a row-major matrix) or in a column-wise fashion (in case of a column-major matrix). In case of non-const matrices, begin() and end() return an Iterator, which allows a manipulation of the (non-zero) value. In case of a constant matrix or in case cbegin() or cend() are used a ConstIterator is returned. Iterators on dense matrices traverse all elements of the matrix, including the zero elements. Iterators on sparse matrices only traverse the non-zero elements.

The following two examples demonstrate how to traverse the elements of a dense and sparse matrix, respectively:

using blaze::DynamicMatrix;
using blaze::rowMajor;
using blaze::columnMajor;

DynamicMatrix<int,rowMajor> M1( 4UL, 6UL );
DynamicMatrix<int,columnMajor> M2( 4UL, 6UL );

// Traversing all elements contained in the row-major matrix by Iterator
for( size_t i=0UL; i<M1.rows(); ++i ) {
   for( DynamicMatrix<int,rowMajor>::Iterator it=M1.begin(i); it!=M1.end(i); ++it ) {
      *it = ...;  // OK: Write access to the value of the element.
      ... = *it;  // OK: Read access to the value of the element.
   }
}

// Traversing all elements contained in the column-major matrix by ConstIterator
for( size_t j=0UL; j<M2.columns(); ++j ) {
   for( DynamicMatrix<int,columnMajor>::ConstIterator it=M2.cbegin(j); it!=M2.cend(j); ++it ) {
      *it = ...;  // Compilation error: Assignment to the value via a ConstIterator is invalid.
      ... = *it;  // OK: Read access to the value of the element.
   }
}
using blaze::CompressedMatrix;
using blaze::rowMajor;
using blaze::columnMajor;

CompressedMatrix<int,rowMajor> M3( 4UL, 6UL );
CompressedMatrix<int,columnMajor> M4( 4UL, 6UL );

// Traversing the non-zero elements contained in the row-major matrix by Iterator
for( size_t i=0UL; i<M3.rows(); ++i ) {
   for( CompressedMatrix<int,rowMajor>::Iterator it=M3.begin(i); it!=M3.end(i); ++it ) {
      it->value() = ...;  // OK: Write access to the value of the non-zero element.
      ... = it->value();  // OK: Read access to the value of the non-zero element.
      it->index() = ...;  // Compilation error: The index of a non-zero element cannot be changed.
      ... = it->index();  // OK: Read access to the index of the non-zero element.
   }
}

// Traversing the non-zero elements contained in the column-major matrix by ConstIterator
for( size_t j=0UL; j<M4.columns(); ++j ) {
   for( CompressedMatrix<int,columnMajor>::ConstIterator it=M4.cbegin(j); it!=M4.cend(j); ++it ) {
      it->value() = ...;  // Compilation error: Assignment to the value via a ConstIterator is invalid.
      ... = it->value();  // OK: Read access to the value of the non-zero element.
      it->index() = ...;  // Compilation error: The index of a non-zero element cannot be changed.
      ... = it->index();  // OK: Read access to the index of the non-zero element.
   }
}

Note that begin(), cbegin(), end(), and cend() are also available as free functions:

for( size_t i=0UL; i<M3.rows(); ++i ) {
   for( CompressedMatrix<int,rowMajor>::Iterator it=begin( M3, i ); it!=end( M3, i ); ++it ) {
      // ...
   }
}

for( size_t j=0UL; j<M4.columns(); ++j ) {
   for( CompressedMatrix<int,columnMajor>::ConstIterator it=cbegin( M4, j ); it!=cend( M4, j ); ++it ) {
      // ...
   }
}

.data() / data()

Sometimes it is necessary to acquire a pointer to the first element of the underlying array of a dense matrix. For that purpose the data() member function or the free data() function can be used:

// Instantiating a dynamic vector with 10 elements
blaze::DynamicMatrix<int> A( 5UL, 7UL );
A.data();   // Returns a pointer to the first element of the dynamic matrix
data( A );  // Same effect as the member function

Note that you can NOT assume that all matrix elements lie adjacent to each other! The dense matrix may use techniques such as padding to improve the alignment of the data. Whereas the number of elements within a row/column are given by the rows() and columns() member functions, respectively, the total number of elements including padding is given by the spacing() member function.


Element Insertion

Whereas a dense matrix always provides enough capacity to store all matrix elements, a sparse matrix only stores the non-zero elements. Therefore it is necessary to explicitly add elements to the matrix.

Function Call Operator

The first possibility to add elements to a sparse matrix is the function call operator:

using blaze::CompressedMatrix;

CompressedMatrix<int> M1( 3UL, 4UL );
M1(1,2) = 9;

In case the element at the given position is not yet contained in the sparse matrix, it is automatically inserted. Otherwise the old value is replaced by the new value 2. The operator returns a reference to the sparse vector element.

.set()

An alternative to the function call operator is the set() function: In case the element is not yet contained in the matrix the element is inserted, else the element's value is modified:

// Insert or modify the value at position (2,0)
M1.set( 2, 0, 1 );

.insert()

The insertion of elements can be better controlled via the insert() function. In contrast to the function call operator and the set() function it emits an exception in case the element is already contained in the matrix. In order to check for this case, the find() function can be used:

// In case the element at position (2,3) is not yet contained in the matrix it is inserted with a value of 4.
if( M1.find( 2, 3 ) == M1.end( 2 ) )
   M1.insert( 2, 3, 4 );

.append()

Although the insert() function is very flexible, due to performance reasons it is not suited for the setup of large sparse matrices. A very efficient, yet also very low-level way to fill a sparse matrix is the append() function. It requires the sparse matrix to provide enough capacity to insert a new element in the specified row/column. Additionally, the index of the new element must be larger than the index of the previous element in the same row/column. Violating these conditions results in undefined behavior!

M1.reserve( 0, 3 );     // Reserving space for three non-zero elements in row 0
M1.append( 0, 1,  2 );  // Appending the element 2 in row 0 at column index 1
M1.append( 0, 2, -4 );  // Appending the element -4 in row 0 at column index 2
// ...

The most efficient way to fill a sparse matrix with elements, however, is a combination of reserve(), append(), and the finalize() function:

// Setup of the compressed row-major matrix
//
//       ( 0 1 0 2 0 )
//   A = ( 0 0 0 0 0 )
//       ( 3 0 0 0 0 )
//
blaze::CompressedMatrix<int> M1( 3UL, 5UL );
M1.reserve( 3 );       // Reserving enough space for 3 non-zero elements
M1.append( 0, 1, 1 );  // Appending the value 1 in row 0 with column index 1
M1.append( 0, 3, 2 );  // Appending the value 2 in row 0 with column index 3
M1.finalize( 0 );      // Finalizing row 0
M1.finalize( 1 );      // Finalizing the empty row 1 to prepare row 2
M1.append( 2, 0, 3 );  // Appending the value 3 in row 2 with column index 0
M1.finalize( 2 );      // Finalizing row 2

Please note that the finalize() function has to be explicitly called for each row or column, even for empty ones! Also note that although append() does not allocate new memory, it still invalidates all iterators returned by the end() functions!


Element Removal

.erase()

The erase() member functions can be used to remove elements from a sparse matrix. The following example gives an impression of the five different flavors of erase():

using blaze::CompressedMatrix;

CompressedMatrix<int,rowMajor> A( 42, 53 );
// ... Initialization of the matrix

// Erasing the element at position (21,23)
A.erase( 21, 23 );

// Erasing a single element in row 17 via iterator
A.erase( 17, A.find( 4 ) );

// Erasing all non-zero elements in the range [7..24] of row 33
A.erase( 33, A.lowerBound( 33, 7 ), A.upperBound( 33, 24 ) );

// Erasing all non-zero elements with a value larger than 9 by passing a unary predicate
A.erase( []( int i ){ return i > 9; } );

// Erasing all non-zero elements in the range [30..40] of row 37 with a value larger than 5
CompressedMatrix<int,rowMajor>::Iterator pos1( A.lowerBound( 37, 30 ) );
CompressedMatrix<int,rowMajor>::Iterator pos2( A.upperBound( 37, 40 ) );
A.erase( 37, pos1, pos2, []( int i ){ return i > 5; } );

Element Lookup

A sparse matrix only stores the non-zero elements contained in the matrix. Therefore, whenever accessing a matrix element at a specific position a lookup operation is required. Whereas the function call operator is performing this lookup automatically, it is also possible to use the find(), lowerBound(), and upperBound() member functions for a manual lookup.

.find() / find()

The find() function can be used to check whether a specific element is contained in the sparse matrix. It specifically searches for the element at the specified position. In case the element is found, the function returns an iterator to the element. Otherwise an iterator just past the last non-zero element of the according row or column (the end() iterator) is returned. Note that the returned iterator is subject to invalidation due to inserting operations via the function call operator, the set() function or the insert() function!

using blaze::CompressedMatrix;

CompressedMatrix<int,rowMajor> A( 42, 53 );
// ... Initialization of the matrix

// Searching the element at position (7,17). In case the element is not
// contained in the vector, the end() iterator of row 7 is returned.
CompressedMatrix<int,rowMajor>::Iterator pos( A.find( 7, 17 ) );

if( pos != A.end( 7 ) ) {
   // ...
}

Alternatively, the free function find() can be used to find a specific element in a sparse matrix:

find( A, 7, 17 );  // Searching the element at position (7,17); same effect as the member function

.lowerBound() / lowerBound()

In case of a row-major matrix, this function returns a row iterator to the first element with an index not less then the given column index. In case of a column-major matrix, the function returns a column iterator to the first element with an index not less then the given row index. In combination with the upperBound() function this function can be used to create a pair of iterators specifying a range of indices. Note that the returned iterator is subject to invalidation due to inserting operations via the function call operator, the set() function or the insert() function!

using blaze::CompressedMatrix;

CompressedMatrix<int,rowMajor> A( 42, 53 );
// ... Initialization of the matrix

// Searching the lower bound of column index 17 in row 7.
CompressedMatrix<int,rowMajor>::Iterator pos1( A.lowerBound( 7, 17 ) );

// Searching the upper bound of column index 28 in row 7
CompressedMatrix<int,rowMajor>::Iterator pos2( A.upperBound( 7, 28 ) );

// Erasing all elements in the specified range
A.erase( 7, pos1, pos2 );

Alternatively, the free function lowerBound() can be used to:

lowerBound( A, 7, 17 );  // Searching the lower bound of (7,17); same effect as the member function

.upperBound() / upperBound()

In case of a row-major matrix, this function returns a row iterator to the first element with an index greater then the given column index. In case of a column-major matrix, the function returns a column iterator to the first element with an index greater then the given row index. In combination with the lowerBound() function this function can be used to create a pair of iterators specifying a range of indices. Note that the returned iterator is subject to invalidation due to inserting operations via the function call operator, the set() function or the insert() function!

using blaze::CompressedMatrix;

CompressedMatrix<int,columnMajor> A( 42, 53 );
// ... Initialization of the matrix

// Searching the lower bound of row index 17 in column 9.
CompressedMatrix<int,columnMajor>::Iterator pos1( A.lowerBound( 17, 9 ) );

// Searching the upper bound of row index 28 in column 9
CompressedMatrix<int,columnMajor>::Iterator pos2( A.upperBound( 28, 9 ) );

// Erasing all elements in the specified range
A.erase( 9, pos1, pos2 );

Alternatively, the free function upperBound() can be used to:

upperBound( A, 28, 9 );  // Searching the upper bound of (28,9); same effect as the member function

Non-Modifying Operations

.rows() / rows()

The current number of rows of a matrix can be acquired via the rows() member function:

// Instantiating a dynamic matrix with 10 rows and 8 columns
blaze::DynamicMatrix<int> M1( 10UL, 8UL );
M1.rows();  // Returns 10

// Instantiating a compressed matrix with 8 rows and 12 columns
blaze::CompressedMatrix<double> M2( 8UL, 12UL );
M2.rows();  // Returns 8

Alternatively, the free functions rows() can be used to query the current number of rows of a matrix. In contrast to the member function, the free function can also be used to query the number of rows of a matrix expression:

rows( M1 );  // Returns 10, i.e. has the same effect as the member function
rows( M2 );  // Returns 8, i.e. has the same effect as the member function

rows( M1 * M2 );  // Returns 10, i.e. the number of rows of the resulting matrix

.columns() / columns()

The current number of columns of a matrix can be acquired via the columns() member function:

// Instantiating a dynamic matrix with 6 rows and 8 columns
blaze::DynamicMatrix<int> M1( 6UL, 8UL );
M1.columns();   // Returns 8

// Instantiating a compressed matrix with 8 rows and 7 columns
blaze::CompressedMatrix<double> M2( 8UL, 7UL );
M2.columns();   // Returns 7

There is also a free function columns() available, which can also be used to query the number of columns of a matrix expression:

columns( M1 );  // Returns 8, i.e. has the same effect as the member function
columns( M2 );  // Returns 7, i.e. has the same effect as the member function

columns( M1 * M2 );  // Returns 7, i.e. the number of columns of the resulting matrix

size()

The size() function returns the total number of elements of a matrix:

// Instantiating a dynamic matrix with 6 rows and 8 columns
blaze::DynamicMatrix<int> M1( 6UL, 8UL );
size( M1 );   // Returns 48

// Instantiating a compressed matrix with 8 rows and 7 columns
blaze::CompressedMatrix<double> M2( 8UL, 7UL );
size( M2 );  // Returns 56

.spacing() / spacing()

The total number of elements of a row or column of a dense matrix, including potential padding elements, can be acquired via the spacing member function. In case of a row-major matrix (i.e. in case the storage order is set to blaze::rowMajor) the function returns the spacing between two rows, in case of a column-major matrix (i.e. in case the storage flag is set to blaze::columnMajor) the function returns the spacing between two columns:

// Instantiating a row-major dynamic matrix with 7 rows and 8 columns
blaze::DynamicMatrix<int,blaze::rowMajor> M1( 7UL, 8UL );
M1.spacing();  // Returns the total number of elements in a row

// Instantiating a column-major dynamic matrix with 8 rows and 12 columns
blaze::CompressedMatrix<double> M2( 8UL, 12UL );
M2.spacing();  // Returns the total number of element in a column

Alternatively, the free functions spacing() can be used to query the current number of elements in a row/column.

spacing( M1 );  // Returns the total number of elements in a row
spacing( M2 );  // Returns the total number of elements in a column

.capacity() / capacity()

The capacity() member function returns the internal capacity of a dense or sparse matrix. Note that the capacity of a matrix doesn't have to be equal to the size of a matrix. In case of a dense matrix the capacity will always be greater or equal than the total number of elements of the matrix. In case of a sparse matrix, the capacity will usually be much less than the total number of elements.

blaze::DynamicMatrix<float> M1( 5UL, 7UL );
blaze::StaticMatrix<float,7UL,4UL> M2;
M1.capacity();  // Returns at least 35
M2.capacity();  // Returns at least 28

There is also a free function capacity() available to query the capacity. However, please note that this function cannot be used to query the capacity of a matrix expression:

capacity( M1 );  // Returns at least 35, i.e. has the same effect as the member function
capacity( M2 );  // Returns at least 28, i.e. has the same effect as the member function

capacity( M1 * M2 );  // Compilation error!

.nonZeros() / nonZeros()

For both dense and sparse matrices the current number of non-zero elements can be queried via the nonZeros() member function. In case of matrices there are two flavors of the nonZeros() function: One returns the total number of non-zero elements in the matrix, the second returns the number of non-zero elements in a specific row (in case of a row-major matrix) or column (in case of a column-major matrix). Sparse matrices directly return their number of non-zero elements, dense matrices traverse their elements and count the number of non-zero elements.

blaze::DynamicMatrix<int,rowMajor> M1( 3UL, 5UL );

// ... Initializing the dense matrix

M1.nonZeros();     // Returns the total number of non-zero elements in the dense matrix
M1.nonZeros( 2 );  // Returns the number of non-zero elements in row 2
blaze::CompressedMatrix<double,columnMajor> M2( 4UL, 7UL );

// ... Initializing the sparse matrix

M2.nonZeros();     // Returns the total number of non-zero elements in the sparse matrix
M2.nonZeros( 3 );  // Returns the number of non-zero elements in column 3

The free nonZeros() function can also be used to query the number of non-zero elements in a matrix expression. However, the result is not the exact number of non-zero elements, but may be a rough estimation:

nonZeros( M1 );     // Has the same effect as the member function
nonZeros( M1, 2 );  // Has the same effect as the member function

nonZeros( M2 );     // Has the same effect as the member function
nonZeros( M2, 3 );  // Has the same effect as the member function

nonZeros( M1 * M2 );  // Estimates the number of non-zero elements in the matrix expression

isEmpty()

The isEmpty() function returns whether the total number of elements of the matrix is zero:

blaze::DynamicMatrix<int> A;  // Create an empty matrix
isEmpty( A );                 // Returns true
A.resize( 5, 0 );             // Resize to a 5x0 matrix
isEmpty( A );                 // Returns true
A.resize( 5, 3 );             // Resize to a 5x3 matrix
isEmpty( A );                 // Returns false

isnan()

The isnan() function provides the means to check a dense or sparse matrix for non-a-number elements:

blaze::DynamicMatrix<double> A( 3UL, 4UL );
// ... Initialization
if( isnan( A ) ) { ... }
blaze::CompressedMatrix<double> A( 3UL, 4UL );
// ... Initialization
if( isnan( A ) ) { ... }

If at least one element of the matrix is not-a-number, the function returns true, otherwise it returns false. Please note that this function only works for matrices with floating point elements. The attempt to use it for a matrix with a non-floating point element type results in a compile time error.

isinf()

The isinf() function checks the given dense or sparse matrix for infinite (inf) elements:

blaze::DynamicMatrix<double> A( 3UL, 4UL );
// ... Initialization
if( isinf( A ) ) { ... }
blaze::CompressedMatrix<double> A( 3UL, 4UL );
// ... Initialization
if( isinf( A ) ) { ... }

If at least one element of the matrix is infinite, the function returns true, otherwise it returns false.

isfinite()

The isfinite() function checks if all elements of the given dense or sparse matrix are finite elements (i.e. normal, subnormal or zero elements, but not infinite or NaN):

blaze::DynamicMatrix<double> A( 3UL, 4UL );
// ... Initialization
if( isfinite( A ) ) { ... }
blaze::CompressedMatrix<double> A( 3UL, 4UL );
// ... Initialization
if( isfinite( A ) ) { ... }

If all elements of the matrix are finite, the function returns true, otherwise it returns false.

isDefault()

The isDefault() function returns whether the given dense or sparse matrix is in default state:

blaze::HybridMatrix<int,5UL,4UL> A;
// ... Resizing and initialization
if( isDefault( A ) ) { ... }

A matrix is in default state if it appears to just have been default constructed. A resizable matrix (HybridMatrix, DynamicMatrix, or CompressedMatrix) is in default state if its size is equal to zero. A non-resizable matrix (StaticMatrix and all submatrices) is in default state if all its elements are in default state. For instance, in case the matrix is instantiated for a built-in integral or floating point data type, the function returns true in case all matrix elements are 0 and false in case any vector element is not 0.

isSquare()

If a dense or sparse matrix is a square matrix (i.e. if the number of rows is equal to the number of columns) can be checked via the isSquare() function:

blaze::DynamicMatrix<double> A;
// ... Resizing and initialization
if( isSquare( A ) ) { ... }

isSymmetric()

Via the isSymmetric() function it is possible to check whether a dense or sparse matrix is symmetric:

blaze::DynamicMatrix<float> A;
// ... Resizing and initialization
if( isSymmetric( A ) ) { ... }

Note that non-square matrices are never considered to be symmetric!

isUniform()

In order to check if all matrix elements are identical, the isUniform() function can be used:

blaze::DynamicMatrix<int> A;
// ... Resizing and initialization
if( isUniform( A ) ) { ... }

Note that in case of a sparse matrix also the zero elements are also taken into account!

isZero()

In order to check if all matrix elements are zero, the isZero() function can be used:

blaze::DynamicMatrix<int> A;
// ... Resizing and initialization
if( isZero( A ) ) { ... }

isLower()

Via the isLower() function it is possible to check whether a dense or sparse matrix is lower triangular:

blaze::DynamicMatrix<float> A;
// ... Resizing and initialization
if( isLower( A ) ) { ... }

Note that non-square matrices are never considered to be lower triangular!

isUniLower()

Via the isUniLower() function it is possible to check whether a dense or sparse matrix is lower unitriangular:

blaze::DynamicMatrix<float> A;
// ... Resizing and initialization
if( isUniLower( A ) ) { ... }

Note that non-square matrices are never considered to be lower unitriangular!

isStrictlyLower()

Via the isStrictlyLower() function it is possible to check whether a dense or sparse matrix is strictly lower triangular:

blaze::DynamicMatrix<float> A;
// ... Resizing and initialization
if( isStrictlyLower( A ) ) { ... }

Note that non-square matrices are never considered to be strictly lower triangular!

isUpper()

Via the isUpper() function it is possible to check whether a dense or sparse matrix is upper triangular:

blaze::DynamicMatrix<float> A;
// ... Resizing and initialization
if( isUpper( A ) ) { ... }

Note that non-square matrices are never considered to be upper triangular!

isUniUpper()

Via the isUniUpper() function it is possible to check whether a dense or sparse matrix is upper unitriangular:

blaze::DynamicMatrix<float> A;
// ... Resizing and initialization
if( isUniUpper( A ) ) { ... }

Note that non-square matrices are never considered to be upper unitriangular!

isStrictlyUpper()

Via the isStrictlyUpper() function it is possible to check whether a dense or sparse matrix is strictly upper triangular:

blaze::DynamicMatrix<float> A;
// ... Resizing and initialization
if( isStrictlyUpper( A ) ) { ... }

Note that non-square matrices are never considered to be strictly upper triangular!

isDiagonal()

The isDiagonal() function checks if the given dense or sparse matrix is a diagonal matrix, i.e. if it has only elements on its diagonal and if the non-diagonal elements are default elements:

blaze::CompressedMatrix<float> A;
// ... Resizing and initialization
if( isDiagonal( A ) ) { ... }

Note that non-square matrices are never considered to be diagonal!

isIdentity()

The isIdentity() function checks if the given dense or sparse matrix is an identity matrix, i.e. if all diagonal elements are 1 and all non-diagonal elements are 0:

blaze::CompressedMatrix<float> A;
// ... Resizing and initialization
if( isIdentity( A ) ) { ... }

Note that non-square matrices are never considered to be identity matrices!

isPositiveDefinite()

The isPositiveDefinite() function checks if the given dense matrix is positive definite.

blaze::DynamicMatrix<double> A;
// ... Initialization
if( isPositiveDefinite( A ) ) { ... }

Note that non-square matrices are never considered to be positive definite!

Note that the isPositiveDefinite() function can only be used for dense matrices with float, double, complex<float> or complex<double> element type. The attempt to call the function with matrices of any other element type or with a sparse matrix results in a compile time error!

Also note that the function is depending on LAPACK kernels. Thus the function can only be used if a fitting LAPACK library is available and linked to the executable. Otherwise a linker error will be created.

trans()

Matrices can be transposed via the trans() function. Row-major matrices are transposed into a column-major matrix and vice versa:

blaze::DynamicMatrix<int,rowMajor> M1( 5UL, 2UL );
blaze::CompressedMatrix<int,columnMajor> M2( 3UL, 7UL );

M1 = M2;            // Assigning a column-major matrix to a row-major matrix
M1 = trans( M2 );   // Assigning the transpose of M2 (i.e. a row-major matrix) to M1
M1 += trans( M2 );  // Addition assignment of two row-major matrices

ctrans()

The conjugate transpose of a dense or sparse matrix (also called adjoint matrix, Hermetian conjugate, or transjugate) can be computed via the ctrans() function:

blaze::DynamicMatrix< complex<float>, rowMajor > M1( 5UL, 2UL );
blaze::CompressedMatrix< complex<float>, columnMajor > M2( 2UL, 5UL );

M1 = ctrans( M2 );  // Compute the conjugate transpose matrix

Note that the ctrans() function has the same effect as manually applying the conj() and trans() function in any order:

M1 = trans( conj( M2 ) );  // Computing the conjugate transpose matrix
M1 = conj( trans( M2 ) );  // Computing the conjugate transpose matrix

reverse()

Via the reverse() function is is possible to reverse the rows or columns of a dense or sparse matrix. The following examples gives an impression of both alternatives:

blaze::DynamicMatrix<int,rowMajor> A{ { 1, 0, 2, 3 },
                                      { 2, 4, 0, 1 },
                                      { 0, 3, 1, 0 } };
blaze::DynamicMatrix<int> B;

// Reversing the rows result in the matrix
//
//    ( 0 3 1 0 )
//    ( 2 4 0 1 )
//    ( 1 0 2 3 )
//
B = reverse<rowwise>( A );

// Reversing the columns result in the matrix
//
//    ( 3 2 0 1 )
//    ( 1 0 4 2 )
//    ( 0 1 3 0 )
//
B = reverse<columnwise>( A );

eval() / evaluate()

The evaluate() function forces an evaluation of the given matrix expression and enables an automatic deduction of the correct result type of an operation. The following code example demonstrates its intended use for the multiplication of a lower and a strictly lower dense matrix:

using blaze::DynamicMatrix;
using blaze::LowerMatrix;
using blaze::StrictlyLowerMatrix;

LowerMatrix< DynamicMatrix<double> > A;
StrictlyLowerMatrix< DynamicMatrix<double> > B;
// ... Resizing and initialization

auto C = evaluate( A * B );

In this scenario, the evaluate() function assists in deducing the exact result type of the operation via the auto keyword. Please note that if evaluate() is used in this way, no temporary matrix is created and no copy operation is performed. Instead, the result is directly written to the target matrix due to the return value optimization (RVO). However, if evaluate() is used in combination with an explicit target type, a temporary will be created and a copy operation will be performed if the used type differs from the type returned from the function:

StrictlyLowerMatrix< DynamicMatrix<double> > D( A * B );  // No temporary & no copy operation
LowerMatrix< DynamicMatrix<double> > E( A * B );          // Temporary & copy operation
DynamicMatrix<double> F( A * B );                         // Temporary & copy operation
D = evaluate( A * B );                                    // Temporary & copy operation

Sometimes it might be desirable to explicitly evaluate a sub-expression within a larger expression. However, please note that evaluate() is not intended to be used for this purpose. This task is more elegantly and efficiently handled by the eval() function:

blaze::DynamicMatrix<double> A, B, C, D;

D = A + evaluate( B * C );  // Unnecessary creation of a temporary matrix
D = A + eval( B * C );      // No creation of a temporary matrix

In contrast to the evaluate() function, eval() can take the complete expression into account and therefore can guarantee the most efficient way to evaluate it (see also Intra-Statement Optimization).

noalias()

The Blaze library is able to reliably detect aliasing during the assignment of matrices. In case the aliasing would lead to an incorrect result, Blaze introduces an intermediate temporary of the appropriate type to break the aliasing. For instance, in the following example Blaze performs an alias detection in both assignments, but only, in the second assignment it detects a problematic aliasing and uses an intermediate temporary in order to be able to compute the correct result:

blaze::DynamicMatrix<double> A, B;

A = A + B;  // No problematic aliasing of A, no intermediate temporary is required.
A = A * B;  // Problematic aliasing of A; intermediate temporary required!

The detection of aliasing effects, however, takes a small runtime effort. In order to disable the aliasing detection, the noalias() function can be used:

blaze::DynamicMatrix<double> A, B;

A = noalias( A + B );  // No alias detection performed, no intermediate temporary.
A = noalias( A * B );  // No alias detection performed, no intermediate temporary.
                       // Note that the final result will be incorrect!

Warning: The noalias() operation has the semantics of a cast: The caller is completely responsible and the system trusts the given information. Using noalias() in a situation where an aliasing effect occurs leads to undefined behavior (which can be violated invariants or wrong computation results)!

nosimd()

By default, Blaze attempts to vectorize all operations by means of SSE, AVX, etc. in order to achieve maximum performance. However, via the nosimd() operation it is possible to disable the SIMD evaluation of any operation:

blaze::DynamicMatrix<double> A, B;

A = nosimd( A + B );  // Disables SIMD for the matrix/matrix addition
A = nosimd( A * B );  // Disables SIMD for the matrix/matrix multiplication

Please note that the main purpose of the nosimd() operation is to enable an easy performance comparison between the vectorized and non-vectorized evaluation. Using the nosimd() operation will likely result in significantly reduced performance!


Modifying Operations

.resize() / .reserve()

The dimensions of a StaticMatrix are fixed at compile time by the second and third template parameter. In contrast, the number or rows and/or columns of DynamicMatrix and CompressedMatrix can be changed at runtime:

using blaze::DynamicMatrix;
using blaze::CompressedMatrix;

DynamicMatrix<int,rowMajor> M1;
CompressedMatrix<int,columnMajor> M2( 3UL, 2UL );

// Adapting the number of rows and columns via the resize() function. The (optional)
// third parameter specifies whether the existing elements should be preserved. Per
// default, the existing elements are preserved.
M1.resize( 2UL, 2UL );         // Resizing matrix M1 to 2x2 elements. Elements of built-in type remain
                               // uninitialized, elements of class type are default constructed.
M1.resize( 3UL, 1UL, false );  // Resizing M1 to 3x1 elements. The old elements are lost, the
                               // new elements are NOT initialized!
M2.resize( 5UL, 7UL, true );   // Resizing M2 to 5x7 elements. The old elements are preserved.
M2.resize( 3UL, 2UL, false );  // Resizing M2 to 3x2 elements. The old elements are lost.

Note that resizing a matrix invalidates all existing views (see e.g. Submatrices) on the matrix:

using MatrixType = blaze::DynamicMatrix<int,rowMajor>;
using RowType    = blaze::Row<MatrixType>;

MatrixType M1( 10UL, 20UL );    // Creating a 10x20 matrix
RowType row8 = row( M1, 8UL );  // Creating a view on the 8th row of the matrix
M1.resize( 6UL, 20UL );         // Resizing the matrix invalidates the view

When the internal capacity of a matrix is no longer sufficient, the allocation of a larger junk of memory is triggered. In order to avoid frequent reallocations, the reserve() function can be used up front to set the internal capacity:

blaze::DynamicMatrix<int> M1;
M1.reserve( 100 );
M1.rows();      // Returns 0
M1.capacity();  // Returns at least 100

Additionally it is possible to reserve memory in a specific row (for a row-major matrix) or column (for a column-major matrix):

blaze::CompressedMatrix<int> M1( 4UL, 6UL );
M1.reserve( 1, 4 );  // Reserving enough space for four non-zero elements in row 1

.shrinkToFit()

The internal capacity of matrices with dynamic memory is preserved in order to minimize the number of reallocations. For that reason, the resize() and reserve() functions can lead to memory overhead. The shrinkToFit() member function can be used to minimize the internal capacity:

blaze::DynamicMatrix<int> M1( 100UL, 100UL );  // Create a 100x100 integer matrix
M1.resize( 10UL, 10UL );                       // Resize to 10x10, but the capacity is preserved
M1.shrinkToFit();                              // Remove the unused capacity

Please note that due to padding the capacity might not be reduced exactly to rows() times columns(). Please also note that in case a reallocation occurs, all iterators (including end() iterators), all pointers and references to elements of this matrix are invalidated.

reset() / clear()

In order to reset all elements of a dense or sparse matrix, the reset() function can be used. The number of rows and columns of the matrix are preserved:

// Setting up a single precision row-major matrix, whose elements are initialized with 2.0F.
blaze::DynamicMatrix<float> M1( 4UL, 5UL, 2.0F );

// Resetting all elements to 0.0F.
reset( M1 );  // Resetting all elements
M1.rows();    // Returns 4: size and capacity remain unchanged

Alternatively, only a single row or column of the matrix can be resetted:

blaze::DynamicMatrix<int,blaze::rowMajor>    M1( 7UL, 6UL, 5 );  // Setup of a row-major matrix
blaze::DynamicMatrix<int,blaze::columnMajor> M2( 4UL, 5UL, 4 );  // Setup of a column-major matrix

reset( M1, 2UL );  // Resetting the 2nd row of the row-major matrix
reset( M2, 3UL );  // Resetting the 3rd column of the column-major matrix

In order to reset a row of a column-major matrix or a column of a row-major matrix, use a row or column view (see \ref views_rows and views_colums).

In order to return a matrix to its default state (i.e. the state of a default constructed matrix), the clear() function can be used:

// Setting up a single precision row-major matrix, whose elements are initialized with 2.0F.
blaze::DynamicMatrix<float> M1( 4UL, 5UL, 2.0F );

// Resetting all elements to 0.0F.
clear( M1 );  // Resetting the entire matrix
M1.rows();    // Returns 0: size is reset, but capacity remains unchanged

transpose()

In addition to the non-modifying trans() function, matrices can be transposed in-place via the transpose() function:

blaze::DynamicMatrix<int,rowMajor> M( 5UL, 2UL );

transpose( M );  // In-place transpose operation.
M = trans( M );  // Same as above

Note however that the transpose operation fails if ...

  • ... the given matrix has a fixed size and is non-square;
  • ... the given matrix is a triangular matrix;
  • ... the given submatrix affects the restricted parts of a triangular matrix;
  • ... the given submatrix would cause non-deterministic results in a symmetric/Hermitian matrix.

ctranspose()

The ctranspose() function can be used to perform an in-place conjugate transpose operation:

blaze::DynamicMatrix<int,rowMajor> M( 5UL, 2UL );

ctranspose( M );  // In-place conjugate transpose operation.
M = ctrans( M );  // Same as above

Note however that the conjugate transpose operation fails if ...

  • ... the given matrix has a fixed size and is non-square;
  • ... the given matrix is a triangular matrix;
  • ... the given submatrix affects the restricted parts of a triangular matrix;
  • ... the given submatrix would cause non-deterministic results in a symmetric/Hermitian matrix.

swap()

Via the swap() function it is possible to completely swap the contents of two matrices of the same type:

blaze::DynamicMatrix<int,blaze::rowMajor> M1( 10UL, 15UL );
blaze::DynamicMatrix<int,blaze::rowMajor> M2( 20UL, 10UL );

swap( M1, M2 );  // Swapping the contents of M1 and M2

Arithmetic Operations

min() / max()

The min() and max() functions can be used for a single matrix, multiple matrices, and a matrix and a scalar.

Single Matrix

If passed a single matrix, the functions return the smallest and largest element of the given dense matrix or the smallest and largest non-zero element of the given sparse matrix, respectively:

blaze::StaticMatrix<int,2UL,3UL> A{ { -5, 2, 7 },
                                    { -4, 0, 1 } };

min( A );  // Returns -5
max( A );  // Returns 7
blaze::CompressedMatrix<int> B{ { 1, 0, 3 },
                                { 0, 0, 0 } };

min( B );  // Returns 1
max( B );  // Returns 3

For more information on the unary min() and max() reduction operations see the Reduction Operations section.

Multiple Matrices

If passed two or more dense matrices, the min() and max() functions compute the componentwise minimum or maximum of the given matrices, respectively:

blaze::StaticMatrix<int,2UL,3UL,rowMajor> C{ { -5, 1, -7 }, { 4, 1, 0 } };
blaze::StaticMatrix<int,2UL,3UL,rowMajor> D{ { -5, 3,  0 }, { 2, 2, -2 } };

min( A, C );     // Results in the matrix ( -5, 1, -7 ) ( -4, 0, 0 )
max( A, C, D );  // Results in the matrix ( -5, 3, 7 ) (  4, 2, 1 )

Please note that sparse matrices can only be used in the unary min() and max() functions. Also note that all forms of the min() and max() functions can be used to compute the smallest and largest element of a matrix expression:

min( A + B + C );  // Returns -9, i.e. the smallest value of the resulting matrix
max( A - B - C );  // Returns 11, i.e. the largest value of the resulting matrix

Matrix and Scalar

If passed a dense matrix and a scalar, the min() and max() functions compute the componentwise minimum or maximum between the given matrix and a uniform matrix represented by the scalar value:

min( A, 0 );  // Results in the matrix ( 0, 2, 7 ) ( 0, 0, 1 )
min( 0, A );  // Results in the matrix ( 0, 2, 7 ) ( 0, 0, 1 )
max( A, 0 );  // Results in the matrix ( -5, 0, 0 ) ( -4, 0, 0 )
max( 0, A );  // Results in the matrix ( -5, 0, 0 ) ( -4, 0, 0 )

softmax()

The softmax function, also called the normalized exponential function, of a given dense matrix can be computed via softmax(). The resulting dense matrix consists of real values in the range (0..1], which add up to 1.

blaze::StaticMatrix<double,3UL,3UL> A{ { 1.0, 2.0, 3.0 }
                                     , { 4.0, 1.0, 2.0 }
                                     , { 3.0, 4.0, 1.0 } };
blaze::StaticMatrix<double,3UL,3UL> B;

// Evaluating the softmax function
B = softmax( A );  // Results in ( 0.0157764  0.0428847  0.116573  )
                   //            ( 0.316878   0.0157764  0.0428847 )
                   //            ( 0.116573   0.316878   0.0157764 )

double b = sum( B );  // Results in 1

Alternatively it is possible to compute a row- or columnwise softmax function. The resulting dense matrix consists of real values in the range (0..1], which add up to the number of rows or columns, respectively.

using blaze::rowwise;
using blaze::columnwise;

blaze::StaticMatrix<double,3UL,3UL> C, D;

// Evaluating the rowwise softmax function
C = softmax<rowwise>( A );  // Results in ( 0.0900306  0.244728   0.665241 )
                            //            ( 0.843795   0.0420101  0.114195 )
                            //            ( 0.259496   0.705385   0.035119 )

double c = sum( C );  // Results in 3 (the number of rows of A)

// Evaluating the columnwise softmax function
D = softmax<columnwise>( A );  // Results in ( 0.035119  0.114195   0.665241  )
                               //            ( 0.705385  0.0420101  0.244728  )
                               //            ( 0.259496  0.843795   0.0900306 )

double d = sum( D );  // Results in 3 (the number of columns of A)

trace()

The trace() function sums the diagonal elements of a square dense or sparse matrix:

blaze::StaticMatrix<int,3UL,3UL> A{ { -1,  2, -3 }
                                  , { -4, -5,  6 }
                                  , {  7, -8, -9 } };

trace( A );  // Returns the sum of the diagonal elements, i.e. -15

In case the given matrix is not a square matrix, a std::invalid_argument exception is thrown.

det()

The determinant of a square dense matrix can be computed by means of the det() function:

blaze::DynamicMatrix<double,blaze::rowMajor> A;
// ... Resizing and initialization
double d = det( A );  // Compute the determinant of A

In case the given dense matrix is not a square matrix, a std::invalid_argument exception is thrown.

Note that the det() function can only be used for dense matrices with float, double, complex<float> or complex<double> element type. The attempt to call the function with matrices of any other element type or with a sparse matrix results in a compile time error!

Also note that the function is depending on LAPACK kernels. Thus the function can only be used if a fitting LAPACK library is available and linked to the executable. Otherwise a linker error will be created.

rank()

The rank() function computes the rank of a given dense matrix:

blaze::DynamicMatrix<double> A( 5UL, 8UL );
// ... Initialization
rank( A );

The rank is determined as the number of singular values greater than a given tolerance. This tolerance is computed as

tolerance = max(m,n) * max(s) * epsilon,

where m is the number of rows of the dense matrix, n is the number of columns of the dense matrix, max(s) is the maximum singular value of the dense matrix and epsilon is the difference between 1 and the least value greater than 1 that is representable by the floating point type of the singular values.

Note that the rank() function can only be used for dense matrices with float, double, complex<float> or complex<double> element type. The attempt to call the function with matrices of any other element type or with a sparse matrix results in a compile time error!

Also note that the function is depending on LAPACK kernels. Thus the function can only be used if a fitting LAPACK library is available and linked to the executable. Otherwise a linker error will be created.

abs()

The abs() function can be used to compute the absolute values of each element of a matrix. For instance, the following computation

blaze::StaticMatrix<int,2UL,3UL,rowMajor> A{ { -1,  2, -3 },
                                             {  4, -5,  6 } };
blaze::StaticMatrix<int,2UL,3UL,rowMajor> B( abs( A ) );

results in the matrix

B = ( 1 2 3 )
    ( 4 5 6 )

sign()

The sign() function can be used to evaluate the sign of each element of a matrix A. For each element (i,j) the corresponding result is 1 if A(i,j) is greater than zero, 0 if A(i,j) is zero, and -1 if A(i,j) is less than zero. For instance, the following use of the sign() function

blaze::StaticMatrix<int,2UL,3UL,rowMajor> A{ { -1,  2,  0 },
                                             {  4,  0, -6 } };
blaze::StaticMatrix<int,2UL,3UL,rowMajor> B( sign( A ) );

results in the matrix

B = ( -1  1  0 )
    (  1  0 -1 )

floor() / ceil() / trunc() / round()

The floor(), ceil(), trunc(), and round() functions can be used to round down/up each element of a matrix, respectively:

blaze::StaticMatrix<double,3UL,3UL> A, B;

B = floor( A );  // Rounding down each element of the matrix
B = ceil ( A );  // Rounding up each element of the matrix
B = trunc( A );  // Truncating each element of the matrix
B = round( A );  // Rounding each element of the matrix

conj()

The conj() function can be applied on a dense or sparse matrix to compute the complex conjugate of each element of the matrix:

using blaze::StaticMatrix;

using cplx = std::complex<double>;

// Creating the matrix
//    ( (1,0)  (-2,-1) )
//    ( (1,1)  ( 0, 1) )
StaticMatrix<cplx,2UL,2UL> A{ { cplx( 1.0, 0.0 ), cplx( -2.0, -1.0 ) },
                              { cplx( 1.0, 1.0 ), cplx(  0.0,  1.0 ) } };

// Computing the matrix of conjugate values
//    ( (1, 0)  (-2, 1) )
//    ( (1,-1)  ( 0,-1) )
StaticMatrix<cplx,2UL,2UL> B;
B = conj( A );

Additionally, matrices can be conjugated in-place via the conjugate() function:

blaze::DynamicMatrix<cplx> C( 5UL, 2UL );

conjugate( C );  // In-place conjugate operation.
C = conj( C );   // Same as above

real()

The real() function can be used on a dense or sparse matrix to extract the real part of each element of the matrix:

using blaze::StaticMatrix;

using cplx = std::complex<double>;

// Creating the matrix
//    ( (1,0)  (-2,-1) )
//    ( (1,1)  ( 0, 1) )
StaticMatrix<cplx,2UL,2UL> A{ { cplx( 1.0, 0.0 ), cplx( -2.0, -1.0 ) },
                              { cplx( 1.0, 1.0 ), cplx(  0.0,  1.0 ) } };

// Extracting the real part of each matrix element
//    ( 1 -2 )
//    ( 1  0 )
StaticMatrix<double,2UL,2UL> B;
B = real( A );

imag()

The imag() function can be used on a dense or sparse matrix to extract the imaginary part of each element of the matrix:

using blaze::StaticMatrix;

using cplx = std::complex<double>;

// Creating the matrix
//    ( (1,0)  (-2,-1) )
//    ( (1,1)  ( 0, 1) )
StaticMatrix<cplx,2UL,2UL> A{ { cplx( 1.0, 0.0 ), cplx( -2.0, -1.0 ) },
                              { cplx( 1.0, 1.0 ), cplx(  0.0,  1.0 ) } };

// Extracting the imaginary part of each matrix element
//    ( 0 -1 )
//    ( 1  1 )
StaticMatrix<double,2UL,2UL> B;
B = imag( A );

arg()

The arg() function can be used on a dense or sparse matrix to compute the phase angle for each element of the matrix:

using blaze::StaticMatrix;

using cplx = std::complex<double>;

// Creating the matrix
//    ( (1,0)  (-2,-1) )
//    ( (1,1)  ( 0, 1) )
StaticMatrix<cplx,2UL,2UL> A{ { cplx( 1.0, 0.0 ), cplx( -2.0, -1.0 ) },
                              { cplx( 1.0, 1.0 ), cplx(  0.0,  1.0 ) } };

// Computing the phase angle of each matrix element
//    ( 0.0      -2.67795 )
//    ( 0.785398  1.5708  )
StaticMatrix<double,2UL,2UL> B;
B = arg( A );

sqrt() / invsqrt()

Via the sqrt() and invsqrt() functions the (inverse) square root of each element of a matrix can be computed:

blaze::StaticMatrix<double,3UL,3UL> A, B, C;

B = sqrt( A );     // Computes the square root of each element
C = invsqrt( A );  // Computes the inverse square root of each element

Note that in case of sparse matrices only the non-zero elements are taken into account!

cbrt() / invcbrt()

The cbrt() and invcbrt() functions can be used to compute the the (inverse) cubic root of each element of a matrix:

blaze::DynamicMatrix<double> A, B, C;

B = cbrt( A );     // Computes the cubic root of each element
C = invcbrt( A );  // Computes the inverse cubic root of each element

Note that in case of sparse matrices only the non-zero elements are taken into account!

hypot()

The hypot() function can be used to compute the componentwise hypotenous for a pair of dense matrices:

blaze::StaticMatrix<double,3UL,3UL> A, B, C;

C = hypot( A, B );  // Computes the componentwise hypotenuous

clamp()

The clamp() function can be used to restrict all elements of a matrix to a specific range:

blaze::DynamicMatrix<double> A, B;

B = clamp( A, -1.0, 1.0 );  // Restrict all elements to the range [-1..1]

Note that in case of sparse matrices only the non-zero elements are taken into account!

pow()

The pow() function can be used to compute the exponential value of each element of a matrix. If passed a matrix and a numeric exponent, the function computes the exponential value of each element of the matrix using the same exponent. If passed a second matrix, the function computes the componentwise exponential value:

blaze::StaticMatrix<double,3UL,3UL> A, B, C;

C = pow( A, 1.2 );  // Computes the exponential value of each element
C = pow( A, B );    // Computes the componentwise exponential value

exp() / exp2() / exp10()

exp(), exp2() and exp10() compute the base e/2/10 exponential of each element of a matrix, respectively:

blaze::HybridMatrix<double,3UL,3UL> A, B;

B = exp( A );    // Computes the base e exponential of each element
B = exp2( A );   // Computes the base 2 exponential of each element
B = exp10( A );  // Computes the base 10 exponential of each element

Note that in case of sparse matrices only the non-zero elements are taken into account!

log() / log2() / log10() / log1p() / lgamma()

The log(), log2(), log10(), log1p() and lgamma() functions can be used to compute the natural, binary and common logarithm of each element of a matrix:

blaze::StaticMatrix<double,3UL,3UL> A, B;

B = log( A );     // Computes the natural logarithm of each element
B = log2( A );    // Computes the binary logarithm of each element
B = log10( A );   // Computes the common logarithm of each element
B = log1p( A );   // Computes the natural logarithm of x+1 of each element
B = lgamma( A );  // Computes the natural logarithm of the absolute value of the gamma function

sin() / cos() / tan() / asin() / acos() / atan()

The following trigonometric functions are available for both dense and sparse matrices:

blaze::DynamicMatrix<double> A, B;

B = sin( A );  // Computes the sine of each element of the matrix
B = cos( A );  // Computes the cosine of each element of the matrix
B = tan( A );  // Computes the tangent of each element of the matrix

B = asin( A );  // Computes the inverse sine of each element of the matrix
B = acos( A );  // Computes the inverse cosine of each element of the matrix
B = atan( A );  // Computes the inverse tangent of each element of the matrix

Note that in case of sparse matrices only the non-zero elements are taken into account!

sinh() / cosh() / tanh() / asinh() / acosh() / atanh()

The following hyperbolic functions are available for both dense and sparse matrices:

blaze::DynamicMatrix<double> A, B;

B = sinh( A );  // Computes the hyperbolic sine of each element of the matrix
B = cosh( A );  // Computes the hyperbolic cosine of each element of the matrix
B = tanh( A );  // Computes the hyperbolic tangent of each element of the matrix

B = asinh( A );  // Computes the inverse hyperbolic sine of each element of the matrix
B = acosh( A );  // Computes the inverse hyperbolic cosine of each element of the matrix
B = atanh( A );  // Computes the inverse hyperbolic tangent of each element of the matrix

atan2()

The multi-valued inverse tangent is available for a pair of dense matrices:

blaze::DynamicMatrix<double> A, B, C;

C = atan2( A, B );  // Computes the componentwise multi-valued inverse tangent

erf() / erfc()

The erf() and erfc() functions compute the (complementary) error function of each element of a matrix:

blaze::StaticMatrix<double,3UL,3UL> A, B;

B = erf( A );   // Computes the error function of each element
B = erfc( A );  // Computes the complementary error function of each element

Note that in case of sparse matrices only the non-zero elements are taken into account!

map() / forEach()

Via the map() functions it is possible to execute componentwise custom operations on matrices. The unary map() function can be used to apply a custom operation on each element of a dense or sparse matrix. For instance, the following example demonstrates a custom square root computation via a lambda:

blaze::DynamicMatrix<double> A, B;

B = map( A, []( double d ) { return std::sqrt( d ); } );

The N-ary map() functions can be used to apply an operation componentwise to the elements of N dense matrices (where N <= 6). The following example demonstrates the merging of two matrices of double precision values into a matrix of double precision complex numbers:

blaze::DynamicMatrix<double> real{ { 2.1, -4.2 }, { 1.0,  0.6 } };
blaze::DynamicMatrix<double> imag{ { 0.3,  1.4 }, { 2.9, -3.4 } };

blaze::DynamicMatrix< complex<double> > cplx;

// Creating the matrix
//    ( ( 2.1,  0.3) (-4.2,  1.4) )
//    ( ( 1.0,  2.9) ( 0.6, -3.4) )
cplx = map( real, imag, []( double r, double i ){ return complex<double>( r, i ); } );

Although the computation can be parallelized it is not vectorized and thus cannot perform at peak performance. However, it is also possible to create vectorized custom operations. See Custom Operations for a detailed overview of the possibilities of custom operations.

Please note that unary custom operations on vectors have been introduced in Blaze 3.0 in form of the forEach() function. With the introduction of binary custom functions, the forEach() function has been renamed to map(). The forEach() function can still be used, but the function might be deprecated in future releases of Blaze.

select()

The select() function performs a componentwise, conditional selection of elements. Given the three dense matrices cond, A, and B, in case an element in the cond vector evaluates to true, the according element of A is selected, in case the cond element evaluates to false, the according element of B is selected. The following example demonstrates the use of the select() function:

blaze::DynamicMatrix<bool> cond{ { true, false }, { true false } };
blaze::DynamicMatrix<int> A{ { 1, -1 }, { 1, -1 } };
blaze::DynamicMatrix<int> B{ { -2, 2 }, { -2, 2 } };
blaze::DynamicMatrix<int> C;
// ... Resizing and initialization

C = select( cond, A, B );  // Results in ( 1, 2 ) ( 1, 2 )

Reduction Operations

reduce()

The reduce() function performs either a total reduction, a rowwise reduction or a columnwise reduction of the elements of the given dense matrix or the non-zero elements of the given sparse matrix. The following examples demonstrate the total reduction of a dense and sparse matrix:

blaze::DynamicMatrix<double> A;
// ... Resizing and initialization

const double totalsum1 = reduce( A, blaze::Add() );
const double totalsum2 = reduce( A, []( double a, double b ){ return a + b; } );
blaze::CompressedMatrix<double> A;
// ... Resizing and initialization

const double totalsum1 = reduce( A, blaze::Add() );
const double totalsum2 = reduce( A, []( double a, double b ){ return a + b; } );

By specifying blaze::columnwise or blaze::rowwise the reduce() function performs a column-wise or row-wise reduction, respectively. In case blaze::columnwise is specified, the (non-zero) elements of the matrix are reduced column-wise and the result is a row vector. In case blaze::rowwise is specified, the (non-zero) elements of the matrix are reduced row-wise and the result is a column vector:

blaze::DynamicMatrix<double> A;
blaze::CompressedMatrix<double> B;
blaze::DynamicVector<double,rowVector> colsum1, colsum2;
// ... Resizing and initialization

colsum1 = reduce<columnwise>( A, blaze::Add() );
colsum2 = reduce<columnwise>( B, []( double a, double b ){ return a + b; } );
blaze::DynamicMatrix<double> A;
blaze::CompressedMatrix<double> B;
blaze::DynamicVector<double,columnVector> rowsum1, rowsum2;
// ... Resizing and initialization

rowsum1 = reduce<rowwise>( A, blaze::Add() );
rowsum2 = reduce<rowwise>( B, []( double a, double b ){ return a + b; } );

As demonstrated in the examples it is possible to pass any binary callable as custom reduction operation. However, for instance in the case of lambdas the vectorization of the reduction operation is compiler dependent and might not perform at peak performance. However, it is also possible to create vectorized custom operations. See Custom Operations for a detailed overview of the possibilities of custom operations.

Please note that the evaluation order of the reduce() function is unspecified. Thus the behavior is non-deterministic if the given reduction operation is not associative or not commutative. Also, the operation is undefined if the given reduction operation modifies the values.

sum()

The sum() function reduces the elements of the given dense vector or the non-zero elements of the given sparse vector by means of addition:

blaze::DynamicMatrix<int> A{ { 1, 2 }, { 3, 4 } };

const int totalsum = sum( A );  // Results in 10
blaze::CompressedMatrix<int> a{ { 1, 2 }, { 3, 4 } };

const int totalsum = sum( A );  // Results in 10

By specifying blaze::columnwise or blaze::rowwise the sum() function performs a column-wise or row-wise summation, respectively. In case blaze::columnwise is specified, the (non-zero) elements of the matrix are summed up column-wise and the result is a row vector. In case blaze::rowwise is specified, the (non-zero) elements of the matrix are summed up row-wise and the result is a column vector:

using blaze::columnwise;

blaze::DynamicMatrix<int> A{ { 1, 0, 2 }, { 1, 3, 4 } };
blaze::CompressedMatrix<int> B{ { 1, 0, 2 }, { 1, 3, 4 } };
blaze::DynamicVector<int,rowVector> colsum1, colsum2;

colsum1 = sum<columnwise>( A );  // Results in ( 2, 3, 6 )
colsum2 = sum<columnwise>( B );  // Same result
using blaze::rowwise;

blaze::DynamicMatrix<int> A{ { 1, 0, 2 }, { 1, 3, 4 } };
blaze::CompressedMatrix<int> B{ { 1, 0, 2 }, { 1, 3, 4 } };
blaze::DynamicVector<int,columnVector> rowsum1, rowsum2;

rowsum1 = sum<rowwise>( A );  // Results in ( 3, 8 )
rowsum2 = sum<rowwise>( B );  // Same result

Please note that the evaluation order of the sum() function is unspecified.

prod()

The prod() function reduces the elements of the given dense vector or the non-zero elements of the given sparse vector by means of multiplication:

blaze::DynamicMatrix<int> A{ { 1, 2 }, { 3, 4 } };

const int totalprod = prod( A );  // Results in 24
blaze::CompressedMatrix<int> A{ { 1, 2 }, { 3, 4 } };

const int totalprod = prod( A );  // Results in 24

By specifying blaze::columnwise or blaze::rowwise the prod() function performs a column-wise or row-wise multiplication, respectively. In case blaze::columnwise is specified, the (non-zero) elements of the matrix are multiplied column-wise and the result is a row vector. In case blaze::rowwise is specified, the (non-zero) elements of the matrix are multiplied row-wise and the result is a column vector:

using blaze::columnwise;

blaze::DynamicMatrix<int> A{ { 1, 0, 2 }, { 1, 3, 4 } };
blaze::CompressedMatrix<int> B{ { 1, 0, 2 }, { 1, 3, 4 } };
blaze::DynamicVector<int,rowVector> colprod1, colprod2;

colprod1 = prod<columnwise>( A );  // Results in ( 1, 0, 8 )
colprod2 = prod<columnwise>( A );  // Results in ( 1, 3, 8 )
using blaze::rowwise;

blaze::DynamicMatrix<int> A{ { 1, 0, 2 }, { 1, 3, 4 } };
blaze::CompressedMatrix<int> B{ { 1, 0, 2 }, { 1, 3, 4 } };
blaze::DynamicVector<int,columnVector> rowprod1, rowprod2;

rowprod1 = prod<rowwise>( A );  // Results in ( 0, 12 )
rowprod2 = prod<rowwise>( A );  // Results in ( 2, 12 )

Please note that the evaluation order of the prod() function is unspecified.

min()

The unary min() function returns the smallest element of the given dense matrix or the smallest non-zero element of the given sparse matrix. This function can only be used for element types that support the smaller-than relationship. In case the given matrix currently has either 0 rows or 0 columns, the returned value is the default value (e.g. 0 in case of fundamental data types).

blaze::DynamicMatrix<int> A{ { 1, 2 }, { 3, 4 } };

const int totalmin = min( A );  // Results in 1
blaze::CompressedMatrix<int> A{ { 1, 0 }, { 3, 0 } };

const int totalmin = min( A );  // Results in 1

Please note that in case the sparse matrix is not completely filled, the implicit zero elements are NOT taken into account. In the previous example the compressed matrix has only 2 non-zero elements. However, the minimum of this matrix is 1.

By specifying blaze::columnwise or blaze::rowwise the min() function determines the smallest (non-zero) element in each row or column, respectively. In case blaze::columnwise is specified, the smallest (non-zero) element of each column is determined and the result is a row vector. In case blaze::rowwise is specified, the smallest (non-zero) element of each row is determined and the result is a column vector.

using blaze::columnwise;

blaze::DynamicMatrix<int> A{ { 1, 0, 2 }, { 1, 3, 4 } };
blaze::CompressedMatrix<int> B{ { 1, 0, 2 }, { 1, 3, 4 } };
blaze::DynamicVector<int,rowVector> colmin1, colmin2;

colmin1 = min<columnwise>( A );  // Results in ( 1, 0, 2 )
colmin2 = min<columnwise>( B );  // Results in ( 1, 3, 2 )
using blaze::rowwise;

blaze::DynamicMatrix<int> A{ { 1, 0, 2 }, { 1, 3, 4 } };
blaze::CompressedMatrix<int> B{ { 1, 0, 2 }, { 1, 3, 4 } };
blaze::DynamicVector<int,columnVector> rowmin1, rowmin2;

rowmin1 = min<rowwise>( A );  // Results in ( 0, 1 )
rowmin2 = min<rowwise>( B );  // Results in ( 1, 1 )

Please note that in case the sparse matrix is not completely filled, the implicit zero elements are NOT taken into account.

max()

The unary {max()} function returns the largest element of the given dense matrix or the largest non-zero element of the given sparse matrix. This function can only be used for element types that support the smaller-than relationship. In case the given matrix currently has either 0 rows or 0 columns, the returned value is the default value (e.g. 0 in case of fundamental data types).

blaze::DynamicMatrix<int> A{ { 1, 2 }, { 3, 4 } };

const int totalmax = max( A );  // Results in 4
blaze::CompressedMatrix<int> A{ { -1, 0 }, { -3, 0 } };

const int totalmax = max( A );  // Results in -1

Please note that in case the sparse matrix is not completely filled, the implicit zero elements are NOT taken into account. In the previous example the compressed matrix has only 2 non-zero elements. However, the maximum of this matrix is -1.

By specifying blaze::columnwise or blaze::rowwise the max() function determines the largest (non-zero) element in each row or column, respectively. In case blaze::columnwise is specified, the largest (non-zero) element of each column is determined and the result is a row vector. In case blaze::rowwise is specified, the largest (non-zero) element of each row is determined and the result is a column vector.

using blaze::columnwise;

blaze::DynamicMatrix<int> A{ { 1, 0, 2 }, { 1, 3, 4 } };
blaze::CompressedMatrix<int> B{ { -1, 0, -2 }, { -1, -3, -4 } };
blaze::DynamicVector<int,rowVector> colmax1, colmax2;

colmax1 = max<columnwise>( A );  // Results in ( 1, 3, 4 )
colmax2 = max<columnwise>( B );  // Results in ( -1, -3, -2 )
using blaze::rowwise;

blaze::DynamicMatrix<int> A{ { 1, 0, 2 }, { 1, 3, 4 } };
blaze::CompressedMatrix<int> B{ { -1, 0, -2 }, { -1, -3, -4 } };
blaze::DynamicVector<int,columnVector> rowmax1, rowmax2;

rowmax1 = max<rowwise>( A );  // Results in ( 2, 4 )
rowmax2 = max<rowwise>( B );  // Results in ( -1, -1 )

Please note that in case the sparse matrix is not completely filled, the implicit zero elements are NOT taken into account.


Norms

norm()

The norm() function computes the L2 norm of the given dense or sparse matrix:

blaze::DynamicMatrix<double> A;
blaze::CompressedMatrix<double> B;
// ... Resizing and initialization

const double norm1 = norm( A );
const double norm2 = norm( B );

sqrNorm()

The sqrNorm() function computes the squared L2 norm of the given dense or sparse matrix:

blaze::DynamicMatrix<double> A;
blaze::CompressedMatrix<double> B;
// ... Resizing and initialization

const double norm1 = sqrNorm( A );
const double norm2 = sqrNorm( B );

l1Norm()

The l1Norm() function computes the squared L1 norm of the given dense or sparse matrix:

blaze::DynamicMatrix<double> A;
blaze::CompressedMatrix<double> B;
// ... Resizing and initialization

const double norm1 = l1Norm( A );
const double norm2 = l1Norm( B );

l2Norm()

The l2Norm() function computes the squared L2 norm of the given dense or sparse matrix:

blaze::DynamicMatrix<double> A;
blaze::CompressedMatrix<double> B;
// ... Resizing and initialization

const double norm1 = l2Norm( A );
const double norm2 = l2Norm( B );

l3Norm()

The l3Norm() function computes the squared L3 norm of the given dense or sparse matrix:

blaze::DynamicMatrix<double> A;
blaze::CompressedMatrix<double> B;
// ... Resizing and initialization

const double norm1 = l3Norm( A );
const double norm2 = l3Norm( B );

l4Norm()

The l4Norm() function computes the squared L4 norm of the given dense or sparse matrix:

blaze::DynamicMatrix<double> A;
blaze::CompressedMatrix<double> B;
// ... Resizing and initialization

const double norm1 = l4Norm( A );
const double norm2 = l4Norm( B );

lpNorm()

The lpNorm() function computes the general Lp norm of the given dense or sparse matrix, where the norm is specified by either a compile time or a runtime argument:

blaze::DynamicMatrix<double> A;
blaze::CompressedMatrix<double> B;
// ... Resizing and initialization

const double norm1 = lpNorm<2>( A );    // Compile time argument
const double norm2 = lpNorm( B, 2.3 );  // Runtime argument

linfNorm() / maxNorm()

The linfNorm() and maxNorm() functions compute the infinity/maximum norm of the given dense or sparse matrix:

blaze::DynamicMatrix<double> A;
blaze::CompressedMatrix<double> B;
// ... Resizing and initialization

const double norm1 = linfNorm( A );
const double norm2 = maxNorm( B );

Scalar Expansion

By means of the uniform() function it is possible to expand a scalar value into a dense, uniform matrix. By default, the resulting uniform matrix is a row-major matrix, but it is possible to specify the storage order explicitly:

using blaze::rowMajor;

int scalar = 5;

blaze::DynamicMatrix<int,rowMajor> A;
// ... Resizing and initialization

// Expansion of 'scalar' to a 3x5 row-major matrix
//
//    ( 5  5  5  5  5 )
//    ( 5  5  5  5  5 )
//    ( 5  5  5  5  5 )
//
A = uniform( 3UL, 5UL, scalar );
A = uniform<columnMajor>( 3UL, 5UL, scalar );

Matrix Repetition

Via the repeat() function it is possible to repeat a dense or sparse matrix multiple times to represent a larger matrix. Repeating a row-major matrix results in a row-major matrix, repeating a column-major matrix results in a column-major matrix. As demonstrated by the following examples, repeat() can be used with both runtime and compile time parameters:

using blaze::rowMajor;
using blaze::columnMajor;

blaze::DynamicMatrix<int,rowMajor> A1{ { 1, 0, -2 }, { 0, 5, 0 } };
blaze::CompressedMatrix<int,columnMajor> B1{ { 0, -1 }, { 0, 4 }, { 7, 0 } };

blaze::DynamicMatrix<int,rowMajor> A2;
blaze::CompressedMatrix<int,columnMajor> B2;

// ... Resizing and initialization

// Repeating the 2x3 dense row-major matrix 'A1' 2x rowwise and 3x columnwise results in
//
//    (  1  0 -2  1  0 -2  1  0 -2 )
//    (  0  5  0  0  5  0  0  5  0 )
//    (  1  0 -2  1  0 -2  1  0 -2 )
//    (  0  5  0  0  5  0  0  5  0 )
//
A2 = repeat( A1, 2UL, 3UL );
A2 = repeat<2UL,3UL>( A1 );

// Repeating the 3x2 sparse column-major matrix 'B1' 2x rowwise and 3x columnwise results in
//
//    (  0 -1  0 -1  0 -1 )
//    (  0  4  0  4  0  4 )
//    (  7  0  7  0  7  0 )
//    (  0 -1  0 -1  0 -1 )
//    (  0  4  0  4  0  4 )
//    (  7  0  7  0  7  0 )
//
B2 = repeat( B1, 2UL, 3UL );
B2 = repeat<2UL,3UL>( B1 );

Statistic Operations

mean()

The (arithmetic) mean of a dense or sparse matrix can be computed via the mean() function. In case of a sparse matrix, both the non-zero and zero elements are taken into account. The following example demonstrates the computation of the mean of a dense matrix:

blaze::DynamicMatrix<int> A{ { 1, 4, 3, 6, 7 }
                           , { 2, 6, 3, 1, 0 } };

const double m = mean( A );  // Results in 3.3 (i.e. 33/10)

In case the number of rows or columns of the given matrix is 0, a std::invalid_argument is thrown.

Alternatively it is possible to compute the row- or columnwise mean:

using blaze::columnVector;
using blaze::rowVector;

blaze::DynamicMatrix<int> A{ { 1, 4, 3, 6, 7 }
                           , { 2, 6, 3, 1, 0 } };

blaze::DynamicVector<double,columnVector> rm;
blaze::DynamicVector<double,rowVector> cm;

rm = mean<rowwise>( A );     // Results in ( 4.2  2.4 )
cm = mean<columnwise>( A );  // Results in ( 1.5  5.0  3.0  3.5  3.5 )

In case the rowwise mean is computed and the number of columns of the given matrix is 0 or in case the columnwise mean is computed and the number of rows of the given matrix is 0, a std::invalid_argument is thrown.

var()

The variance of a dense or sparse matrix can be computed via the var() function. In case of a sparse vector, both the non-zero and zero elements are taken into account. The following example demonstrates the computation of the variance of a dense matrix:

blaze::DynamicMatrix<int> A{ { 1, 3, 2 }
                           , { 2, 6, 4 }
                           , { 9, 6, 3 } };

const double v = var( A );  // Results in 6.5

In case the size of the given matrix is smaller than 2, a std::invalid_argument is thrown.

Alternatively it is possible to compute the row- or columnwise variance:

using blaze::columnVector;
using blaze::rowVector;

blaze::DynamicMatrix<int> A{ { 1, 3, 2 }
                           , { 2, 6, 4 }
                           , { 9, 6, 3 } };

blaze::DynamicVector<double,columnVector> rv;
blaze::DynamicVector<double,rowVector> cv;

rv = var<rowwise>( A );     // Results in ( 1  4  9 )
cv = var<columnwise>( A );  // Results in ( 19  3  1 )

In case the rowwise variance is computed and the number of columns of the given matrix is smaller than 2 or in case the columnwise mean is computed and the number of rows of the given matrix is smaller than 2, a std::invalid_argument is thrown.

stddev()

The standard deviation of a dense or sparse matrix can be computed via the stddev() function. In case of a sparse vector, both the non-zero and zero elements are taken into account. The following example demonstrates the computation of the standard deviation of a dense matrix:

blaze::DynamicMatrix<int> A{ { 1, 3, 2 }
                           , { 2, 6, 4 }
                           , { 9, 6, 3 } };

const double s = stddev( A );  // Results in sqrt(6.5)

In case the size of the given matrix is smaller than 2, a std::invalid_argument is thrown.

Alternatively it is possible to compute the row- or columnwise standard variance:

using blaze::columnVector;
using blaze::rowVector;

blaze::DynamicMatrix<int> A{ { 1, 3, 2 }
                           , { 2, 6, 4 }
                           , { 9, 6, 3 } };

blaze::DynamicVector<double,columnVector> rs;
blaze::DynamicVector<double,rowVector> cs;

rs = stddev<rowwise>( A );     // Results in ( 1  2  3 )
cs = stddev<columnwise>( A );  // Results in ( sqrt(19)  sqrt(3)  1 )

In case the rowwise standard variance is computed and the number of columns of the given matrix is smaller than 2 or in case the columnwise mean is computed and the number of rows of the given matrix is smaller than 2, a std::invalid_argument is thrown.


Declaration Operations

declsym()

The declsym() operation can be used to explicitly declare any matrix or matrix expression as symmetric:

blaze::DynamicMatrix<double> A, B;
// ... Resizing and initialization

B = declsym( A );

Any matrix or matrix expression that has been declared as symmetric via declsym() will gain all the benefits of a symmetric matrix, which range from reduced runtime checking to a considerable speed-up in computations:

using blaze::DynamicMatrix;
using blaze::SymmetricMatrix;

DynamicMatrix<double> A, B, C;
SymmetricMatrix< DynamicMatrix<double> > S;
// ... Resizing and initialization

isSymmetric( declsym( A ) );  // Will always return true without runtime effort

S = declsym( A );  // Omit any runtime check for symmetry

C = declsym( A * B );  // Declare the result of the matrix multiplication as symmetric,
                       // i.e. perform an optimized matrix multiplication

Warning: The declsym() operation has the semantics of a cast: The caller is completely responsible and the system trusts the given information. Declaring a non-symmetric matrix or matrix expression as symmetric via the declsym() operation leads to undefined behavior (which can be violated invariants or wrong computation results)!

declherm()

The declherm() operation can be used to explicitly declare any matrix or matrix expression as Hermitian:

blaze::DynamicMatrix<double> A, B;
// ... Resizing and initialization

B = declherm( A );

Any matrix or matrix expression that has been declared as Hermitian via declherm() will gain all the benefits of an Hermitian matrix, which range from reduced runtime checking to a considerable speed-up in computations:

using blaze::DynamicMatrix;
using blaze::HermitianMatrix;

DynamicMatrix<double> A, B, C;
HermitianMatrix< DynamicMatrix<double> > S;
// ... Resizing and initialization

isHermitian( declherm( A ) );  // Will always return true without runtime effort

S = declherm( A );  // Omit any runtime check for Hermitian symmetry

C = declherm( A * B );  // Declare the result of the matrix multiplication as Hermitian,
                        // i.e. perform an optimized matrix multiplication

Warning: The declherm() operation has the semantics of a cast: The caller is completely responsible and the system trusts the given information. Declaring a non-Hermitian matrix or matrix expression as Hermitian via the declherm() operation leads to undefined behavior (which can be violated invariants or wrong computation results)!

decllow()

The decllow() operation can be used to explicitly declare any matrix or matrix expression as lower triangular:

blaze::DynamicMatrix<double> A, B;
// ... Resizing and initialization

B = decllow( A );

Any matrix or matrix expression that has been declared as lower triangular via decllow() will gain all the benefits of a lower triangular matrix, which range from reduced runtime checking to a considerable speed-up in computations:

using blaze::DynamicMatrix;
using blaze::LowerMatrix;

DynamicMatrix<double> A, B, C;
LowerMatrix< DynamicMatrix<double> > L;
// ... Resizing and initialization

isLower( decllow( A ) );  // Will always return true without runtime effort

L = decllow( A );  // Omit any runtime check for A being a lower matrix

C = decllow( A * B );  // Declare the result of the matrix multiplication as lower triangular,
                       // i.e. perform an optimized matrix multiplication

Warning: The decllow() operation has the semantics of a cast: The caller is completely responsible and the system trusts the given information. Declaring a non-lower matrix or matrix expression as lower triangular via the decllow() operation leads to undefined behavior (which can be violated invariants or wrong computation results)!

declunilow()

The declunilow() operation can be used to explicitly declare any matrix or matrix expression as lower unitriangular:

blaze::DynamicMatrix<double> A, B;
// ... Resizing and initialization

B = declunilow( A );

Any matrix or matrix expression that has been declared as lower unitriangular via declunilow() will gain all the benefits of a lower unitriangular matrix, which range from reduced runtime checking to a considerable speed-up in computations:

using blaze::DynamicMatrix;
using blaze::UniLowerMatrix;

DynamicMatrix<double> A, B, C;
UniLowerMatrix< DynamicMatrix<double> > L;
// ... Resizing and initialization

isUniLower( declunilow( A ) );  // Will always return true without runtime effort

L = declunilow( A );  // Omit any runtime check for A being an unilower matrix

C = declunilow( A * B );  // Declare the result of the matrix multiplication as lower
                          // unitriangular, i.e. perform an optimized matrix multiplication

Warning: The declunilow() operation has the semantics of a cast: The caller is completely responsible and the system trusts the given information. Declaring a non-unilower matrix or matrix expression as lower unitriangular via the declunilow() operation leads to undefined behavior (which can be violated invariants or wrong computation results)!

declstrlow()

The declstrlow() operation can be used to explicitly declare any matrix or matrix expression as strictly lower triangular:

blaze::DynamicMatrix<double> A, B;
// ... Resizing and initialization

B = declstrlow( A );

Any matrix or matrix expression that has been declared as strictly lower triangular via declstrlow() will gain all the benefits of a strictly lower triangular matrix, which range from reduced runtime checking to a considerable speed-up in computations:

using blaze::DynamicMatrix;
using blaze::StrictlyLowerMatrix;

DynamicMatrix<double> A, B, C;
StrictlyLowerMatrix< DynamicMatrix<double> > L;
// ... Resizing and initialization

isStrictlyLower( declstrlow( A ) );  // Will always return true without runtime effort

L = declstrlow( A );  // Omit any runtime check for A being a strictly lower matrix

C = declstrlow( A * B );  // Declare the result of the matrix multiplication as strictly lower
                          // triangular, i.e. perform an optimized matrix multiplication

Warning: The declstrlow() operation has the semantics of a cast: The caller is completely responsible and the system trusts the given information. Declaring a non-strictly-lower matrix or matrix expression as strictly lower triangular via the declstrlow() operation leads to undefined behavior (which can be violated invariants or wrong computation results)!

declupp()

The declupp() operation can be used to explicitly declare any matrix or matrix expression as upper triangular:

blaze::DynamicMatrix<double> A, B;
// ... Resizing and initialization

B = declupp( A );

Any matrix or matrix expression that has been declared as upper triangular via declupp() will gain all the benefits of an upper triangular matrix, which range from reduced runtime checking to a considerable speed-up in computations:

using blaze::DynamicMatrix;
using blaze::UpperMatrix;

DynamicMatrix<double> A, B, C;
UpperMatrix< DynamicMatrix<double> > U;
// ... Resizing and initialization

isUpper( declupp( A ) );  // Will always return true without runtime effort

U = declupp( A );  // Omit any runtime check for A being an upper matrix

C = declupp( A * B );  // Declare the result of the matrix multiplication as upper triangular,
                       // i.e. perform an optimized matrix multiplication

Warning: The declupp() operation has the semantics of a cast: The caller is completely responsible and the system trusts the given information. Declaring a non-upper matrix or matrix expression as upper triangular via the declupp() operation leads to undefined behavior (which can be violated invariants or wrong computation results)!

decluniupp()

The decluniupp() operation can be used to explicitly declare any matrix or matrix expression as upper unitriangular:

blaze::DynamicMatrix<double> A, B;
// ... Resizing and initialization

B = decluniupp( A );

Any matrix or matrix expression that has been declared as upper unitriangular via decluniupp() will gain all the benefits of a upper unitriangular matrix, which range from reduced runtime checking to a considerable speed-up in computations:

using blaze::DynamicMatrix;
using blaze::UniUpperMatrix;

DynamicMatrix<double> A, B, C;
UniUpperMatrix< DynamicMatrix<double> > L;
// ... Resizing and initialization

isUniUpper( decluniupp( A ) );  // Will always return true without runtime effort

L = decluniupp( A );  // Omit any runtime check for A being an uniupper matrix

C = decluniupp( A * B );  // Declare the result of the matrix multiplication as upper
                          // unitriangular, i.e. perform an optimized matrix multiplication

Warning: The decluniupp() operation has the semantics of a cast: The caller is completely responsible and the system trusts the given information. Declaring a non-uniupper matrix or matrix expression as upper unitriangular via the {{{decluniupp() operation leads to undefined behavior (which can be violated invariants or wrong computation results)!

declstrupp()

The declstrupp() operation can be used to explicitly declare any matrix or matrix expression as strictly upper triangular:

blaze::DynamicMatrix<double> A, B;
// ... Resizing and initialization

B = declstrupp( A );

Any matrix or matrix expression that has been declared as strictly upper triangular via declstrupp() will gain all the benefits of a strictly upper triangular matrix, which range from reduced runtime checking to a considerable speed-up in computations:

!c++
using blaze::DynamicMatrix;
using blaze::StrictlyUpperMatrix;

DynamicMatrix<double> A, B, C;
StrictlyUpperMatrix< DynamicMatrix<double> > L;
// ... Resizing and initialization

isStrictlyUpper( declstrupp( A ) );  // Will always return true without runtime effort

L = declstrupp( A );  // Omit any runtime check for A being a strictly upper matrix

C = declstrupp( A * B );  // Declare the result of the matrix multiplication as strictly upper
                          // triangular, i.e. perform an optimized matrix multiplication

Warning: The declstrupp() operation has the semantics of a cast: The caller is completely responsible and the system trusts the given information. Declaring a non-strictly-upper matrix or matrix expression as strictly upper triangular via the declstrupp() operation leads to undefined behavior (which can be violated invariants or wrong computation results)!

decldiag()

The decldiag() operation can be used to explicitly declare any matrix or matrix expression as diagonal:

blaze::DynamicMatrix<double> A, B;
// ... Resizing and initialization

B = decldiag( A );

Any matrix or matrix expression that has been declared as diagonal via decldiag() will gain all the benefits of a diagonal matrix, which range from reduced runtime checking to a considerable speed-up in computations:

using blaze::DynamicMatrix;
using blaze::DiagonalMatrix;

DynamicMatrix<double> A, B, C;
DiagonalMatrix< DynamicMatrix<double> > D;
// ... Resizing and initialization

isDiagonal( decldiag( A ) );  // Will always return true without runtime effort

D = decldiag( A );  // Omit any runtime check for A being a diagonal matrix

C = decldiag( A * B );  // Declare the result of the matrix multiplication as diagonal,
                        // i.e. perform an optimized matrix multiplication

Warning: The decldiag() operation has the semantics of a cast: The caller is completely responsible and the system trusts the given information. Declaring a non-diagonal matrix or matrix expression as diagonal via the decldiag() operation leads to undefined behavior (which can be violated invariants or wrong computation results)!

declid()

The declid() operation can be used to explicitly declare any matrix or matrix expression as identity matrix:

blaze::DynamicMatrix<double> A, B;
// ... Resizing and initialization

B = declid( A );

Any matrix or matrix expression that has been declared as identity matrix via declid() will gain all the benefits of an identity matrix, which range from reduced runtime checking to a considerable speed-up in computations:

using blaze::DynamicMatrix;
using blaze::DiagonalMatrix;

DynamicMatrix<double> A, B, C;
DiagonalMatrix< DynamicMatrix<double> > D;
// ... Resizing and initialization

isIdentity( declid( A ) );  // Will always return true without runtime effort

D = declid( A );  // Omit any runtime check for A being a diagonal matrix

C = declid( A ) * B;  // Declare the left operand of the matrix multiplication as an
                      // identity matrix, i.e. perform an optimized matrix multiplication

Warning: The declid() operation has the semantics of a cast: The caller is completely responsible and the system trusts the given information. Declaring a non-identity matrix or matrix expression as identity matrix via the declid() operation leads to undefined behavior (which can be violated invariants or wrong computation results)!

declzero()

The declzero() operation can be used to explicitly declare any matrix or matrix expression as zero matrix:

blaze::DynamicMatrix<double> A, B;
// ... Resizing and initialization

B = declzero( A );

Any matrix or matrix expression that has been declared as zero matrix via declzero() will gain all the benefits of a zero matrix, which range from reduced runtime checking to a considerable speed-up in computations:

using blaze::DynamicMatrix;

DynamicMatrix<double> A, B, C;
// ... Resizing and initialization

isZero( declzero( A ) );  // Will always return true without runtime effort

C = declzero( A ) + B;  // Declare the left operand of the matrix addition as a
                        // zero matrix, i.e. no addition needs to be performed

Warning: The declzero() operation has the semantics of a cast: The caller is completely responsible and the system trusts the given information. Declaring a non-zero matrix or matrix expression as zero matrix via the declzero() operation leads to undefined behavior (which can be violated invariants or wrong computation results)!


Matrix Generators

generate()

The generate() function returns a dense matrix filled elementwise via the given custom binary operation. By default, the returned matrix is a row-major matrix, but this setting can be changed via the BLAZE_DEFAULT_STORAGE_ORDER switch (see Default Matrix Storage). Alternatively it is possible to specify the storage order explicitly.

The following example demonstrates the use of the generate() function:

using blaze::generate;
using blaze::rowMajor;
using blaze::columnMajor>

// Generates the uniform integer matrix ( ( 2, 2, 2 ), ( 2, 2, 2 ) )
blaze::DynamicMatrix<int,rowMajor> A;
A = generate( 2UL, 3UL, []( size_t i, size_t j ){ return 2; } );

// Generates the linearly spaced float matrix ( ( 2.1, 3.2, 4.3 ), ( 5.4, 6.5, 7.6 ) )
blaze::DynamicMatrix<float,rowMajor> B;
B = generate( 2UL, 3UL, []( size_t i, size_t j ){ return 2.1F + 1.1F*(i*3UL+j); } );

// Generates the logarithmically spaced double vector ( ( 1.0, 10.0 ), ( 100.0, 1000.0 ) )
blaze::DynamicMatrix<double,rowMajor> C;
C = generate<rowMajor>( 2UL, 2UL, []( size_t i, size_t j ) { return blaze::exp10( 1.0 + 1.0*(i*2UL+j) ); } );

// Generates the vector of integer vectors ( ( 1, 2 ), ( 2, 3 ), ( 3, 4 ), ( 4, 5 ) )
using VT = StaticVector<int,2UL>;
blaze::DynamicMatrix<VT,columnMajor> D;
D = generate<columnMajor>( 2UL, 2UL, []( size_t i, size_t j ) { return evaluate( VT{ 1, 2 } + (i*2UL+j) ); } );

uniform()

The uniform() function creates a uniform matrix of the given size. By default, the resulting uniform matrix is a row-major matrix, but this setting can be changed via the BLAZE_DEFAULT_STORAGE_ORDER switch (see Default Matrix Storage). Alternatively it is possible to specify the storage order explicitly.

The following example demonstrates the use of the uniform() function:

using blaze::uniform;
using blaze::rowMajor;
using blaze::columnMajor;

// Creates the uniform row-major matrix
//    ( 1, 1, 1, 1, 1 )
//    ( 1, 1, 1, 1, 1 )
auto U1 = uniform( 2UL, 5UL, 1 );

// Creates the uniform row-major matrix
//    ( 1.2, 1.2 )
//    ( 1.2, 1.2 )
//    ( 1.2, 1.2 )
auto U2 = uniform<rowMajor>( 3UL, 2UL, 1.2 );

// Creates the uniform column-major matrix
//   ( 5U, 5U, 5U, 5U, 5U, 5U, 5U )
//   ( 5U, 5U, 5U, 5U, 5U, 5U, 5U )
auto U3 = uniform<columnMajor>( 2UL, 7UL, 5U );

zero()

The zero() function creates a zero matrix of the given element type and size. By default, the resulting zero matrix is a row-major matrix, but this setting can be changed via the BLAZE_DEFAULT_STORAGE_ORDER switch (see Default Matrix Storage). Alternatively it is possible to specify the storage order explicitly.

The following example demonstrates the use of the zero() function:

using blaze::zero;
using blaze::rowMajor;
using blaze::columnMajor;

// Creates the row-major zero matrix
//    ( 0, 0, 0, 0, 0 )
//    ( 0, 0, 0, 0, 0 )
auto Z1 = zero<int>( 2UL, 5UL );

// Creates the row-major zero matrix
//    ( 0.0, 0.0 )
//    ( 0.0, 0.0 )
//    ( 0.0, 0.0 )
auto Z2 = zero<double,rowMajor>( 3UL, 2UL );

// Creates the column-major zero matrix
//    ( 0U, 0U, 0U, 0U, 0U, 0U, 0U )
//    ( 0U, 0U, 0U, 0U, 0U, 0U, 0U )
auto Z3 = zero<unsigned int,columnMajor>( 2UL, 7UL );

Matrix Inversion

The inverse of a square dense matrix can be computed via the inv() function:

blaze::DynamicMatrix<float,blaze::rowMajor> A, B;
// ... Resizing and initialization
B = inv( A );  // Compute the inverse of A

Alternatively, an in-place inversion of a dense matrix can be performed via the invert() function:

blaze::DynamicMatrix<double,blaze::rowMajor> A;
// ... Resizing and initialization
invert( A );  // In-place matrix inversion

Both the inv() and the invert() functions will automatically select the most suited matrix inversion algorithm depending on the size and type of the given matrix. For small matrices of up to 6x6, both functions use manually optimized kernels for maximum performance. For matrices larger than 6x6 the inversion is performed by means of the most suited matrix decomposition method: In case of a general matrix the LU decomposition is used, for symmetric matrices the LDLT decomposition is applied, for Hermitian matrices the LDLH decomposition is performed, and for triangular matrices the inverse is computed via a forward or back substitution.

In case the type of the matrix does not provide additional compile time information about its structure (symmetric, lower, upper, diagonal, ...), the information can be provided manually by means of Declaration Operations when calling the invert() function:

invert( declsym( A ) );     // In-place inversion of a symmetric matrix
invert( declherm( A ) );    // In-place inversion of an Hermitian matrix
invert( decllow( A ) );     // In-place inversion of a lower triangular matrix
invert( declunilow( A ) );  // In-place inversion of a lower unitriangular matrix
invert( declupp( A ) );     // In-place inversion of an upper triangular matrix
invert( decluniupp( A ) );  // In-place inversion of an upper unitriangular matrix
invert( decldiag( A ) );    // In-place inversion of a diagonal matrix

Alternatively, via the invert() function it is possible to explicitly specify the inversion algorithm:

using blaze::byLU;
using blaze::byLDLT;
using blaze::byLDLH;
using blaze::byLLH;

// In-place inversion of a general matrix by means of an LU decomposition
invert<byLU>( A );

// In-place inversion of a symmetric indefinite matrix by means of a Bunch-Kaufman decomposition
invert<byLDLT>( A );

// In-place inversion of an Hermitian indefinite matrix by means of a Bunch-Kaufman decomposition
invert<byLDLH>( A );

// In-place inversion of a positive definite matrix by means of a Cholesky decomposition
invert<byLLH>( A );

Whereas the inversion by means of an LU decomposition works for every general square matrix, the inversion by LDLT only works for symmetric indefinite matrices, the inversion by LDLH is restricted to Hermitian indefinite matrices and the Cholesky decomposition (LLH) only works for Hermitian positive definite matrices. Please note that it is in the responsibility of the function caller to guarantee that the selected algorithm is suited for the given matrix. In case this precondition is violated the result can be wrong and might not represent the inverse of the given matrix!

For both the inv() and invert() function the matrix inversion fails if ...

  • ... the given matrix is not a square matrix;
  • ... the given matrix is singular and not invertible.

In all failure cases either a compilation error is created if the failure can be predicted at compile time or a std::invalid_argument exception is thrown.

Note that the matrix inversion can only be used for dense matrices with float, double, complex<float> or complex<double> element type. The attempt to call the function with matrices of any other element type or with a sparse matrix results in a compile time error!

Also note that the functions invert the dense matrix by means of LAPACK kernels. Thus the functions can only be used if a fitting LAPACK library is available and linked to the executable. Otherwise a linker error will be created.

Furthermore, it is important to note that it is not possible to use any kind of view on the expression object returned by the inv() function. Also, it is not possible to access individual elements via the function call operator on the expression object:

row( inv( A ), 2UL );  // Compilation error: Views cannot be used on an inv() expression!
inv( A )(1,2);         // Compilation error: It is not possible to access individual elements!

Finally, please note that the inversion functions do not provide any exception safety guarantee, i.e. in case an exception is thrown the matrix may already have been modified.


Matrix Exponential

The matrix exponential of a NxN matrix X is defined as

images/matexp.jpg

In order to compute the matrix exponential of a square dense matrix, the matexp() function can be used:

blaze::DynamicMatrix<float,blaze::rowMajor> A, B;
// ... Resizing and initialization
B = matexp( A );  // Compute the exponential of A

Note that the matrix exponential can only be used for dense matrices with float, double, complex<float> or complex<double> element type. The attempt to call the function with matrices of any other element type results in a compile time error!

Also note that it is not possible to use any kind of view on the expression object returned by the matexp() function. Also, it is not possible to access individual elements via the function call operator on the expression object:

row( matexp( A ), 2UL );  // Compilation error: Views cannot be used on an matexp() expression!
matexp( A )(1,2);         // Compilation error: It is not possible to access individual elements!

Matrix Decomposition

Note that all decomposition functions can only be used for dense matrices with float, double, complex<float> or complex<double> element type. The attempt to call the function with matrices of any other element type or with a sparse matrix results in a compile time error!

Also note that the functions decompose a dense matrix by means of LAPACK kernels. Thus the functions can only be used if a fitting LAPACK library is available and linked to the executable. Otherwise a linker error will be created.

LU Decomposition

The LU decomposition of a dense matrix can be computed via the lu() function:

blaze::DynamicMatrix<double,blaze::rowMajor> A;
// ... Resizing and initialization

blaze::DynamicMatrix<double,blaze::rowMajor> L, U, P;

lu( A, L, U, P );  // LU decomposition of a row-major matrix

assert( A == L * U * P );
blaze::DynamicMatrix<double,blaze::columnMajor> A;
// ... Resizing and initialization

blaze::DynamicMatrix<double,blaze::columnMajor> L, U, P;

lu( A, L, U, P );  // LU decomposition of a column-major matrix

assert( A == P * L * U );

The function works for both rowMajor and columnMajor matrices. Note, however, that the three matrices A, L and U are required to have the same storage order. Also, please note that the way the permutation matrix P needs to be applied differs between row-major and column-major matrices, since the algorithm uses column interchanges for row-major matrices and row interchanges for column-major matrices.

Furthermore, lu() can be used with adaptors. For instance, the following example demonstrates the LU decomposition of a symmetric matrix into a lower and upper triangular matrix:

blaze::SymmetricMatrix< blaze::DynamicMatrix<double,blaze::columnMajor> > A;
// ... Resizing and initialization

blaze::LowerMatrix< blaze::DynamicMatrix<double,blaze::columnMajor> > L;
blaze::UpperMatrix< blaze::DynamicMatrix<double,blaze::columnMajor> > U;
blaze::DynamicMatrix<double,blaze::columnMajor> P;

lu( A, L, U, P );  // LU decomposition of A

Cholesky Decomposition

The Cholesky (LLH) decomposition of a dense matrix can be computed via the llh() function:

blaze::DynamicMatrix<double,blaze::rowMajor> A;
// ... Resizing and initialization

blaze::DynamicMatrix<double,blaze::rowMajor> L;

llh( A, L );  // LLH decomposition of a row-major matrix

assert( A == L * ctrans( L ) );

The function works for both rowMajor and columnMajor matrices and the two matrices A and L can have any storage order.

Furthermore, llh() can be used with adaptors. For instance, the following example demonstrates the LLH decomposition of a symmetric matrix into a lower triangular matrix:

blaze::SymmetricMatrix< blaze::DynamicMatrix<double,blaze::columnMajor> > A;
// ... Resizing and initialization

blaze::LowerMatrix< blaze::DynamicMatrix<double,blaze::columnMajor> > L;

llh( A, L );  // Cholesky decomposition of A

QR Decomposition

The QR decomposition of a dense matrix can be computed via the qr() function:

blaze::DynamicMatrix<double,blaze::rowMajor> A;
// ... Resizing and initialization

blaze::DynamicMatrix<double,blaze::columnMajor> Q;
blaze::DynamicMatrix<double,blaze::rowMajor> R;

qr( A, Q, R );  // QR decomposition of a row-major matrix

assert( A == Q * R );

The function works for both rowMajor and columnMajor matrices and the three matrices A, Q and R can have any storage order.

Furthermore, qr() can be used with adaptors. For instance, the following example demonstrates the QR decomposition of a symmetric matrix into a general matrix and an upper triangular matrix:

blaze::SymmetricMatrix< blaze::DynamicMatrix<double,blaze::columnMajor> > A;
// ... Resizing and initialization

blaze::DynamicMatrix<double,blaze::rowMajor> Q;
blaze::UpperMatrix< blaze::DynamicMatrix<double,blaze::columnMajor> > R;

qr( A, Q, R );  // QR decomposition of A

RQ Decomposition

Similar to the QR decomposition, the RQ decomposition of a dense matrix can be computed via the rq() function:

blaze::DynamicMatrix<double,blaze::rowMajor> A;
// ... Resizing and initialization

blaze::DynamicMatrix<double,blaze::rowMajor> R;
blaze::DynamicMatrix<double,blaze::columnMajor> Q;

rq( A, R, Q );  // RQ decomposition of a row-major matrix

assert( A == R * Q );

The function works for both rowMajor and columnMajor matrices and the three matrices A, R and Q can have any storage order.

Also the \rq() function can be used in combination with matrix adaptors. For instance, the following example demonstrates the RQ decomposition of an Hermitian matrix into a general matrix and an upper triangular matrix:

blaze::HermitianMatrix< blaze::DynamicMatrix<complex<double>,blaze::columnMajor> > A;
// ... Resizing and initialization

blaze::UpperMatrix< blaze::DynamicMatrix<complex<double>,blaze::columnMajor> > R;
blaze::DynamicMatrix<complex<double>,blaze::rowMajor> Q;

rq( A, R, Q );  // RQ decomposition of A

QL Decomposition

The QL decomposition of a dense matrix can be computed via the ql() function:

blaze::DynamicMatrix<double,blaze::rowMajor> A;
// ... Resizing and initialization

blaze::DynamicMatrix<double,blaze::rowMajor> Q;
blaze::DynamicMatrix<double,blaze::columnMajor> L;

ql( A, Q, L );  // QL decomposition of a row-major matrix

assert( A == Q * L );

The function works for both rowMajor and columnMajor matrices and the three matrices A, Q and L can have any storage order.

Also the ql() function can be used in combination with matrix adaptors. For instance, the following example demonstrates the QL decomposition of a symmetric matrix into a general matrix and a lower triangular matrix:

blaze::SymmetricMatrix< blaze::DynamicMatrix<double,blaze::columnMajor> > A;
// ... Resizing and initialization

blaze::DynamicMatrix<double,blaze::rowMajor> Q;
blaze::LowerMatrix< blaze::DynamicMatrix<double,blaze::columnMajor> > L;

ql( A, Q, L );  // QL decomposition of A

LQ Decomposition

The LQ decomposition of a dense matrix can be computed via the lq() function:

blaze::DynamicMatrix<double,blaze::rowMajor> A;
// ... Resizing and initialization

blaze::DynamicMatrix<double,blaze::rowMajor> L;
blaze::DynamicMatrix<double,blaze::columnMajor> Q;

lq( A, L, Q );  // LQ decomposition of a row-major matrix

assert( A == L * Q );

The function works for both rowMajor and columnMajor matrices and the three matrices A, L and Q can have any storage order.

Furthermore, lq() can be used with adaptors. For instance, the following example demonstrates the LQ decomposition of an Hermitian matrix into a lower triangular matrix and a general matrix:

blaze::HermitianMatrix< blaze::DynamicMatrix<complex<double>,blaze::columnMajor> > A;
// ... Resizing and initialization

blaze::LowerMatrix< blaze::DynamicMatrix<complex<double>,blaze::columnMajor> > L;
blaze::DynamicMatrix<complex<double>,blaze::rowMajor> Q;

lq( A, L, Q );  // LQ decomposition of A

Linear Systems

The solve() function computes a solution for the given dense linear system of equations (LSE) A*x=b, where A is the given system matrix, x is the solution vector, and b is the given dense right-hand side vector:

blaze::DynamicMatrix<double> A;  // The square general system matrix
blaze::DynamicVector<double> b;  // The right-hand side vector
// ... Resizing and initialization

blaze::DynamicVector<double> x;  // The solution vector

solve( A, x, b );   // Computing the solution x
x = solve( A, b );  // Alternative syntax

Alternatively, solve() computes a solution for the given dense LSE A*X=B, where A is the given dense system matrix, the columns of X are the solution vectors, and the columns of B are the given right-hand side vectors:

blaze::DynamicMatrix<double> A;  // The square general system matrix
blaze::DynamicMatrix<double> B;  // The right-hand side matrix
// ... Resizing and initialization

blaze::DynamicMatrix<double> X;  // The solution matrix

solve( A, X, B );   // Computing the solutions X
X = solve( A, B );  // Alternative syntax

Both solve() functions will automatically select the most suited direct solver algorithm depending on the size and type of the given system matrix. For small matrices of up to 6x6, both functions use manually optimized kernels for maximum performance. For matrices larger than 6x6 the computation is performed by means of the most suited LAPACK solver method (see Linear System Solver).

In case the type of the matrix does not provide additional compile time information about its structure (symmetric, lower, upper, diagonal, ...), the information can be provided manually by means of Declaration Operations when calling the solve() functions:

blaze::DynamicMatrix<double> A;  // The square lower system matrix
blaze::DynamicVector<double> b;  // The right-hand side vector
// ... Resizing and initialization

blaze::DynamicVector<double> x;  // The solution vector

solve( declsym( A ), x, b );     // Solving the LSE with a symmetric system matrix
solve( declherm( A ), x, b );    // Solving the LSE with an Hermitian system matrix
solve( decllow( A ), x, b );     // Solving the LSE with a lower system matrix
solve( declunilow( A ), x, b );  // Solving the LSE with an unilower system matrix
solve( declupp( A ), x, b );     // Solving the LSE with an upper system matrix
solve( decluniupp( A ), x, b );  // Solving the LSE with an uniupper system matrix
solve( decldiag( A ), x, b );    // Solving the LSE with a diagonal system matrix

For both solve() functions the computation fails if ...

  • ... the given matrix is not a square matrix;
  • ... the size of the right-hand side vector doesn't match the dimensions of the system matrix;
  • ... the number of rows of the right-hand side matrix doesn't match the dimensions of the system matrix;
  • ... the given matrix is singular and not invertible.

In all failure cases either a compilation error is created if the failure can be predicted at compile time or a std::invalid_argument exception is thrown.

Note that the solve() functions can only be used for dense matrices with float, double, complex<float> or complex<double> element type. The attempt to call the function with matrices of any other element type or with a sparse matrix results in a compile time error!

Also note that the functions may make use of LAPACK kernels. Thus the functions can only be used if a fitting LAPACK library is available and linked to the executable. Otherwise a linker error will be created.

Furthermore, it is important to note that it is not possible to use any kind of view on the expression object returned by the two-argument solve() functions. Also, it is not possible to access individual elements via the subscript operator or function call operator on the expression object:

row( solve( A, b ), 2UL );  // Compilation error: Views cannot be used on an solve() expression!
solve( A, b )[2];           // Compilation error: It is not possible to access individual elements!

rows( solve( A, B ), { 2UL, 4UL } );  // Compilation error: Views cannot be used on an solve() expression!
solve( A, B )(1,2);                   // Compilation error: It is not possible to access individual elements!

Finally, please note that the solve() functions do not provide any exception safety guarantee, i.e. in case an exception is thrown the solution vector or matrix may already have been modified.


Eigenvalues/Eigenvectors

The eigenvalues and eigenvectors of a dense matrix can be computed via the eigen() functions. The following examples give an impression of the computation of eigenvalues and eigenvectors for a general, a symmetric, and an Hermitian matrix:

using blaze::DynamicMatrix;
using blaze::DynamicVector;
using blaze::rowMajor;
using blaze::columnVector;

DynamicMatrix<double,rowMajor> A( 5UL, 5UL );  // The general matrix A
// ... Initialization

DynamicVector<complex<double>,columnVector> w( 5UL );   // The vector for the complex eigenvalues
DynamicMatrix<complex<double>,rowMajor> V( 5UL, 5UL );  // The matrix for the left eigenvectors

w = eigen( A );    // Computing only the eigenvalues of A (one argument)
eigen( A, w );     // Computing only the eigenvalues of A (two arguments)
eigen( A, w, V );  // Computing both the eigenvalues and eigenvectors of A (three arguments)
using blaze::SymmetricMatrix;
using blaze::DynamicMatrix;
using blaze::DynamicVector;
using blaze::rowMajor;
using blaze::columnVector;

SymmetricMatrix< DynamicMatrix<double,rowMajor> > A( 5UL );  // The symmetric matrix A
// ... Initialization

DynamicVector<double,columnVector> w( 5UL );       // The vector for the real eigenvalues
DynamicMatrix<double,rowMajor>     V( 5UL, 5UL );  // The matrix for the left eigenvectors

w = eigen( A );    // Computing only the eigenvalues of A (one argument)
eigen( A, w );     // Computing only the eigenvalues of A (two arguments)
eigen( A, w, V );  // Computing both the eigenvalues and eigenvectors of A (three arguments)
using blaze::HermitianMatrix;
using blaze::DynamicMatrix;
using blaze::DynamicVector;
using blaze::rowMajor;
using blaze::columnVector;

HermitianMatrix< DynamicMatrix<complex<double>,rowMajor> > A( 5UL );  // The Hermitian matrix A
// ... Initialization

DynamicVector<double,columnVector>      w( 5UL );       // The vector for the real eigenvalues
DynamicMatrix<complex<double>,rowMajor> V( 5UL, 5UL );  // The matrix for the left eigenvectors

w = eigen( A );    // Computing only the eigenvalues of A (one argument)
eigen( A, w );     // Computing only the eigenvalues of A (two arguments)
eigen( A, w, V );  // Computing both the eigenvalues and eigenvectors of A (three arguments)

The one- and two-argument functions compute only the eigenvalues of the given n-by-n matrix, the three-argument function additionally computes the eigenvectors. The eigenvalues are returned in the given vector w and the eigenvectors are returned in the given matrix V, which are both resized to the correct dimensions (if possible and necessary).

Depending on the given matrix type, the resulting eigenvalues are either of floating point or complex type: In case the given matrix is either a compile time symmetric matrix with floating point elements or an Hermitian matrix with complex elements, the resulting eigenvalues will be of floating point type and therefore the elements of the given eigenvalue vector are expected to be of floating point type. In all other cases they are expected to be of complex type. Please note that for complex eigenvalues no order of eigenvalues can be assumed, except that complex conjugate pairs of eigenvalues appear consecutively with the eigenvalue having the positive imaginary part first.

In case A is a row-major matrix, V will contain the left eigenvectors, otherwise V will contain the right eigenvectors. In case V is a row-major matrix the eigenvectors are returned in the rows of V, in case V is a column-major matrix the eigenvectors are returned in the columns of V. In case the given matrix is a compile time symmetric matrix with floating point elements, the resulting eigenvectors will be of floating point type and therefore the elements of the given eigenvector matrix are expected to be of floating point type. In all other cases they are expected to be of complex type.

The following examples give an impression of the computation of eigenvalues and eigenvectors for a general, a symmetric, and an Hermitian matrix:

The functions fail if ...

  • ... the given matrix A is not a square matrix;
  • ... the given vector w is a fixed size vector and the size doesn't match;
  • ... the given matrix V is a fixed size matrix and the dimensions don't match;
  • ... the eigenvalue computation fails.

In all failure cases an exception is thrown.

Note that all eigen() functions can only be used for dense matrices with float, double, complex<float> or complex<double> element type. The attempt to call the function with matrices of any other element type or with a sparse matrix results in a compile time error!

Also note that the functions compute the eigenvalues and/or eigenvectors of a dense matrix by means of LAPACK kernels. Thus the functions can only be used if a fitting LAPACK library is available and linked to the executable. Otherwise a linker error will be created.


Singular Values/Singular Vectors

The singular value decomposition (SVD) of a dense matrix can be computed via the svd() functions. The following two examples give an impression of the computation of singular values and singular vectors for a general dense matrix with double and complex<double> element type, respectively:

using blaze::DynamicMatrix;
using blaze::DynamicVector;
using blaze::rowMajor;
using blaze::columnVector;

DynamicMatrix<double,rowMajor>  A( 5UL, 8UL );  // The general matrix A
// ... Initialization

DynamicMatrix<double,rowMajor>     U;  // The matrix for the left singular vectors
DynamicVector<double,columnVector> s;  // The vector for the singular values
DynamicMatrix<double,rowMajor>     V;  // The matrix for the right singular vectors

s = svd( A );       // (1) Computing only the singular values of A
svd( A, s );        // (2) Computing only the singular values of A
svd( A, U, s, V );  // (3) Computing the singular values and vectors of A

svd( A, s, 0.0, 1.0 );    // (4) Computing all singular values in the floating point range [0.0..1.0)
svd( A, U, s, V, 0, 2 );  // (5) Computing the singular values and vectors in the index range [0..2]
using blaze::DynamicMatrix;
using blaze::DynamicVector;
using blaze::rowMajor;
using blaze::columnVector;

DynamicMatrix<complex<double>,rowMajor>  A( 5UL, 8UL );  // The general matrix A
// ... Initialization

DynamicMatrix<complex<double>,rowMajor> U;  // The matrix for the left singular vectors
DynamicVector<double,columnVector>      s;  // The vector for the singular values
DynamicMatrix<complex<double>,rowMajor> V;  // The matrix for the right singular vectors

s = svd( A );       // (1) Computing only the singular values of A
svd( A, s );        // (2) Computing only the singular values of A
svd( A, U, s, V );  // (3) Computing the singular values and vectors of A

svd( A, s, 0.0, 1.0 );    // (4) Computing all singular values in the floating point range [0.0..1.0)
svd( A, U, s, V, 0, 2 );  // (5) Computing the singular values and vectors in the index range [0..2]

Functions (1), (2) and (4) compute only singular values of the given general m-by-n matrix, functions (3) and (5) additionally compute singular vectors. The resulting singular values are returned in the given vector s, the left singular vectors are returned in the given matrix U, and the right singular vectors are returned in the matrix V. s, U, and V are resized to the correct dimensions (if possible and necessary).

Functions (4) and (5) allow for the specification of a subset of singular values and/or vectors. The number of singular values and vectors to be computed is specified by the lower bound low and the upper bound upp, which either form an integral or a floating point range.

In case low and upp form are of integral type, the function computes all singular values in the index range [low..upp]. The num resulting real and non-negative singular values are stored in descending order in the given vector s, which is either resized (if possible) or expected to be a num-dimensional vector. The resulting left singular vectors are stored in the given matrix U, which is either resized (if possible) or expected to be a m-by-num matrix. The resulting right singular vectors are stored in the given matrix V, which is either resized (if possible) or expected to be a num-by-n matrix.

In case low and upp are of floating point type, the function computes all singular values in the half-open interval (low..upp]. The resulting real and non-negative singular values are stored in descending order in the given vector s, which is either resized (if possible) or expected to be a min(m,n)-dimensional vector. The resulting left singular vectors are stored in the given matrix U, which is either resized (if possible) or expected to be a m-by-min(m,n) matrix. The resulting right singular vectors are stored in the given matrix V, which is either resized (if possible) or expected to be a min(m,n)-by-n matrix.

The functions fail if ...

  • ... the given matrix U is a fixed size matrix and the dimensions don't match;
  • ... the given vector s is a fixed size vector and the size doesn't match;
  • ... the given matrix V is a fixed size matrix and the dimensions don't match;
  • ... the given scalar values don't form a proper range;
  • ... the singular value decomposition fails.

In all failure cases an exception is thrown.

Note that all svd() functions can only be used for dense matrices with float, double, complex<float> or complex<double> element type. The attempt to call the function with matrices of any other element type or with a sparse matrix results in a compile time error!

Also note that the functions compute the singular values and/or singular vectors of a dense matrix by means of LAPACK kernels. Thus the functions can only be used if a fitting LAPACK library is available and linked to the executable. Otherwise a linker error will be created.


Previous: Matrix Types ---- Next: Adaptors

Updated