Reputation: 1090
How can I map a function to every element of a vector in uBLAS (like Map[] in Mathematica)?
For example; I want to take the sin()
of all the elements of a uBLAS vector. Is there an optimized way in Boost, GSL, or any other numerical libraries to do this instead of simply looping through the elements of the vector?
Also, how would I perform other advanced operations on the uBLAS vectors such as rotating, deleting duplicates, or padding with zeros, etc?
Upvotes: 2
Views: 953
Reputation: 3873
Your vector (according to this) supports normal vector operations, just use the standard algorithms. In your case, here are a few of help (all inside <algorithm>
):
std::transform
with sinef
from <cmath>
std::rotate
.std::unique
after a sort, deleting the unused elements.Upvotes: 1
Reputation: 88711
The closest equivalent to map is std::transform
#include <algorithm>
#include <functional>
#include <vector>
#include <cmath>
int main() {
std::vector<float> values;
values.push_back(0.5f);
values.push_back(1.0f);
std::transform(values.begin(), values.end(), values.begin(), std::ptr_fun(sinf));
}
And for de-duplication:
#include <algorithm>
#include <vector>
#include <iostream>
#include <iterator>
int main() {
std::vector<int> duplicates;
duplicates.push_back(1);
duplicates.push_back(3);
duplicates.push_back(5);
duplicates.push_back(1);
std::sort(duplicates.begin(), duplicates.end());
duplicates.erase(std::unique(duplicates.begin(), duplicates.end()), duplicates.end());
std::copy(duplicates.begin(), duplicates.end(), std::ostream_iterator<int>(std::cout, "\n"));
}
(I believe ublas vector has begin()
and end()
or similar)
Upvotes: 1