特征:将向量的每个元素与常数进行比较

发布于 2025-02-01 08:13:53 字数 319 浏览 4 评论 0原文

有没有办法将向量的每个元素与常数进行比较?到目前为止,我正在比较2D矢量eigen :: vector2d与常数double bolerance这样:

    if (x(0) > tolerance && x(1) > tolerance)
    {
     ...
    }

我找到了功能isapprox(),但是它没有用。关于如何做的是更好或推荐的方法吗?

is there a way how to compare each element of a vector with a constant? So far, I am comparing 2D vector Eigen::Vector2d with a constant double tolerance like this:

    if (x(0) > tolerance && x(1) > tolerance)
    {
     ...
    }

I have found the function isApprox() but it did not worked somehow. Is there is a nicer or recommended way on how to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千纸鹤带着心事 2025-02-08 08:13:53

一种方法是使用array vector类的方法。像这样:

#include <Eigen/Dense>
#include <iostream>

int main(int argc, char * argv[]) {
    Eigen::Vector2d A{ 7.5, 8.2 };
    std::cout << A << '\n';
    auto res = A.array() >= 8.0;
    std::cout << res << '\n';
    if (res.all()) {
        std::cout << "True" << '\n';
    }
    else {
        std::cout << "False" << '\n';
    }

    A(0) = 10.2;
    auto res2 = A.array() >= 8.0;
    std::cout << res2 << '\n';
    if (res2.all()) {
        std::cout << "True" << '\n';
    }
    else {
        std::cout << "False" << '\n';
    }

    return 0;
}

在这种情况下,resres2cashingbinaryop,其中包含a中每个元素的布尔值。使用所有在两者都是true时查找。

One way to do this is to use the array method of the Vector class. Like this:

#include <Eigen/Dense>
#include <iostream>

int main(int argc, char * argv[]) {
    Eigen::Vector2d A{ 7.5, 8.2 };
    std::cout << A << '\n';
    auto res = A.array() >= 8.0;
    std::cout << res << '\n';
    if (res.all()) {
        std::cout << "True" << '\n';
    }
    else {
        std::cout << "False" << '\n';
    }

    A(0) = 10.2;
    auto res2 = A.array() >= 8.0;
    std::cout << res2 << '\n';
    if (res2.all()) {
        std::cout << "True" << '\n';
    }
    else {
        std::cout << "False" << '\n';
    }

    return 0;
}

In this case res and res2 are CwiseBinaryOp which contains booleans for each element in A. Use all to find when both are True.

凉薄对峙 2025-02-08 08:13:53

我觉得这是一个很好的调用是写一个简单的功能:

bool is_componentwise_greater_than(
    Eigen::Ref<Eigen::VectorXd const> const &vector, double lower_bound) {
  for (auto value : vector) {
    if (value <= lower_bound)
      return false;
  }
  return true;
}

与@matt的解决方案相比,这种方式的缺点是,对于更复杂的用例,使用特征表达式可以更具性能(不知道,如果,如果这适用于这里)。

这种解决方案的(我认为巨大的)优势是,您
可以准确地看到它的用法所做的。

当然,您还可以将Matts解决方案打包到一个恰当地命名的功能中,以获得此优势。另一个优点是,通过我提供的功能,您确切地知道它的作用,而不必怀疑,是否将自动与特征类型一起使用会咬您。我想不会,马特可能确切地知道为什么。但是我(和你?)不这样做,因此不想依靠它。

I feel like a good call on this one is to write a simple function:

bool is_componentwise_greater_than(
    Eigen::Ref<Eigen::VectorXd const> const &vector, double lower_bound) {
  for (auto value : vector) {
    if (value <= lower_bound)
      return false;
  }
  return true;
}

The drawback of this way compared to the solution by @Matt is, that for more complicated use cases, using an Eigen expression can be more performant (no idea, if this applies here).

The (in my opinion huge) advantage of such an solution is, that you
can see exactly what it does from its usage.

Of course you can also pack Matts solution in an aptly named function to get this advantage. Another advantage is, that with the function I provided you know exactly what it does and never have to wonder, whether using auto with an Eigen type could bite you. I guess it won't and Matt probably knows exactly why. But I (and you?) do not and therefore wouldn't want to rely on it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文