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 1093835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:53:04+00:00 2026-05-16T23:53:04+00:00

I am confused on how the boost::compressed_matrix works. Suppose I declare the compressed_matrix like

  • 0

I am confused on how the boost::compressed_matrix works. Suppose I declare the compressed_matrix like this:

boost::numeric::ublas::compressed_matrix<double> T(1000, 1000, 3*1000);

This allocates space for 3*1000 elements in a 1000×1000 matrix. Now how do I give it the locations which are the non-zero elements? When and how are the non-zero elements set? Is it each time I assign an element in the matrix, e.g. B(4,4)=4, it would mark that element as non-zero?

I would really appreciate if you could help me learn this using an example if possible. Some insight into the internal implementation would be great. I want to make sure I don’t write programs that are sub-optimal by guess work.

thank you!

  • 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-16T23:53:04+00:00Added an answer on May 16, 2026 at 11:53 pm

    compressed matrix has an underlying linear container (unbounded_array by default, but you can make it bounded_array or std::vector if you want), which contains all non-zero elements of the matrix, in row-major (by default) order. That means that whenever you write a new non-zero element to compressed matrix, it is inserted into that underlying array. If you’re not filling the matrix in (row-major) order, every insert will be O(n). When you’re changing an existing non-zero element, it is simply changed in the underlying array.

    Here’s a simple test to see what the underlying structure looks like:

    #include <boost/numeric/ublas/matrix_sparse.hpp>
    #include <boost/numeric/ublas/storage.hpp>
    namespace ublas = boost::numeric::ublas;
    void show_array(const ublas::unbounded_array<double>& a)
    {
        for(size_t i=0; i<a.size(); ++i)
                std::cout << a[i] << ' ';
        std::cout << '\n';
    }
    int main()
    {
        ublas::compressed_matrix<double> m (10, 10, 3 * 10);
        m(0, 5) = 1; // underlying array is {1, 0, 0, 0, ...}
        show_array(m.value_data());
        m(0, 6) = 2; // underlying array is {1, 2, 0, 0, ...}
        show_array(m.value_data());
        m(0, 4) = 3;  // underlying array is {3, 1, 2, 0, ...}
        show_array(m.value_data());
        m(0, 4) = 7;  // underlying array is {7, 1, 2, 0, ...}
        show_array(m.value_data());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My confuse is like this code: #include stdafx.h #include <boost/bind.hpp> using namespace std; void
INFO I'd like to use boost::function to pass callback as parameter, like this way:
I'm a bit confused as to the status of Boost.Log. Is this an official
I'm a bit confused on setting up the boost test library. Here is my
Pretty confused right now, while i feel i know a bit about vhosts this
I'm learning the use of boost smart pointers but I'm a bit confused about
confused how to achieve this maybe with FULL OUTER or a UNION. I want
Confused by java compilation process OK i know this: We write java source code,
I am confused as to what boost::bind does when we bind to member variables.
I am very confused about the difference between a lock and mutex. In Boost

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.