Boost uBlas Matrix

Matrix overview
The templated class matrix is the base container adaptor for dense matrice
Header : <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/matrix.hpp> #include <boost/numeric/ublas/io.hpp> void ublas2matrix() { using namespace boost::numeric::ublas; matrix m (3, 3); for (unsigned i = 0; i < m.size1 (); ++ i) // row for (unsigned j = 0; j < m.size2 (); ++ j) // column m (i, j) = 3 * i + j; // opertor(i,j) std::cout << m << std::endl; m.erase_element(1,1); m.erase_element(1,2); std::cout << m << std::endl; m.clear(); std::cout << m << std::endl;
//identity_matrix
identity_matrix im (3); std::cout << im << std::endl;
//zero_matrix
zero_matrix zm (3); std::cout << zm << std::endl;
//scalar_matrix
int val=20; scalar_matrix sm (3,3,val); // default val=1 std::cout << sm << std::endl; }
Triangular Matrix

Example code link
#include <boost/numeric/ublas/triangular.hpp> void ublas3matrix() { using namespace boost::numeric::ublas; matrix m (3, 3); for (unsigned i = 0; i < m.size1 (); ++ i) // row for (unsigned j = 0; j < m.size2 (); ++ j) // column m (i, j) = 3 * i + j; // opertor(i,j) std::cout << "full matrix = " << m << "\n" ; triangular_matrix < double, lower> L=triangular_adaptor< matrix < double >, lower> (m); triangular_matrix < double, unit_lower> UL=triangular_adaptor< matrix < double >, unit_lower> (m); triangular_matrix < double, strict_lower> SL=triangular_adaptor< matrix < double >, strict_lower> (m); std::cout << "lower L = " << L << "\n" << "strict lower SL = " << SL << "\n" << "unit lower UL = " << UL << "\n" ; triangular_matrix < double, upper> U = triangular_adaptor< matrix < double >, upper> (m); triangular_matrix < double, unit_upper> UU = triangular_adaptor < matrix < double >, unit_upper> (m); triangular_matrix < double, strict_upper> SU=triangular_adaptor < matrix < double >, strict_upper> (m); std::cout << "upper U = " << U << "\n" << "strict upper SU = " << SU << "\n" << "unit upper UU = " << UU << "\n" ; }

Symmetric Matrix
#include <boost/numeric/ublas/symmetric.hpp> void ublas4matrix() { using namespace boost::numeric::ublas; symmetric_matrix ml (3, 3); std::cout << ml << std::endl; for (unsigned i = 0; i < ml.size1 (); ++ i) for (unsigned j = 0; j < ml.size2(); ++ j) ml (i, j) = rand()%20; std::cout << ml << std::endl; symmetric_matrix mu (3, 3); std::cout << mu << std::endl; for (unsigned i = 0; i < mu.size1 (); ++ i) for (unsigned j = 0; j < mu.size2 (); ++ j) mu (i, j) = rand()%30; std::cout << mu << std::endl; std::cout << "=====================================================" << std::endl; matrix m (3, 3); symmetric_adaptor< matrix , lower> sal (m); for (unsigned i = 0; i < sal.size1 (); ++ i) for (unsigned j = 0; j < sal.size2(); ++ j) sal (i, j) = rand()%20; std::cout << sal << std::endl; symmetric_adaptor< matrix , upper> sau (m); for (unsigned i = 0; i < sau.size1 (); ++ i) for (unsigned j = 0; j < sau.size2 (); ++ j) sau (i, j) = rand()%30; std::cout << sau << std::endl; }

Hermitian matrix

Hermitian matrix (or self-adjoint matrix) is a square matrix with complex entries that is equal to its own conjugate transpose – that is, the element in the i-th row and j-th column is equal to the complex conjugate of the element in the j-th row and i-th column, for all indices i and j:

#include <boost/numeric/ublas/hermitian.hpp> void ublas5matrix () { using namespace boost::numeric::ublas; hermitian_matrix< std::complex < double >, lower > ml (3, 3); for (unsigned i = 0; i < ml.size1 (); ++ i) { for (unsigned j = 0; j < i; ++ j) ml (i, j) = std::complex < double> (rand()%3, 3 * i + j); ml (i, i) = std::complex < double> (rand()%4, 0); } std::cout << ml << std::endl; hermitian_matrix< std::complex < double>, upper> mu (3, 3); for (unsigned i = 0; i < mu.size1 (); ++ i) { mu (i, i) = std::complex (4 * i, 0); for (unsigned j = i + 1; j < mu.size2 (); ++ j) mu (i, j) = std::complex < double> (rand()%5, rand()%j); } std::cout << mu << std::endl; std::cout << "=====================================================" << std::endl; matrix< std::complex < double> > m (3, 3); hermitian_adaptor< matrix < std::complex < double> >, lower> hal (m); for (unsigned i = 0; i < hal.size1 (); ++ i) { for (unsigned j = 0; j < i; ++ j) hal (i, j) = std::complex< double> (3 * i + j, 3 * i + j); hal (i, i) = std::complex< double> (4 * i, 0); } std::cout << hal << std::endl; hermitian_adaptor< matrix < std::complex < double> >, upper> hau (m); for (unsigned i = 0; i < hau.size1 (); ++ i) { hau (i, i) = std::complex (4 * i, 0); for (unsigned j = i + 1; j < hau.size2 (); ++ j) hau (i, j) = std::complex (3 * i + j, 3 * i + j); } std::cout << hau << std::endl; }


No comments:

Post a Comment

would you like it. :)