Use std::count to count equal values in a pair of arrays - c++

Given a pair of equal length arrays . .
void someFunc (const float **inputChannelData)
{
const float* L = inputChannelData[0];
const float* R = inputChannelData[1];
...
I can see that it is pretty easy to count the number of occurrences of a constant value using std::count . .
std::count(L, L+someIntegerOffset, 0.0f)
... but is there a standard algorithm (or idiom) that counts the element-wise (same index in each array) number of identical values in two arrays?
A Matlab equivalent would look like sum(L==R)

std::count has a predicate form std::count_if, and documentation suggest it gets the argument by lvalue.
So I believe I could hack together a lambda that captures the two arrays, from &arg gets the distance/index, then checks both values. I doubt I would use it that way though instead of old-fashioned for loop.

You can use zip for this, look here Sequence-zip function for c++11?
And then count_if with predicate, that tuple has only identical elements.
Also, you can simply create array of pairs and do the same.
Something like this for count with pairs.
void someFunc(const float** inputChannelData, size_t offset)
{
const float* R = inputChannelData[0];
const float* L = inputChannelData[1];
std::vector<std::pair<int, int>> pairs;
std::transform(L, L + offset, R, std::back_inserter(pairs),
[](float l, float r)
{
return std::make_pair(l, r);
});
size_t equal_pairs = std::count_if(pairs.begin(), pairs.end(),
[](const std::pair<float, float>& p)
{
return p.first == p.second;
});
}

Related

How to know how many elements are in the result of thrust::partition_copy

I am trying to partition an array with the thrust library's partition_copy function.
I have seen examples where pointers are passed, but I need to know how many elements are in each partition.
What I have tried is to pass device vectors as the OutputIterator parameters, something like this:
#include <thrust/device_vector.h>
#include <thrust/device_ptr.h>
#include <thrust/partition.h>
struct is_even {
__host__ __device__ bool operator()(const int &x) {
return (x % 2) == 0;
}
};
int N;
int *d_data;
cudaMalloc(&d_data, N*sizeof(int));
//... Some data is put in the d_data array
thrust::device_ptr<int> dptr_data(d_data);
thrust::device_vector<int> out_true(N);
thrust::device_vector<int> out_false(N);
thrust::partition_copy(dptr_data, dptr_data + N, out_true, out_false, is_even());
When I try to compile I get this error:
error: class "thrust::iterator_system<thrust::device_vector<int, thrust::device_allocator<int>>>" has no member "type"
detected during instantiation of "thrust::pair<OutputIterator1, OutputIterator2> thrust::partition_copy(InputIterator, InputIterator, OutputIterator1, OutputIterator2, Predicate) [with InputIterator=thrust::device_ptr<int>, OutputIterator1=thrust::device_vector<int, thrust::device_allocator<int>>, OutputIterator2=thrust::device_vector<int, thrust::device_allocator<int>>, Predicate=leq]"
So my question is: How can you use either thrust::partition or thrust::partition_copy and know how many elements you ended up with in each partition?
Your compile error is due to the fact that you are passing vectors instead of iterators here:
thrust::partition_copy(dptr_data, dptr_data + N, out_true, out_false, is_even());
^^^^^^^^^^^^^^^^^^^
Instead you should pass iterators based on those containers:
thrust::partition_copy(dptr_data, dptr_data + N, out_true.begin(), out_false.begin(), is_even());
In order to get the lengths of the results, we must use the return value of thrust::partition copy():
Returns
A pair p such that p.first is the end of the output range beginning at out_true and p.second is the end of the output range beginning at out_false.
Something like this:
auto r = thrust::partition_copy(dptr_data, dptr_data + N, out_true.begin(), out_false.begin(), is_even());
int length_true = r.first - out_true.begin();
int length_false = r.second - out_false.begin();
Note that a similar method can be used with other thrust algorithms. Those that don't return a tuple will be even easier to work with.
For example:
auto length = (thrust::remove_if(A.begin(), A.end(), ...) - A.begin());

how to sum up a vector of vector int in C++ without loops

I try to implement that summing up all elements of a vector<vector<int>> in a non-loop ways.
I have checked some relevant questions before, How to sum up elements of a C++ vector?.
So I try to use std::accumulate to implement it but I find it is hard for me to overload a Binary Operator in std::accumulate and implement it.
So I am confused about how to implement it with std::accumulate or is there a better way?
If not mind could anyone help me?
Thanks in advance.
You need to use std::accumulate twice, once for the outer vector with a binary operator that knows how to sum the inner vector using an additional call to std::accumulate:
int sum = std::accumulate(
vec.begin(), vec.end(), // iterators for the outer vector
0, // initial value for summation - 0
[](int init, const std::vector<int>& intvec){ // binaryOp that sums a single vector<int>
return std::accumulate(
intvec.begin(), intvec.end(), // iterators for the inner vector
init); // current sum
// use the default binaryOp here
}
);
In this case, I do not suggest using std::accumulate as it would greatly impair readability. Moreover, this function use loops internally, so you would not save anything. Just compare the following loop-based solution with the other answers that use std::accumulate:
int result = 0 ;
for (auto const & subvector : your_vector)
for (int element : subvector)
result += element;
Does using a combination of iterators, STL functions, and lambda functions makes your code easier to understand and faster? For me, the answer is clear. Loops are not evil, especially for such simple application.
According to https://en.cppreference.com/w/cpp/algorithm/accumulate , looks like BinaryOp has the current sum on the left hand, and the next range element on the right. So you should run std::accumulate on the right hand side argument, and then just sum it with left hand side argument and return the result. If you use C++14 or later,
auto binary_op = [&](auto cur_sum, const auto& el){
auto rhs_sum = std::accumulate(el.begin(), el.end(), 0);
return cur_sum + rhs_sum;
};
I didn't try to compile the code though :). If i messed up the order of arguments, just replace them.
Edit: wrong terminology - you don't overload BinaryOp, you just pass it.
Signature of std::accumulate is:
T accumulate( InputIt first, InputIt last, T init,
BinaryOperation op );
Note that the return value is deduced from the init parameter (it is not necessarily the value_type of InputIt).
The binary operation is:
Ret binary_op(const Type1 &a, const Type2 &b);
where... (from cppreference)...
The type Type1 must be such that an object of type T can be implicitly converted to Type1. The type Type2 must be such that an object of type InputIt can be dereferenced and then implicitly converted to Type2. The type Ret must be such that an object of type T can be assigned a value of type Ret.
However, when T is the value_type of InputIt, the above is simpler and you have:
using value_type = std::iterator_traits<InputIt>::value_type;
T binary_op(T,value_type&).
Your final result is supposed to be an int, hence T is int. You need two calls two std::accumulate, one for the outer vector (where value_type == std::vector<int>) and one for the inner vectors (where value_type == int):
#include <iostream>
#include <numeric>
#include <iterator>
#include <vector>
template <typename IT, typename T>
T accumulate2d(IT outer_begin, IT outer_end,const T& init){
using value_type = typename std::iterator_traits<IT>::value_type;
return std::accumulate( outer_begin,outer_end,init,
[](T accu,const value_type& inner){
return std::accumulate( inner.begin(),inner.end(),accu);
});
}
int main() {
std::vector<std::vector<int>> x{ {1,2} , {1,2,3} };
std::cout << accumulate2d(x.begin(),x.end(),0);
}
Solutions based on nesting std::accumulate may be difficult to understand.
By using a 1D array of intermediate sums, the solution can be more straightforward (but possibly less efficient).
int main()
{
// create a unary operator for 'std::transform'
auto accumulate = []( vector<int> const & v ) -> int
{
return std::accumulate(v.begin(),v.end(),int{});
};
vector<vector<int>> data = {{1,2,3},{4,5},{6,7,8,9}}; // 2D array
vector<int> temp; // 1D array of intermediate sums
transform( data.begin(), data.end(), back_inserter(temp), accumulate );
int result = accumulate(temp);
cerr<<"result="<<result<<"\n";
}
The call to transform accumulates each of the inner arrays to initialize the 1D temp array.
To avoid loops, you'll have to specifically add each element:
std::vector<int> database = {1, 2, 3, 4};
int sum = 0;
int index = 0;
// Start the accumulation
sum = database[index++];
sum = database[index++];
sum = database[index++];
sum = database[index++];
There is no guarantee that std::accumulate will be non-loop (no loops). If you need to avoid loops, then don't use it.
IMHO, there is nothing wrong with using loops: for, while or do-while. Processors that have specialized instructions for summing arrays use loops. Loops are a convenient method for conserving code space. However, there may be times when loops want to be unrolled (for performance reasons). You can have a loop with expanded or unrolled content in it.
With range-v3 (and soon with C++20), you might do
const std::vector<std::vector<int>> v{{1, 2}, {3, 4, 5, 6}};
auto flat = v | ranges::view::join;
std::cout << std::accumulate(begin(flat), end(flat), 0);
Demo

Fast way to do lexicographical comparing 2 numbers

I'm trying to sort a vector of unsigned int in lexicographical order.
The std::lexicographical_compare function only supports iterators so I'm not sure how to compare two numbers.
This is the code I'm trying to use:
std::sort(myVector->begin(),myVector->end(), [](const unsigned int& x, const unsigned int& y){
std::vector<unsigned int> tmp1(x);
std::vector<unsigned int> tmp2(y);
return lexicographical_compare(tmp1.begin(),tmp1.end(),tmp2.begin(),tmp2.end());
} );
C++11 introduces std::to_string
You can use from to_string as below:
std::sort(myVector->begin(),myVector->end(), [](const unsigned int& x, const unsigned int& y){
std::string tmp1 = std::to_string(x);
std::string tmp2 = std::to_string(y);
return lexicographical_compare(tmp1.begin(),tmp1.end(),tmp2.begin(),tmp2.end());
} );
I assume you have some good reasons, but allow me to ask: Why are you sorting two int's by using the std::lexicographical order? In which scenario is 0 not less than 1, for example?
I suggest for comparing the scalars you want to use std::less . Same as std lib itself does.
Your code (from the question) might contain a lambda that will use std::less and that will work perfectly. But let us go one step further and deliver some reusable code ready for pasting into your code. Here is one example:
/// sort a range in place
template< typename T>
inline void dbj_sort( T & range_ )
{
// the type of elements range contains
using ET = typename T::value_type;
// use of the std::less type
using LT = std::less<ET>;
// make its instance whose 'operator ()'
// we will use
LT less{};
std::sort(
range_.begin(),
range_.end(),
[&]( const ET & a, const ET & b) {
return less(a, b);
});
}
The above is using std::less<> internally. And it will sort anything that has begin() and end() and public type of the elements it contains. In other words implementation of the range concept.
Example usage:
std::vector<int> iv_ = { 13, 42, 2 };
dbj_sort(iv_);
std::array<int,3> ia_ = { 13, 42, 2 };
dbj_sort(ia_);
std:: generics in action ...
Why is std::less working here? Among other obvious things, because it compares two scalars. std::lexicographical_compare compares two ordinals.
std::lexicographical_compare might be used two compare two vectors, not two elements from one vector containing scalars.
HTH

sum of non-integer elements in std::vector

I was reading following question:
How to sum up elements of a C++ vector?, and I wanted to use second method (sum_of_elems =std::accumulate(vector.begin(),vector.end(),0);//#include <numeric>).
However, I don't have std::vector<int>, but std::vector<struct packet>. The packet is defined as following:
struct packet {
/// ...
int length() const;
///
}
and I want sum of packet lengths.
This is what I tried:
std::vector<packet> packets;
...
std::accumulate(packets.begin(), packets.end(), 0, [](const packet& a, const packet& b){return a.length() + b.length();});
but it doesn't work. In C#, I'd write something like
packet[] p;
p.Select(x => p.length()).Sum();
Is it possible to do something like that in C++? I can write method for iterating through the vector and doing it on my own, but I prefer the functional approach where possible.
I would note that the C# implementation is slightly different, in essence.
In C++ you are trying to add int and packet whilst in C# you first provide a transformation step from packet to int and then add ints.
The equivalent C++, without adaptation:
std::vector<size_t> lengths; // a length cannot be negative!
std::transform(packets.begin(),
packets.end(),
backward_inserter(lengths),
[](packet const& p) { return p.length(); });
auto const sum = std::accumulate(lengths.begin(), lengths.end(), 0ul);
Of course, it is wasteful to store the intermediate lengths, however it does work out of the box.
But because we are cool, let us have look at Boost.Range, and more precisely:
boost::accumulate
boost::transformed
Which have a bit of coolness like Linq:
#include <boost/range/numeric.hpp> // accumulate
#include <boost/range/adaptor/transformed.hpp>
size_t total_size(std::vector<packet> const& packets) {
return boost::accumulate(
packets | boost::transformed([](packet const& p) { return p.length(); }),
0ul);
}
You are accumulating via a binary operation. Your accumulated value starts with 0 (an int), so the left hand side of your binary operation has to be convertible-from 0 -- otherwise, how does it start adding?
Try this:
std::accumulate(
packets.begin(),
packets.end(),
0,
[](int a, const packet& b){
return a + b.length();
}
);
you can also do this via a simple loop:
int acc = 0;
for( const packet& p : packets ) {
acc += packets.length();
}
The first parameter of the accumulate operation is the running total. In your case, this is an integer, not a packet, so your lambda should be
[](int a, const packet& b) {return a + b.length();}
The problem is your accumulate function. Its first parameter has to be of the type you're trying to accumulate (int in this case) and add a value on top of that.
Your lambda function should look like this: [](int currTotal, const packet& b){return currTotal + b.length();}
Apart from lamba, you can change it to
std::accumulate(packets.begin(), packets.end(), 0, packet());
Where you can define functor as:
int operator() (int result, const packet& obj)
{
return result+ obj.length();
}

dot product of vector < vector < int > > over the first dimension

I have
vector < vector < int > > data_mat ( 3, vector < int > (4) );
vector < int > data_vec ( 3 );
where data_mat can be thought of as a matrix and data_vec as a column vector, and I'm looking for a way to compute the inner product of every column of data_mat with data_vec, and store it in another vector < int > data_out (4).
The example http://liveworkspace.org/code/2bW3X5%241 using for_each and transform, can be used to compute column sums of a matrix:
sum=vector<int> (data_mat[0].size());
for_each(data_mat.begin(), data_mat.end(),
[&](const std::vector<int>& c) {
std::transform(c.begin(), c.end(), sum.begin(), sum.begin(),
[](int d1, double d2)
{ return d1 + d2; }
);
}
);
Is it possible, in a similar way (or in a slightly different way that uses STL functions), to compute column dot products of matrix columns with a vector?
The problem is that the 'd2 = d1 + d2' trick does not work here in the column inner product case -- if there is a way to include a d3 as well that would solve it ( d3 = d3 + d1 * d2 ) but ternary functions do not seem to exist in transform.
In fact you can use your existing column sum approach nearly one to one. You don't need a ternary std::transform as inner loop because the factor you scale the matrix rows with before summing them up is constant for each row, since it is the row value from the column vector and that iterates together with the matrix rows and thus the outer std::for_each.
So what we need to do is iterate over the rows of the matrix and multiply each complete row by the corresponding value in the column vector and add that scaled row to the sum vector. But unfortunately for this we would need a std::for_each function that simultaneously iterates over two ranges, the rows of the matrix and the rows of the column vector. To achieve this, we could use the usual unary std::for_each and just do the iteration over the column vector manually, using an additional iterator:
std::vector<int> sum(data_mat[0].size());
auto vec_iter = data_vec.begin();
std::for_each(data_mat.begin(), data_mat.end(),
[&](const std::vector<int>& row) {
int vec_value = *vec_iter++; //manually advance vector row
std::transform(row.begin(), row.end(), sum.begin(), sum.begin(),
[=](int a, int b) { return a*vec_value + b; });
});
The additional manual iteration inside the std::for_each isn't really that idiomatic use of the standard library algorithms, but unfortunately there is no binary std::for_each we could use.
Another option would be to use std::transform as outer loop (which can iterate over two ranges), but we don't really compute a single value in each outer iteration to return, so we would have to just return some dummy value from the outer lambda and throw it away by using some kind of dummy output iterator. That wouldn't be the cleanest solution either:
//output iterator that just discards any output
struct discard_iterator : std::iterator<std::output_iterator_tag,
void, void, void, void>
{
discard_iterator& operator*() { return *this; }
discard_iterator& operator++() { return *this; }
discard_iterator& operator++(int) { return *this; }
template<typename T> discard_iterator& operator=(T&&) { return *this; }
};
//iterate over rows of matrix and vector, misusing transform as binary for_each
std::vector<int> sum(data_mat[0].size());
std::transform(data_mat.begin(), data_mat.end(),
data_vec.begin(), discard_iterator(),
[&](const std::vector<int>& row, int vec_value) {
return std::transform(row.begin(), row.end(),
sum.begin(), sum.begin(),
[=](int a, int b) {
return a*vec_value + b;
});
});
EDIT: Although this has already been discussed in comments and I understand (and appreciate) the theoretic nature of the question, I will still include the suggestion that in practice a dynamic array of dynamic arrays is an awfull way to represent such a structurally well-defined 2D array like a matrix. A proper matrix data structure (which stores its contents contigously) with the appropriate operators is nearly always a better choice. But nevertheless due to their genericity you can still use the standard library algorithms for working with such a custom datastructure (maybe even by letting the matrix type provide its own iterators).