boost压缩矩阵基础知识

发布于 2024-09-24 02:14:58 字数 362 浏览 4 评论 0原文

我对 boost::compressed_matrix 的工作原理感到困惑。假设我像这样声明压缩矩阵:

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

这为 1000x1000 矩阵中的 3*1000 个元素分配空间。现在我如何给它非零元素的位置?何时以及如何设置非零元素?是否每次我在矩阵中分配一个元素(例如 B(4,4)=4)时,它都会将该元素标记为非零?

如果可能的话,如果您能通过示例帮助我学习这一点,我将非常感激。对内部实施的一些了解会很棒。我想确保我不会编写通过猜测而次优的程序。

谢谢你!

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 1000x1000 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妥活 2024-10-01 02:14:58

压缩矩阵有一个底层线性容器(默认情况下为unbounded_array,但如果需要,您可以将其设为bounded_arraystd::vector),其中包含矩阵的所有非零元素,按行优先(默认)顺序。这意味着每当您向压缩矩阵写入新的非零元素时,它都会插入到该基础数组中。如果您不按(行优先)顺序填充矩阵,则每次插入都将是 O(n)。当您更改现有的非零元素时,它只是在底层数组中进行更改。

这是一个简单的测试,看看底层结构是什么样的:

#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());
}

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());
}
恍梦境° 2024-10-01 02:14:58

您可以使用 (i,j) 运算符隐式创建非零元素,也可以使用 insert_element 函数显式插入元素。

最好的地方实际上是查看内部实现:

http://www.tena-sda.org/doc/5.2.2/boost/d2/db7/matrix__sparse_8hpp-source.html#l02761

true_reference insert_element (size_type i, size_type j, const_reference t)

将值 t 插入到第 i 行的第 j 个元素处。不允许有重复的元素。

<小时>

void擦除元素(尺寸类型i,尺寸类型j)

删除第 i 行第 j 个元素的值。

you can either use (i,j) operator to create nonzero element implicitly or use insert_element function to insert element explicitly.

Best place is actually look inside implementation:

http://www.tena-sda.org/doc/5.2.2/boost/d2/db7/matrix__sparse_8hpp-source.html#l02761

true_reference insert_element (size_type i, size_type j, const_reference t)

Inserts the value t at the j-th element of the i-th row. Duplicates elements are not allowed.


void erase_element (size_type i, size_type j)

Erases the value at the j-th element of the i-th row.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文