使用单个索引访问子矩阵的有效方式
Efficient way to access submatrices with single index
我正在尝试对具有单个索引的子矩阵执行跨步访问。我需要这个来创建一个使用表达式模板的库。我已经计算出了以下类,其中访问是由过载的operator[]
执行的,请参见以下内容:
template <class A, class Type>
class SubMatrixExpr
{
private:
int Rows_; // Rows of the SubMatrix
int Columns_; // Columns of the SubMatrix
int Rows_up_; // Rows of the original Matrix
int Columns_up_; // Columns of the original Matrix
int a_, c_; // Starting indices of the SubMatrix as evaluated in the original Matrix
int rowstep_, columnstep_; // Stride along rows and columns for the original matrix
A M_;
public:
SubMatrixExpr(A &M, int Rows_up, int Columns_up, int Rows, int Columns, int a, int rowstep, int c, int columnstep) :
a_(a), c_(c), M_(M),
Rows_(Rows),
Columns_(Columns),
Rows_up_(Rows_up), Columns_up_(Columns_up),
rowstep_(rowstep), columnstep_(columnstep) { }
inline const Type& operator[](const int i) const
{
const int LocalRow = i/Columns_;
const int LocalColumn = i%Columns_;
const int GlobalRow = a_+rowstep_*LocalRow;
const int GlobalColumn = c_+columnstep_*LocalColumn;
return M_[IDX2R(GlobalRow,GlobalColumn,Columns_up_)];
}
inline Type& operator[](const int i)
{
// Similar to above
}
};
其中
#define IDX2R(i,j,N) (((i)*(N))+(j))
过载的operator[]
工作正常,但计算成本很高。
有什么方法可以更好地实现过载的operator[]
吗
提前非常感谢。
获得加速的唯一方法是在编译时调整矩阵和子矩阵的大小。然后使用template/constexpr可能会加快速度。例如,如果在编译时已知大小是2的幂,则编译器将能够用移位代替除法。
相关文章:
- 我们如何并行运行算法的 n 个实例并以有效的方式计算结果函数的平均值?
- 只需要知道我在c ++中打印模式的方式是否有效,或者有另一种方法可以有效地做到这一点
- 如何以更有效的方式检查一个数字是否是素数?
- 为什么以这种方式使用迭代器有效?
- EIGEN地图类:将C阵列映射到VectorXD指针以有效的方式
- 以有效的方式从 Node .js运行 C 或C++代码
- 在这种情况下,如何以有效的方式使用模板
- 可能以有效的方式在模拟的循环中进行多线程
- 更有效的填充unordered_set方式?
- 持有多个特征矩阵Xd的最有效方式
- 存储资源的最有效方式
- 存储IPv4/IPv6地址的有效方式
- 使用单个索引访问子矩阵的有效方式
- 发出信号并保持pthread打开的有效方式
- 顺时针和逆时针旋转阵列的有效方式
- 使用Qt和OpenCV读取和处理视频文件的有效方式
- 这是将字符串作为参数传递给函数的内存有效方式
- 这是在c++中运行循环的有效方式吗?
- 在内存映射文件中交换字节的有效方式
- 将跨步数据复制到CUDA设备和从CUDA设备复制跨步数据的有效方式