Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) BoostUblasMatrixExample3

 

Boost

 

boost::ublas::matrix example 3 is a boost::ublas::matrix example.

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppBoostUblasMatrixExample3/CppBoostUblasMatrixExample3.pro

 

include(../../ConsoleApplication.pri) #Or use the code below
# QT += core
# QT += gui
# greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
# CONFIG   += console
# CONFIG   -= app_bundle
# TEMPLATE = app
# CONFIG(release, debug|release) {
#   DEFINES += NDEBUG NTRACE_BILDERBIKKEL
# }
# QMAKE_CXXFLAGS += -std=c++1y -Wall -Wextra -Weffc++
# unix {
#   QMAKE_CXXFLAGS += -Werror
# }

include(../../Libraries/Boost.pri) #Or use the code below
# win32 {
#   INCLUDEPATH += \
#     ../../Libraries/boost_1_54_0
# }

SOURCES += main.cpp

 

 

 

 

 

./CppBoostUblasMatrixExample3/main.cpp

 

#include <boost/numeric/ublas/matrix.hpp>

const boost::numeric::ublas::matrix<double> Inverse(
  const boost::numeric::ublas::matrix<double>& m)
{
  assert(m.size1() == m.size2() && "Can only calculate the inverse of square matrices");
  assert(m.size1() == 2 && m.size2() == 2 && "Only for 2x2 matrices");
  boost::numeric::ublas::matrix<double> n(2,2);
  const double a = m(0,0);
  const double b = m(0,1);
  const double c = m(1,0);
  const double d = m(1,1);
  assert((a * d) - (b * c) != 0.0 && "Determinant must be nonzero");
  const double determinant = 1.0 / ((a * d) - (b * c));
  n(0,0) =  d * determinant;
  n(0,1) = -b * determinant;
  n(1,0) = -c * determinant;
  n(1,1) =  a * determinant;
  return n;
}

const boost::numeric::ublas::matrix<double> CreateMatrix()
{
  boost::numeric::ublas::matrix<double> m(2,2); //y-x-ordered
  m(0,0) = 4.0;
  m(0,1) = 7.0;
  m(1,0) = 2.0;
  m(1,1) = 6.0;
  return m;
}

bool IsAboutEqual(const double x, const double y) { return std::abs(x-y) < 0.00001; }

int main()
{
  //Create an example matrix
  const boost::numeric::ublas::matrix<double> m = CreateMatrix();

  assert(IsAboutEqual(m(0,0),4.0)); assert(IsAboutEqual(m(0,1),7.0));
  assert(IsAboutEqual(m(1,0),2.0)); assert(IsAboutEqual(m(1,1),6.0));

  //Obtain the inverse of the example matrix
  const boost::numeric::ublas::matrix<double> n = Inverse(m);

  assert(IsAboutEqual(n(0,0), 0.6)); assert(IsAboutEqual(n(0,1),-0.7));
  assert(IsAboutEqual(n(1,0),-0.2)); assert(IsAboutEqual(n(1,1), 0.4));

  //Check that the product of a matrix and its inverse yields an identity matrix
  const boost::numeric::ublas::matrix<double> p = boost::numeric::ublas::prod(m,n);

  assert(IsAboutEqual(p(0,0),1.0)); assert(IsAboutEqual(p(0,1),0.0));
  assert(IsAboutEqual(p(1,0),0.0)); assert(IsAboutEqual(p(1,1),1.0));
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml