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

We have started using the boost unit testing library for a large existing code
After installing Boost library using Synaptic Package Manager, the version installed is 1.42 .
i have some problem with my boost library. i m using freebsd and installed
I am using the boost regex library in my code. I have a simple
I am writing a Graph-class using boost-graph-library. I use custom vertex and edge properties
What is the purpose of using the Boost.MPI library? Does it increase performance? And
What are the differences in using boost:thread, Posix Thread library and the new C++11
I'm using boost::asio, and I have code like this: void CServer::Start(int port) { tcp::acceptor
I'm new in using boost and have a problem. I need shared_mutex function in
I am using boost shared pointers in my program, and I have a class

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.