将特征稀疏矩阵添加到特征张量
Add an Eigen's SparseMatrix to an Eigen's Tensor
如何将特征的sparsematrix添加到特征的张量?
以下代码(未编译(解释了我要做的事情。
#include <iostream>
#include <Eigen/Sparse>
#include <unsupported/Eigen/CXX11/Tensor>
using Eigen::Tensor;
using Eigen::SparseMatrix;
int main()
{
Tensor<double, 2> tensor(10, 10);
for(int i=0; i < 10; i++) {
for(int j=0; j < 10; j++) {
tensor(i, j) = i * 10 + j;
}
}
SparseMatrix<double> sparse(10, 10);
auto tensor2 = tensor;
tensor2 += sparse;
std::cout << tensor2 << std::endl;
}
显然,这未实现。您必须自己对这两种类型的operator+=
超载。请参阅此表以获取正确的签名。另请参阅»在非零系数上迭代«在特征文档中如何有效迭代稀疏矩阵。
#include <iostream>
#include <Eigen/Sparse>
#include <unsupported/Eigen/CXX11/Tensor>
using Eigen::Tensor;
using Eigen::SparseMatrix;
template < typename T >
Tensor<T,2>& operator+=(Tensor<T,2>& lhs, SparseMatrix<T> const& rhs)
{
for (int k = 0; k < rhs.outerSize(); ++k)
for (typename SparseMatrix<T>::InnerIterator it(rhs,k); it; ++it)
lhs(it.row(), it.col()) = it.value();
return lhs;
}
int main()
{
Tensor<double, 2> tensor(10, 10);
for(int i=0; i < 10; i++) {
for(int j=0; j < 10; j++) {
tensor(i, j) = i * 10 + j;
}
}
// We want a sparse matrix that is not only zeros
Eigen::MatrixXd m = Eigen::MatrixXd::Zero(10,10);
m(0,0) = 1;
SparseMatrix<double> sparse(10, 10);
sparse = m.sparseView();
auto tensor2 = tensor;
tensor2 += sparse;
std::cout << tensor2 << std::endl;
}
您的意思是在+=
方法中:
lhs(it.row(), it.col()) += it.value();
而不是
lhs(it.row(), it.col()) = it.value();
?
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 如何在C++中从两个单独的for循环中添加两个数组
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 如何仅为一个函数添加延迟
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 如何将更多文件夹添加到c++include路径
- 如何将元素添加到数组的线程安全函数?
- QT通过C++添加映射QML项目
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- 在特征3中添加矩阵和对角矩阵
- 将特征稀疏矩阵添加到特征张量
- C++ 特征:为什么可以为方法添加赋值,但不能为方法添加 Diagonal()
- 通过cmake添加外部库(例如,特征)
- 如何编写特征转换以将"const"添加到类型*
- 向多个自定义c++ BLE GATT服务添加特征用户描述
- 向自定义c++ BLE GATT服务添加特征用户描述