简体   繁体   中英

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

Following code:

#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;
}

gives the following compile time error:

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

Same for min, sum and other operations on sparse matrices, while they work perfectly well for dense matrices. Am I doing something wrong here?

The max operation on a sparse matrix will result in a sparse matrix.

Change your code to:

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

To calculate the sum of each row, I've used matrix multiplication:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM