Banded Matrix View, Upper and Lower Triangular Views

For any matrix A the upper and the strict upper triangular part can be accessed with the function upper and strict_upper:

#include <iostream>
#include <boost/numeric/mtl/mtl.hpp>

int main(int argc, char* argv[])
{
    using namespace mtl;

    double           array[][3]= {{1., 2., 3.}, {4., 5., 6.}, {7., 8., 9.}};
    dense2D<double>  A(array);

    std::cout << "\nupper(A) = \n" << upper(A);

    std::cout << "\nstrict_upper(A) = \n" << strict_upper(A);
        
    return 0;
}

The functions return views on the arguments. The resulting view can be used in expressions but this is not recommended in high-performance applications because the lower triangle is still traversed while returning zero values. For the future it is planned to implement traversal of such views more efficiently.

Likewise lower and strict lower triangle matrices are yielded:

#include <iostream>
#include <boost/numeric/mtl/mtl.hpp>

int main(int argc, char* argv[])
{
    using namespace mtl;

    double           array[][3]= {{1., 2., 3.}, {4., 5., 6.}, {7., 8., 9.}};
    dense2D<double>  A(array), L, SL;

    L= lower(A);
    std::cout << "\nlower(A) = \n" << L;

    SL= strict_lower(A);
    std::cout << "\nstrict_lower(A) = \n" << SL;
        
    return 0;
}

In case of sparse matrices the assignment of a lower triangle matrix leads to an efficient representation because the entries in the upper part are not explicitly stored as zeros but omitted entirely.

The most general form of views in this section is returned by the function bands (in fact the others are implemented by it). It returns bands in terms of half-open intervals of diagonals. For instance, the two off-diagonal right from the main diagonal are computed by bands(A, 1, 3):

#include <iostream>
#include <boost/numeric/mtl/mtl.hpp>

int main(int argc, char* argv[])
{
    using namespace mtl;

    double                array[][5]= {{1., 2., 3., 4., 5.}, {4., 5., 6., 7., 8.}, 
                                       {7., 8., 9., 8., 7.}, {6., 5., 4., 3., 2.}};
    dense2D<double>       A(array), B;
    compressed2D<double>  T;

    B= bands(A, 1, 3);
    std::cout << "\nbands(A, 1, 3) = \n" << B;

    T= bands(A, -1, 2);
    std::cout << "\ntri_diagonal(A):= bands(A, -1, 2) = \n" << T;
        
    return 0;
}

A tri-diagonal matrix is returned for the band interval [-1, 2) as in the example above. For performance reasons it is advisable to store the tri-diagonal matrix in a compressed format instead of using it directly.

Return to Permutations and Reordering                                Table of Content                                Proceed to Rank-One and Rank-Two Update






Banded Matrix View, Upper and Lower Triangular Views -- MTL 4 -- Peter Gottschling and Andrew Lumsdaine -- Generated on 19 May 2009 by Doxygen 1.5.5 -- Copyright 2007 by the Trustees of Indiana University.