我在blitz ++,armadillo,boost :: MultiArray之间用以下代码进行了比较(借用旧帖子)
#include <iostream>
using namespace std;
#include <windows.h>
#define _SCL_SECURE_NO_WARNINGS
#define BOOST_DISABLE_ASSERTS
#include <boost/multi_array.hpp>
#include <blitz/array.h>
#include <armadillo>
int main(int argc, char* argv[])
{
const int X_SIZE = 1000;
const int Y_SIZE = 1000;
const int ITERATIONS = 100;
unsigned int startTime = 0;
unsigned int endTime = 0;
// Create the boost array
//------------------Measure boost Loop------------------------------------------
{
typedef boost::multi_array<double, 2> ImageArrayType;
ImageArrayType boostMatrix(boost::extents[X_SIZE][Y_SIZE]);
startTime = ::GetTickCount();
for (int i = 0; i < ITERATIONS; …Run Code Online (Sandbox Code Playgroud) c++ boost-multi-array multidimensional-array blitz++ armadillo
我试图将boost :: multi_array的性能与本机动态分配的数组进行比较,使用以下测试程序:
#include <windows.h>
#define _SCL_SECURE_NO_WARNINGS
#define BOOST_DISABLE_ASSERTS
#include <boost/multi_array.hpp>
int main(int argc, char* argv[])
{
const int X_SIZE = 200;
const int Y_SIZE = 200;
const int ITERATIONS = 500;
unsigned int startTime = 0;
unsigned int endTime = 0;
// Create the boost array
typedef boost::multi_array<double, 2> ImageArrayType;
ImageArrayType boostMatrix(boost::extents[X_SIZE][Y_SIZE]);
// Create the native array
double *nativeMatrix = new double [X_SIZE * Y_SIZE];
//------------------Measure boost----------------------------------------------
startTime = ::GetTickCount();
for (int i = 0; i < ITERATIONS; ++i) …Run Code Online (Sandbox Code Playgroud) 看完文档后我无法想出这个.
我可以写代码等
typedef boost::multi_array<boost::int32_t, 3> data_t;
// 3d --
typedef data_t::array_view<3>::type data_3d_view_t;
// 2d --
typedef data_3d_view_t::reference data_2d_subarray_t;
typedef data_t::array_view<2>::type data_2d_view_t;
Run Code Online (Sandbox Code Playgroud)
然后我可以通过类型data_2d_subarray_t或访问使用2d切片data_2d_view_t.
他们之间有什么区别?
对于一个我不能用另一个做的人,我该怎么办?
它有任何性能差异吗?
非常感谢您向我澄清这一点.最好的问候,rodrigob.
我有一个Boost多阵列,其维度根据用户的输入在运行时设置.
我现在想通过x,y,z组件迭代该数组.
如果这是一个std :: vector,我会使用:
for(int i=0;i<v.size();i++){
Run Code Online (Sandbox Code Playgroud)
或许是某种迭代器.
如何获取多阵列尺寸的数值?
如何迭代多重阵列?
谢谢!
我有一个3D multi_array,我想使用运行时指定的维度制作2D切片.我知道退化维度的索引和我想要在该简并维度中提取的切片的索引.目前丑陋的解决方案看起来像这样:
if (0 == degenerate_dimension)
{
Slice slice = input_array[boost::indices[slice_index][range()][range()]];
}
else if (1 == degenerate_dimension)
{
Slice slice = input_array[boost::indices[range()][slice_index][range()]];
}
else if (2 == degenerate_dimension)
{
Slice slice = input_array[boost::indices[range()][range()][slice_index]];
}
Run Code Online (Sandbox Code Playgroud)
有没有更美观的方法来构造index_gen对象?像这样的东西:
var slicer;
for(int i = 0; i < 3; ++i) {
if (degenerate_dimension == i)
slicer = boost::indices[slice_index];
else
slicer = boost::indices[range()];
}
Slice slice = input_array[slicer];
Run Code Online (Sandbox Code Playgroud)
似乎每次对boost :: indices :: operator []的后续调用都会返回一个不同的类型,具体取决于维度(即先前调用的数量),因此无法使用单个变量来保存临时的index_gen对象.
我想分配一个boost :: multi_array的副本.我怎样才能做到这一点.我想要将其分配给的对象已使用默认构造函数进行初始化.
此代码不起作用,因为尺寸和大小不相同
class Field {
boost::multi_array<char, 2> m_f;
void set_f(boost::multi_array<short, 2> &f) {
m_f = f;
}
}
Run Code Online (Sandbox Code Playgroud)
用什么而不是m_f = f?
我编写了一个operator<<处理的专门化boost::multi_array,并使用ConstMultiArrayConcept它以便它可以在外部数组和子数组上工作.我想知道,为什么这些multi_array概念有一个std::size_t NumDims模板参数,因为它可以简单地从中提取出来multi_array.NumDimsin 的唯一用途ConstMultiArrayConcept是作为递归深度arg idgen_helper,用于测试切片.
作为参考,这里是multi_array概念的标题:http:
//www.boost.org/doc/libs/1_51_0/boost/multi_array/concept_checks.hpp
这是我的超载 operator<<
template <typename CharT, typename Traits, typename MultiArrayT>
BOOST_CONCEPT_REQUIRES(
((boost::multi_array_concepts::ConstMultiArrayConcept<MultiArrayT, MultiArrayT::dimensionality>)),
(std::basic_ostream<CharT, Traits>&)) // return type
operator <<( std::basic_ostream<CharT, Traits>& os, MultiArrayT const& ary )
{
typename std::basic_ostream<CharT, Traits>::sentry opfx( os );
if ( opfx ) {
boost::multi_array_types::size_type const* sizes = ary.shape();
// using Mathematica array notation
os << "{";
for ( …Run Code Online (Sandbox Code Playgroud) 我一直在研究boost :: multi_array库,寻找一个迭代器,它允许你在一个for循环中遍历整个 multi_array.
我不认为该库中有任何这样的迭代器.(在那里找到的迭代器允许您遍历multi_array的单个维度)
我错了吗?
如果没有,是否有任何库定义这样的迭代器?
进入细节,我想写一些类似的东西:
boost::multi_array< double, 3 > ma(boost::extents[3][4][2]);
for( my_iterator it = ma.begin(); it != ma.end(); ++it )
{
// do something
// here *it has element type (in this case double)
}
Run Code Online (Sandbox Code Playgroud)
并获得一个重复3x4x2次的循环
我有一个n维Boost.MultiArray我初始化如下:
const int n=3, size=4; //# of dimensions and size of one dimension
boost::multi_array<char,n> arr;
boost::array<size_t,n> extents; //size of each dimension
extents.assign(size); //assign size to each dimension -> {{4, 4, 4}}
arr.resize(extents);
Run Code Online (Sandbox Code Playgroud)
所以我有4行代码来获取MultiArray,但我想在一行中完成.是否有任何简单的方法来生成具有n个维度的MultiArray,每个维度都有size长度(所以我可以写arr(samevaluearray(n,size)))或者我是否错过了MultiArray的方便构造函数?
编辑:它应该工作,不依赖于n的某个值,即arr({{size,size}}仅适用于n=2.
由于可能不太清楚:boost::multi_array<char,n>(boost::extents[4][4][4])正确初始化4x4x4阵列,但每次n在源代码中更改时,每次初始化都必须手动更新,因此它不是一个选项.
我对使用Boost的C++很陌生.
我希望类"world"的对象有一个名为"chunk"的数组"octreenode".以前我有一个普通的一维数组,这很好用.现在我正在尝试使用具有Boost的multi_array功能的3D数组,我真的不确定我做错了什么.
简化代码:
class world {
public:
typedef boost::multi_array<octreenode, 3> planetchunkarray; // a boost_multi for chunks
typedef planetchunkarray::index index;
planetchunkarray *chunk;
world(double x,double y,double z,
int widtheast, int widthnorth, int height) :
originx(x), originy(y), originz(z),
chunkseast(widtheast), chunksnorth(widthnorth), chunksup(height) {
chunk = new planetchunkarray(boost::extents[chunksnorth][chunkseast][chunksup]);
planetchunkarray::extent_gen extents;
for (int cz = 0; cz < chunksnorth; ++cz) {
for (int cx = 0; cx < chunkseast; ++cx) {
for (int cy = 0; cy < chunksup; ++cy) {
(*chunk)[cz][cx][cy] = new octreenode(1,72);
} …Run Code Online (Sandbox Code Playgroud) c++ ×10
boost ×9
iterator ×2
armadillo ×1
blitz++ ×1
c++-concepts ×1
performance ×1
pointers ×1