1

I am trying to study boost. I want just to see example how to solve Ax=B linear equation for sparse matrix. I saw example using umfpack but in boost 1_58 version there is no umfpack and I modified that example a little bit and just try to build application. I also installed boost before started the example. Here is code:

#include <iostream>
#include <boost/numeric/ublas/matrix_sparse.hpp>
#include <boost/numeric/ublas/vector_sparse.hpp>
//#include <boost/numeric/ublas/traits/ublas_sparse.hpp>
//#include <boost/numeric/bindings/umfpack/umfpack.hpp>
#include <boost/numeric/ublas/io.hpp>

namespace ublas = boost::numeric::ublas;
//namespace umf = boost::numeric::bindings::umfpack;

int main() {

    ublas::compressed_matrix<double, ublas::column_major, 0,
        ublas::unbounded_array<int>, ublas::unbounded_array<double> > A(5, 5, 12);
    ublas::vector<double> B(5), X(5);

    A(0, 0) = 2.; A(0, 1) = 3;
    A(1, 0) = 3.; A(1, 2) = 4.; A(1, 4) = 6;
    A(2, 1) = -1.; A(2, 2) = -3.; A(2, 3) = 2.;
    A(3, 2) = 1.;
    A(4, 1) = 4.; A(4, 2) = 2.; A(4, 4) = 1.;

    B(0) = 8.; B(1) = 45.; B(2) = -3.; B(3) = 3.; B(4) = 19.;

    /*
    umf::symbolic_type<double> Symbolic;
    umf::numeric_type<double> Numeric;

    umf::symbolic(A, Symbolic);
    umf::numeric(A, Symbolic, Numeric);
    umf::solve(A, X, B, Numeric);
    */

    std::cout << X << std::endl;  // output: [5](1,2,3,4,5)
}

Can anyone help me and show how to solve simple linear equation for sparse matrix using new boost ?

G.Ch.
  • 35
  • 6

1 Answers1

1

You can get the solution with just boost but it won't be very efficient. The UMFPACK extension is well known and reputed. Use it (you stil use boost, so your requirement is matched).

In the interest of here's the Boost-only solution:¹

Live On Coliru

#include <iostream>
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix_sparse.hpp>
#include <boost/numeric/ublas/vector_sparse.hpp>
#include <boost/numeric/ublas/lu.hpp>
#include <boost/numeric/ublas/io.hpp>

namespace ublas = boost::numeric::ublas;

int main()
{
    ublas::compressed_matrix<double, ublas::column_major, 0> Matrix A(5, 5, 12);

    A(0, 0) =  2.; A(0, 1) =  3.;
    A(1, 0) =  3.; A(1, 2) =  4.; A(1, 4) =  6.;
    A(2, 1) = -1.; A(2, 2) = -3.; A(2, 3) =  2.;
    A(3, 2) =  1.;
    A(4, 1) =  4.; A(4, 2) =  2.; A(4, 4) =  1.;

    ublas::vector<double> y(5);
    y(0) =  8.;
    y(1) = 45.;
    y(2) = -3.;
    y(3) =  3.;
    y(4) = 19.;

    ublas::permutation_matrix<size_t> pm(A.size1());
    lu_factorize(A, pm);
    lu_substitute(A, pm, y);

    std::cout << y << std::endl; // output: [5](1,2,3,4,5)
}

Prints:

[5](1,2,3,4,5)

¹ credits: Boost's Linear Algebra Solution for y=Ax

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
  • This is the most efficient solution for boost ? Or is there another one ? – G.Ch. May 29 '15 at 04:11
  • can you tell me what is the value of epsilon ? I checked it in documentation and I didn't find any real value – G.Ch. Jun 11 '15 at 12:34
  • Which epsilon? I don't know uBlas, but you can get the epsilon for the actual numbers using `std::numeric_limits` see http://en.cppreference.com/w/cpp/types/numeric_limits – sehe Jun 11 '15 at 12:36
  • Tolerance value , the desired approximation – G.Ch. Jun 11 '15 at 16:59