boost压缩矩阵基础知识
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
压缩矩阵有一个底层线性容器(默认情况下为
unbounded_array
,但如果需要,您可以将其设为bounded_array
或std::vector
),其中包含矩阵的所有非零元素,按行优先(默认)顺序。这意味着每当您向压缩矩阵写入新的非零元素时,它都会插入到该基础数组中。如果您不按(行优先)顺序填充矩阵,则每次插入都将是 O(n)。当您更改现有的非零元素时,它只是在底层数组中进行更改。这是一个简单的测试,看看底层结构是什么样的:
compressed matrix has an underlying linear container (
unbounded_array
by default, but you can make itbounded_array
orstd::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:
您可以使用
(i,j)
运算符隐式创建非零元素,也可以使用 insert_element 函数显式插入元素。最好的地方实际上是查看内部实现:
http://www.tena-sda.org/doc/5.2.2/boost/d2/db7/matrix__sparse_8hpp-source.html#l02761
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