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

  • Home
  • SEARCH
  • 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 3619618
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:54:02+00:00 2026-05-18T22:54:02+00:00

I am using the Boost library, version 1.44.0 and c++ with gcc 4.0. I

  • 0

I am using the Boost library, version 1.44.0 and c++ with gcc 4.0.

I have created a ublas::compressed_matrix<double> and I have filled it using the method insert_element(index1, index2, value).
Thereafter I am iterating through the elements of the matrix and printing out the non-zero elements. I use the following code for iterating (note that i1_t and i2_t are typedef boost::numeric::ublas::compressed_matrix<double>::iterator):

   for (i1_t i1 = matrix->begin1(); i1 != matrix->end1(); i1++) {
    for (i2_t i2 = matrix->begin2(); i2 != matrix->end2(); i2++) {
     cout<<"("<<i2.index1()<<","<<i2.index2()<<")="<<*i2<<" ";
    }
    cout<<endl;
   }

Or alternatively the following:

    for (i1_t i1 = matrix->begin1(); i1 != matrix->end1(); i1++) {
    cout<<i1.index1()<<" -> ";
    for (i2_t i2 = matrix->begin2(); i2 != matrix->end2(); i2++) {
     cout<<i2.index2()<<" ("<<*i2<<") ";
    }
    cout<<endl;
   }

Of course, both of them are doing pretty much the same thing. The code follows the suggestions provided by another post in stackoverflow: iterating on non-zero elements

However, when running the code, I get that only the first few elements are correctly printed (in particular, this applies to the first six entries), until I get the following error:

Check failed in file /opt/local/include/boost/numeric/ublas/matrix_sparse.hpp at line 3109:
p
terminate called after throwing an instance of 'boost::numeric::ublas::bad_index'
  what():  bad index

All the elements, however, seem to be correctly inserted into the matrix.
I am wondering what is going on, why I get this error and what does it mean?

Many thanks.

  • 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-18T22:54:03+00:00Added an answer on May 18, 2026 at 10:54 pm

    I have found what the problem was, and of course it was my mistake on the code.

    In fact the inner loop (i.e. the second for loop) iterates through the wrong thing. Originally I wrote:

    for (i2_t i2 = matrix->begin2(); i2 != matrix->end2(); i2++)
    

    However, i2 should not iterate from matrx->begin2() to matrix->end2(). Instead, the limits of the iteration for i2 should be i1.begin() and i1.end(), where i1 is the first iterator! Apologies for my mistake.

    So, to summarise, the correct loop should be:

    for (i1_t i1 = matrix->begin1(); i1 != matrix->end1(); i1++) {
       for (i2_t i2 = i1.begin(); i2 != i1.end(); i2++) {
          cout << "(" << i2.index1() << "," << i2.index2() << ")=" << *i2 << " ";
       }
       cout << endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Since I have started using this site, I keep hearing about the Boost library.
After installing Boost library using Synaptic Package Manager, the version installed is 1.42 .
I have just started using Boost 1.36. These libraries would be very useful in
I have a section of code that uses a Boost library that results in
I'm using the Boost Graph Library to process an undirected graph, and declared my
im just installing the boost library using an installer. Its asking me which variants
I have ubuntu 10.04 and want to install the latest boost library 1.44_0 I
I'm using the boost::thread library (V1.44) to support threads in my C++ project. The
I'm using boost for several C++ projects. I recently made a upgrade (1.33.1 to
I'm using boost's shared pointers, and enable_shared_from_this to enable returning a shared pointer to

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.