在 c++ 上的犰狳中,稀<sp_mat><dim>疏矩阵上的 sum(,) 不起作用

In armadillo on c++, sum(<sp_mat>,<dim>) on sparse matrices does not work

本文关键字:lt gt dim sum 不起作用 sp c++ mat      更新时间:2023-10-16

以下代码:

#include <iostream>
#include <armadillo>
using namespace std;
using namespace arma;
int main()
{
        sp_mat A = speye<sp_mat>(5,5);
        rowvec s1 = max(A,0);
        return 0;
}

给出以下编译时错误:

benchmark.cpp: In function ‘int main()’:
benchmark.cpp:11:21: error: conversion from ‘arma::enable_if2<true, const arma::SpOp<arma::SpMat<double>, arma::spop_max> >::result {aka const arma::SpOp<arma::SpMat<double>, arma::spop_max>}’ to non-scalar type ‘arma::rowvec {aka arma::Row<double>}’ requested
  rowvec s1 = max(A,0);
                     ^
make: *** [all] Error 1

稀疏矩阵上的min、sum和其他运算也是如此,而它们对密集矩阵非常有效。我是不是做错了什么?

对稀疏矩阵的最大运算将产生稀疏矩阵。

将您的代码更改为:

sp_mat A = speye<sp_mat>(5,5);
sp_mat s1 = max(A,0);

为了计算每行的总和,我使用了矩阵乘法:

sp_mat A = sprandu<sp_mat>(5, 5);
mat sumRows = A * ones(A.n_cols, 1);