c++ - Eigen :累积可变大小的数组

标签 c++ eigen

我有一个包含 Eigen::Array 数据的类和一个通过沿第一个轴附加到数组来添加新数据(行数可能不同)的方法。我通过创建一个大小合适的新数组并使用新旧数据对其进行初始化来解决累积问题。

typedef Eigen::Array<double, Eigen::Dynamic, 3> DataArray

class Accumulator {
    void add(DataArray &new_data) {
        DataArray accu(accumulated_data_.rows() + new_data.rows(), 3)
        accu << accumulated_data_, new_data;
        accumulated_data_ = accu;
    }

    DataArray accumulated_data_;
}

这样做有什么问题吗?或者最好调整累积数据数组的大小:

  • .resize() 复制旧的和新的
  • .conservative_resize()并复制新数据(如果新数据超过1行,则需要分块操作)

最佳答案

首先,您当前的实现有两个易于修复的缺陷:

  • Eigen 默认以列优先顺序存储数组(和矩阵),因此如果您要追加行,您应该更喜欢 RowMajor存储顺序:

    Eigen::Array<double, Eigen::Dynamic, 3, Eigen::RowMajor>

  • accu将不再使用,你应该将它移动到累加器: accumulated_data_ = std::move(accu);

    如果你是 C++11 之前的版本,你也可以交换数据:

    accumulated_data_.swap(accu);

那么你的方法几乎等同于

accumulated_data_.conservativeResize(accumulated_data_.rows() + new_data.rows(), 3);
accumulated_data_.bottomRows(new_data.rows()) = new_data;

您仍然会在每次调用时进行内存(重新)分配和内存复制。

一种更有效的方法是调整 accumulated_data_ 的大小只是偶尔(最好在开始时就一次),并跟踪其中有多少是当前实际有效的:

typedef Eigen::Array<double, Eigen::Dynamic, 3, Eigen::RowMajor> DataArray;

class Accumulator {
public:
    Accumulator(Eigen::Index initialCapacity=10000) : accumulated_data_(initialCapacity, 3), actual_rows_(0) {}
    void add(DataArray &new_data) {
        if(actual_rows_+new_data.rows() > accumulated_data_.rows())
        { // TODO adapt memory-growing to your use case
             accumulated_data_.conservativeResize(2*actual_rows_+new_data.rows(), 3);
        }
        accumulated_data_.midRows(actual_rows, new_data.rows()) = new_data;
        actual_rows_+=new_data.rows();
    }

    DataArray accumulated_data_;
    Eigen::Index actual_rows_;
};

关于c++ - Eigen :累积可变大小的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49882761/

相关文章:

c++ - std::thread 消耗 Qt 中的所有 CPU

c++ - 在字符串中使用空字符 (C++)

c++ - Eigen 中的稀疏矩阵构造

c++ - C++ 中的 Eigen::Tensor 声明是安全的还是有问题的?我应该为此提交一个问题吗?

c++ - 比较一个字符串的两半

c++ - c32rtomb 转换成什么编码?

c++ - 实现 3d vector 数组的最佳方法是什么?

c++ - 如何在 C++ 中将特征张量乘以另一个特征张量的标量和?

c++ - 泛化特征类型的输入参数

c++ - 指向用户定义结构的智能指针的成员初始化列表