调整boost :: multi_array匹配另一个
resizing boost::multi_array to match another
我需要将一个多_array大小调整到另一个大小。
在Blitz 中我只能做
arr1.resize(arr2.shape());
是否有类似长度的多_array解决方案?因为
arr1.resize(boost::extents[arr2.shape()[0]][arr2.shape()[1]]);
似乎有点长而艰巨。
您可以使用shape()
成员。可悲的是,它不能直接用作ExtentList
(它不能建模Collection
概念),但很容易将其制成一个:
using MA = multi_array<double, 2>;
MA ma(extents[12][34]);
auto& ma_shape = reinterpret_cast<boost::array<size_t, MA::dimensionality> const&>(*ma.shape());
这样
// demo
std::cout << "[" << ma_shape[0] << "][" << ma_shape[1] << "]n";
打印[12][34]
。
现在,ma_shape
可以直接用于重塑/调整另一个数组:
活在coliru
#include <boost/multi_array.hpp>
#include <iostream>
int main() {
using namespace boost;
using MA = multi_array<double, 2>;
MA ma(extents[12][34]);
auto& ma_shape = reinterpret_cast<boost::array<size_t, MA::dimensionality> const&>(*ma.shape());
// demo
std::cout << "[" << ma_shape[0] << "][" << ma_shape[1] << "]n";
// resize
MA other;
assert(!std::equal(ma_shape.begin(), ma_shape.end(), other.shape()));
other.resize(ma_shape);
assert(std::equal(ma_shape.begin(), ma_shape.end(), other.shape()));
// reshape
other.resize(extents[1][12*34]);
assert(!std::equal(ma_shape.begin(), ma_shape.end(), other.shape()));
other.reshape(ma_shape);
assert(std::equal(ma_shape.begin(), ma_shape.end(), other.shape()));
}
我认为这是对boost.multiarray的另一个监督。我写了一堆"效用"函数,这些功能允许呈现形状(尺寸),基本索引(例如,每个维度为0或1)和扩展(每个维度中的基本指数和大小)。
namespace boost{
template<class MultiArray>
detail::multi_array::extent_gen<MultiArray::dimensionality>
extension(MultiArray const& ma){ //this function is adapted from
typedef detail::multi_array::extent_gen<MultiArray::dimensionality> gen_type;
gen_type ret;
typedef typename gen_type::range range_type;
for(int i=0; i != MultiArray::dimensionality; ++i)
ret.ranges_[i] = range_type(ma.index_bases()[i], ma.index_bases()[i]+ma.shape()[i]);
return ret;
}
}
后来用作:
boost::multi::array<double, 3> m(boost::multi::extents[3][4][5]);
boost::multi::array<double, 3> n(extension(m)); // n takes the extension (and shape) of m
(如果基本索引不是零,则它也有效。它也适用于视图和其他Multi_array-type类。)
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 运行同一解决方案的另一个项目的项目
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- C++从另一个类访问公共静态向量的正确方法是什么
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 使用std::transform将一个范围的元素添加到另一个范围中
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- 修改函数中的指针(将另一个指针作为参数传递)
- 为什么我不能将一个对象push_back到属于另一个类的对象向量中?
- C++试图读取一个文件并输出到另一个文本文件
- 如何将指针从一个void函数传递到另一个C++
- 如何从另一个文件继承私有成员变量和公共函数
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- C++-我可以创建另一个类的成员并在构造函数中使用它吗
- 将 std::array 移动到另一个 std::array
- std::array和std::vector的区别是什么?什么时候你用一个而不是另一个