I would like to take the element-wise max of two vectors/matrices in Eigen. So far, I've written this code:
template <typename S, typename T>
auto elemwise_max(const S & A, const T & B) {
return (A.array() > B.array()).select(A, B);
}
Is this correct, or is this there a better way of doing this?
For the positive part (ie. max(A, 0)
), I'm not sure how to proceed. Do I need to create two methods?
template <typename S>
auto positive_part_matrix(const S & A) {
auto zeros = S::Zero(A.rows(), A.cols());
return elemwise_max(A, zeros);
}
template <typename S>
auto positive_part_vec(const S & A) {
auto zeros = S::Zero(A.size());
return elemwise_max(A, zeros);
}
Ideally both of the above would just be called positive_part
.