Element-wise max and positive part in Eigen

user674155

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.

ggael

The answer is there.

You can either move to the "array" world and use max:

A.array().max(B.array())

or use cwiseMax:

A.cwiseMax(B)

In both cases, B can be either a Matrix or a scalar:

A = A.cwiseMax(0);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related