我是一名 python 程序员,我正在尝试了解 boost。
这是我得到的:
>>> import numpy as np
>>> a
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
>>> b
array([[20, 21, 22],
[23, 24, 25]])
>>> a[[0,2]] = b
>>> a
array([[20, 21, 22], # first row of b
[ 3, 4, 5],
[23, 24, 25], # second row of b
[ 9, 10, 11]])
我可以使用 boost 来做到这一点:
#include <iostream>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
using namespace std;
int main()
{
using namespace boost::numeric::ublas;
matrix<double> a (4, 3);
for (unsigned i = 0; i < a.size1 (); ++ i)
for (unsigned j = 0; j < a.size2 (); ++ j)
a (i, j) = a.size2() * i + j;
matrix<double> b (2,3);
for (unsigned i = 0; i < b.size1 (); ++ i)
for (unsigned j = 0; j < b.size2 (); ++ j)
b (i, j) = b.size2() * i + j + 20;
cout << "matrix a : " << a << endl;
cout << "matrix b : " << b << endl;
// replace row 0 in a with row 0 in b
// replace row 2 in a with row 1 in b
unsigned rows[] = {0,2};
int length = sizeof(rows) / sizeof(rows[0]);
for(int i = 0; i < length; i++)
for(int j = 0; j < a.size2(); j++)
a(rows[i], j) = b(i, j);
cout << "matrix a : " << a << endl;
return 0;
}
但是,我不确定这是否是最好的方法。看着 documentation我没有看到内置的索引方法,所以看起来循环是唯一的选择,我错过了什么吗?
但是,在 C++ 中循环可能并没有那么糟糕。 python中的python循环很慢,我上面的例子循环发生在C层。但是在 C++ 中循环并不慢,因此即使我们必须手动循环,代码仍然有效,对吗?
请让我知道我的理解是否正确和/或我是否缺少实现上述内容的更好方法。
最佳答案
Boost.MultiArray更适合这种索引。您的 Python 示例可以重现如下:
// helper type for a 2d matrix of ints
using array_type = boost::multi_array<int, 2>;
array_type a(boost::extents[4][3]); // 4x3 matrix
array_type b(boost::extents[2][3]); // 2x3 matrix
现在填充这些矩阵
std::iota(a.data(), a.data() + a.num_elements(), 0);
std::iota(b.data(), b.data() + b.num_elements(), 20);
然后定义一个 lambda 函数来打印一个二维矩阵,这样我们就可以看到内容
auto array_printer = [](array_type const& arr) {
for(auto const &row : arr) {
for(auto const& elem : row) {
std::cout << std::setw(2) << elem << ' ';
}
std::cout << '\n';
}
std::cout << '\n';
};
让我们打印到目前为止的内容
std::cout << "Before:\na =\n";
array_printer(a);
std::cout << "b =\n";
array_printer(b);
输出:
Before:
a =
0 1 2
3 4 5
6 7 8
9 10 11
b =
20 21 22
23 24 25
是时候创建数组的二维 View 了
using range_type = boost::multi_array_types::index_range;
using view_type = array_type::array_view<2>::type;
// Create a 2d view of a
// - the rows of the view consist of rows [0, 3) with a stride of 2
// - the columns of the view consist of all columns of a
view_type a_view = a[boost::indices[range_type(0,3,2)][range_type()]];
现在将b
赋给我们创建的 View ,并打印结果
a_view = b;
std::cout << "After:\na =\n";
array_printer(a);
输出:
After:
a =
20 21 22
3 4 5
23 24 25
9 10 11
关于c++ - Boost 中的索引矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24522743/