Block Vectors and Matrices

Table of Contents


General Concepts


In addition to fundamental element types, the Blaze library supports vectors and matrices with non-fundamental element type. For instance, it is possible to define block matrices by using a matrix type as the element type:

DynamicMatrix< DynamicMatrix<double,rowMajor>, rowMajor > A;
DynamicVector< DynamicVector<double,columnVector >, columnVector > x, y;
// ... Resizing and initialization
y = A * x;

The matrix/vector multiplication in this example runs fully parallel and uses vectorization for every inner matrix/vector multiplication and vector addition.


Pitfalls


The only thing to keep in mind when using non-fundamental element types is that all operations between the elements have to be well defined. More specifically, the size of vector and matrix elements has to match. The attempt to combine two non-matching elements results in either a compilation error (in case of statically sized elements) or an exception (for dynamically sized elements):

DynamicVector< StaticVector<int,2UL> > a;
DynamicVector< StaticVector<int,3UL> > b;
DynamicVector< DynamicVector<int> > c( a + b ); // Compilation error: element size doesn't match

Therefore please don't forget that dynamically sized elements (e.g. blaze::DynamicVector, blaze::HybridVector, blaze::DynamicMatrix, blaze::HybridMatrix, ...) need to be sized accordingly upfront.


Example


The following example demonstrates a complete multiplication between a statically sized block matrix and block vector:

// ( ( 1 1 ) ( 2 2 ) ) ( ( 1 ) ) ( ( 10 ) )
// ( ( 1 1 ) ( 2 2 ) ) ( ( 1 ) ) ( ( 10 ) )
// ( ) * ( ) = ( )
// ( ( 3 3 ) ( 4 4 ) ) ( ( 2 ) ) ( ( 22 ) )
// ( ( 3 3 ) ( 4 4 ) ) ( ( 2 ) ) ( ( 22 ) )
typedef StaticMatrix<int,2UL,2UL,rowMajor> M2x2;
typedef StaticVector<int,2UL,columnVector> V2;
DynamicMatrix<M2x2,rowMajor> A{ { M2x2(1), M2x2(2) }
{ M2x2(3), M2x2(4) } };
DynamicVector<V2,columnVector> x{ V2(1), V2(2) };
DynamicVector<V2,columnVector> y( A * x );


Previous: LAPACK Functions     Next: Intra-Statement Optimization