sum<rowwise> of DynamicMatrix<bool> producing error output
Consider the below code:
blaze::DynamicMatrix<bool> A{ { 1, 0, 1 }, { 1, 1, 1 } }; blaze::DynamicVector<int, blaze::columnVector> rowsum1 = blaze::sum<blaze::rowwise>(A);
It produces (1, 1)
as output where as I would expect the row sums to be (2, 3)
Even an explicit function such as blaze::Add()
with reduce
returns the same (1,1) instead of the correct sum values.
blaze::DynamicMatrix<bool> A{ { 1, 0, 1 }, { 1, 1, 1 } }; blaze::DynamicVector<int, blaze::columnVector> rowsum2 = blaze::reduce<blaze::rowwise>(A, blaze::Add());
Note that the return type is expected to be DynamicVector<int>
> while the sum
and reduce
seem to be basing the return value calculations on the input type DynamicMatrix<bool>
thus clamping the outputs to be just 0 or 1.
While that may be useful in some cases, often times the sum
is expected to be int
and not just bool
.
Is there anyway we could ask the sum
or reduce
to return the int
values ?
As of now the balze::Add
is working same as blaze::Or
which is confusing.
Comments (6)

reporter 
reporter After digging into the code, came across this
dvecreduce
that is defined as below:template< typename VT // Type of the dense vector , bool TF // Transpose flag , typename OP > // Type of the reduction operation inline auto dvecreduce( const DenseVector<VT,TF>& dv, OP op ) > EnableIf_t< DVecReduceExprHelper<VT,OP>::value, ElementType_t<VT> > { using CT = CompositeType_t<VT>; using ET = ElementType_t<VT>; ... ET redux{}; ... return redux; }
Note that the return type of the reduce method is governed by the input element type
ET
rather than the output type of theop
For example, an input vector of
string
s that is being reduced to anint
by anop
function that counts how many strings in the vector start with the lettera
Such function is not possible to implement when the return type is expected to be same as the input element type of the vector
How to overcome this situation? Any help is really appreciated.

Hi GK!
Thanks a lot for reporting this issue. You are correct, the correct answer would be
{2,3}
instead of{1,1}
. Also, you have already identified the source of the issue. Indeed, thedvecreduce()
operations do not return the type returned by the reduction operation, but the element type of the vector expression. Will provide a fix as quickly as possible. In the meantime, you can easily work around this by using the rowwise reduction operation on a columnmajor matrix:blaze::DynamicMatrix<bool,blaze::columnMajor> A{ { 1, 0, 1 }, { 1, 1, 1 } }; // Note: Columnmajor matrix! // Will produce (2,3) instead of (1,1) blaze::DynamicVector<int, blaze::columnVector> rowsum2 = blaze::reduce<blaze::rowwise>(A, [](int a, int b) { return a + b; });
We apologize for the inconvenience,
Best regards,
Klaus!


assigned issue to

assigned issue to

 changed status to open

 changed status to resolved
Commit cb70dcb fixes the return type of nonvectorized reduction operations for dense and sparse vectors and dense and sparse matrices. The fix is immediately available via cloning the Blaze repository and will be officially released in Blaze 3.9.
 Log in to comment
Even an explicit lambda function that adds the values explicitly is not giving the correct return values.
Not sure how to return the summed value as integers correctly.