Is there a count_if function for gsl_vector? C / C ++

I use the gnu science library (GSL). Say I have gsl_vector : 7 0 -6 5 8 0 10 -2

This is a vector containing positive numbers, negative numbers, and zeros as elements.

I want to count the number of non-zero elements or zero elements in this gsl_vector . I know that there is a function called count_if for C ++ Vector. But I look through gsl_vector.h and gsl_blas.h , there is no function equal to this. I can go through at least all the elements, evaluate them, although gsl_vector_get() , and then ask the if question.

 int counter = 0; for(int i = 0;i<length_of_the_gsl_vector;++i){ if(fabs(gsl_vector_get(y,i))<0.5) ++counter; } return counter; 

But I was wondering for almost the whole day whether there is such a feature already in GSL, which is much more efficient.

Or maybe there is a count_if function for gsl_array ?

+4
source share
2 answers

You can get a data pointer using gsl_vector_ptr , then use std::count_if for pointers:

 struct Predicate{ inline bool operator()(double x) const { return fabs(x) < 0.5 ; } } ; int res = std::count_if( gsl_vector_ptr(y,0), gsl_vector_ptr(y,0) + size, Predicate() ) ; 
+2
source

They state that GSL implements std :: valarray , I read such a statement to allow me to use something like:

 gsl_block vec; /* initialize ... */ std::valarray<double> a(vec.data, vec.size); /* use std:: galore */ 
+1
source

All Articles