Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 332067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:49:58+00:00 2026-05-12T09:49:58+00:00

I am using Numeric Library Bindings for Boost UBlas to solve a simple linear

  • 0

I am using Numeric Library Bindings for Boost UBlas to solve a simple linear system.
The following works fine, except it is limited to handling matrices A(m x m) for relatively
small ‘m’.

In practice I have a much larger matrix with dimension m= 10^6 (up to 10^7).
Is there existing C++ approach for solving Ax=b that uses memory efficiently.

#include<boost/numeric/ublas/matrix.hpp>
#include<boost/numeric/ublas/io.hpp>
#include<boost/numeric/bindings/traits/ublas_matrix.hpp>
#include<boost/numeric/bindings/lapack/gesv.hpp>
#include <boost/numeric/bindings/traits/ublas_vector2.hpp>

// compileable with this command


//g++ -I/home/foolb/.boost/include/boost-1_38 -I/home/foolb/.boostnumbind/include/boost-numeric-bindings solve_Axb_byhand.cc -o solve_Axb_byhand -llapack


namespace ublas = boost::numeric::ublas;
namespace lapack= boost::numeric::bindings::lapack;


int main()
{
    ublas::matrix<float,ublas::column_major> A(3,3);
    ublas::vector<float> b(3);


    for(unsigned i=0;i < A.size1();i++)
        for(unsigned j =0;j < A.size2();j++)
        {
            std::cout << "enter element "<<i << j << std::endl;
            std::cin >> A(i,j);
        }

    std::cout << A << std::endl;

    b(0) = 21; b(1) = 1; b(2) = 17;

    lapack::gesv(A,b);

    std::cout << b << std::endl;


    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:49:59+00:00Added an answer on May 12, 2026 at 9:49 am

    Short answer: Don’t use Boost’s LAPACK bindings, these were designed for dense matrices,
    not sparse matrices, use UMFPACK instead.

    Long answer: UMFPACK is one of the best libraries for solving Ax=b when A is large and sparse.

    • http://www.cise.ufl.edu/research/sparse/umfpack/
    • http://www.cise.ufl.edu/research/sparse/umfpack/UMFPACK/Doc/QuickStart.pdf

    Below is sample code (based on umfpack_simple.c) that generates a simple A and b
    and solves Ax = b.

    #include <stdlib.h>
    #include <stdio.h>
    #include "umfpack.h"
    
    int    *Ap; 
    int    *Ai;
    double *Ax; 
    double *b; 
    double *x; 
    
    /* Generates a sparse matrix problem: 
       A is n x n tridiagonal matrix
       A(i,i-1) = -1;
       A(i,i) = 3; 
       A(i,i+1) = -1; 
    */
    void generate_sparse_matrix_problem(int n){
      int i;  /* row index */ 
      int nz; /* nonzero index */
      int nnz = 2 + 3*(n-2) + 2; /* number of nonzeros*/
      int *Ti; /* row indices */ 
      int *Tj; /* col indices */ 
      double *Tx; /* values */ 
    
      /* Allocate memory for triplet form */
      Ti = malloc(sizeof(int)*nnz);
      Tj = malloc(sizeof(int)*nnz);
      Tx = malloc(sizeof(double)*nnz);
    
      /* Allocate memory for compressed sparse column form */
      Ap = malloc(sizeof(int)*(n+1));
      Ai = malloc(sizeof(int)*nnz);
      Ax = malloc(sizeof(double)*nnz);
    
      /* Allocate memory for rhs and solution vector */
      x = malloc(sizeof(double)*n);
      b = malloc(sizeof(double)*n);
    
      /* Construct the matrix A*/
      nz = 0;
      for (i = 0; i < n; i++){
        if (i > 0){
          Ti[nz] = i;
          Tj[nz] = i-1;
          Tx[nz] = -1;
          nz++;
        }
    
        Ti[nz] = i;
        Tj[nz] = i;
        Tx[nz] = 3;
        nz++;
    
        if (i < n-1){
          Ti[nz] = i;
          Tj[nz] = i+1;
          Tx[nz] = -1;
          nz++;
        }
        b[i] = 0;
      }
      b[0] = 21; b[1] = 1; b[2] = 17;
      /* Convert Triplet to Compressed Sparse Column format */
      (void) umfpack_di_triplet_to_col(n,n,nnz,Ti,Tj,Tx,Ap,Ai,Ax,NULL);
    
      /* free triplet format */ 
      free(Ti); free(Tj); free(Tx);
    }
    
    
    int main (void)
    {
        double *null = (double *) NULL ;
        int i, n;
        void *Symbolic, *Numeric ;
        n = 500000;
        generate_sparse_matrix_problem(n);
        (void) umfpack_di_symbolic (n, n, Ap, Ai, Ax, &Symbolic, null, null);
        (void) umfpack_di_numeric (Ap, Ai, Ax, Symbolic, &Numeric, null, null);
        umfpack_di_free_symbolic (&Symbolic);
        (void) umfpack_di_solve (UMFPACK_A, Ap, Ai, Ax, x, b, Numeric, null, null);
        umfpack_di_free_numeric (&Numeric);
        for (i = 0 ; i < 10 ; i++) printf ("x [%d] = %g\n", i, x [i]);
        free(b); free(x); free(Ax); free(Ai); free(Ap);
        return (0);
    }
    

    The function generate_sparse_matrix_problem creates the matrix A and the
    right-hand side b. The matrix is first constructed in triplet form. The
    vectors Ti, Tj, and Tx fully describe A. Triplet form is easy to create but
    efficient sparse matrix methods require Compressed Sparse Column format. Conversion
    is performed with umfpack_di_triplet_to_col.

    A symbolic factorization is performed with umfpack_di_symbolic. A sparse
    LU decomposition of A is performed with umfpack_di_numeric.
    The lower and upper triangular solves are performed with umfpack_di_solve.

    With n as 500,000, on my machine, the entire program takes about a second to run.
    Valgrind reports that 369,239,649 bytes (just a little over 352 MB) were allocated.

    Note this page discusses Boost’s support for sparse matrices in Triplet (Coordinate)
    and Compressed format. If you like, you can write routines to convert these boost objects
    to the simple arrays UMFPACK requires as input.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 223k
  • Answers 223k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try doing this, import mx.utils.Delegate; class newMovie extends MovieClip {… May 13, 2026 at 12:34 am
  • Editorial Team
    Editorial Team added an answer It is not possible to switch back to the old… May 13, 2026 at 12:34 am
  • Editorial Team
    Editorial Team added an answer I'm not sure what you mean by listing the column… May 13, 2026 at 12:34 am

Related Questions

I have just started using Boost 1.36. These libraries would be very useful in
Walter Rumsby provided a great answer to Where to place JavaScript in an HTML
I am writing a Matlab extension using the C++ ublas library, and I would

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.