This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Compare VectorXd and VectorXd::Constant

Tags: None
(comma "," separated)
Horus
Registered Member
Posts
296
Karma
0
OS
Hello,

I want to know if at least one component of a VectorXd is greater than 1:
Code: Select all
(offset > Eigen::VectorXd::Constant(dim, 1)).any()


offset is a const Eigen::VectorXd&.

but this gives the compiler error:

Fehler: no match for »operator>« (operand types are »const VectorXd {aka const Eigen::Matrix<double, -1, 1>}« and »const ConstantReturnType {aka const Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, -1, 1> >}«)
assertion(not (offset > Eigen::VectorXd::Constant(dim, 1)).any());


It's probably that Constant returns an expression. But what is way for doing comparisions like that one? I also tried (offset > 1) but there is no operator> for int.

Thanks!
Florian
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
It's easier to go to the "array" world:

(offset.array() > 1).any()
Horus
Registered Member
Posts
296
Karma
0
OS
Thanks, that works fine:
Code: Select all
(A.array() > B.array()).any()


We used to have a function oneGreater, that I'm substituting with the code above. This function, however actually compared A-B > e, with a floating point precision e.

Is there a way to set a floating point precision for comparision in Eigen?

Thanks!
Horus
Registered Member
Posts
296
Karma
0
OS
Ok, I started to reimplement the oneGreater function

Code: Select all
#include <Eigen/Dense>

template<class Derived>
bool oneGreater(const Eigen::EigenBase<Derived> & A,
                const Eigen::EigenBase<Derived> & B,
                double tolerance = math::NUMERICAL_ZERO_DIFFERENCE)
{
  for (Eigen::Index i = 0; i < A.size(); i++) {
    if ( *(A.data()+i) - *(B.data()+i) > tolerance)
      return true;
  }
}


but where is Eigen::Index defined. The documentation says, one should use it for indexing, and it is the return type of size(). However, even grepping through the Eigen code case was not enlighting.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
You can replace your loop by:

return ((A-B).array()>tolerance).all();

Eigen::Index is in 3.3 only. (file src/Core/util/Meta.h line 33)


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Sogou [Bot]