It is not difficult to implement element-wise product in C++:
vector<float> a_array;
vector<float> b_array;
vector<float> c_array;
vector<float> dot_array;
....
for(int i=0; i<a_array.size(); i++)
{
float temp;
temp = a_array[i]*b_array[i]*c_array[i];
dot_array[i] = temp;
}
This is a very simple implementation, and I am wondering whether there are more efficient algorithms already available in STL. Thanks!
std::transform
can be used to multiply two vectors:
#include <algorithm>
#include <functional>
// Precondition: b.size() >= a.size() && r.size() >= a.size()
std::transform(a.begin(), a.end(), b.begin(), r.begin(), std::multiplies<float>());
There is no similar standard algorithm with more than two input sequences.
Have a look at std::inner_product.
Edit I posted this answer when the question was about dot product. For element-wise product, see the second version of std::transform.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With