简体   繁体   English

如果本征中两个张量相等,该如何比较?

[英]How to compare if two tensors are equal in Eigen?

There is this: 有这个:

https://codeyarns.com/2016/02/16/how-to-compare-eigen-matrices-for-equality/ https://codeyarns.com/2016/02/16/how-to-compare-eigen-matrices-for-equality/

But there is no isApprox for tensors. 但是,张量没有isApprox。

The following doesn't do what I want: 以下内容无法满足我的要求:

#include <Eigen/Core>
#include <unsupported/Eigen/CXX11/Tensor>
#include <array>
#include <iostream>

using namespace Eigen;
using namespace std;

int main()
{
// Create 2 matrices using tensors of rank 2
Eigen::Tensor<int, 2> a(2, 3);
Eigen::Tensor<int, 2>* b = &a;

cerr<<(*b==*b)<<endl;
}

because it does coordinate wise comparison and returns a tensor of the same dimension instead of a true/false vale. 因为它确实进行了明智的比较,并返回了相同维度的张量,而不是真/假虚假的vale。

How do I check if two tensors are identical? 如何检查两个张量是否相同? No isApprox for tensors. 张量没有isApprox。

I could write my own function, but I want to be able to use GPU power when available, and it seems like Eigen has built-in GPU support. 我可以编写自己的函数,但是我希望能够在可用的情况下使用GPU功能,而且似乎Eigen具有内置的GPU支持。

For an exact comparison of 2 tensors A and B, you can use the comparison operator followed by a boolean reduction: Tensor<bool, 0> eq = (A==B).all(); 要对2个张量A和B进行精确比较,可以使用比较运算符,然后进行布尔归约: Tensor<bool, 0> eq = (A==B).all();

This will return a tensor of rank 0 (ie a scalar) that contains a boolean value that's true iff each coefficient of A is equal to the corresponding coefficient of B. 这将返回等级0(即标量)的张量,其中包含一个布尔值,如果A的每个系数等于B的对应系数,则该布尔值为真。

There is no approximate comparison at the moment, although it wouldn't be difficult to add. 目前尚无近似比较,尽管添加起来并不困难。

You can always use a couple of Eigen::Map s to do the isApprox checks. 您始终可以使用几个Eigen::Map进行isApprox检查。

#include <iostream>
#include <unsupported/Eigen/CXX11/Tensor>

using namespace Eigen;

int main()
{

    Tensor<double, 3> t(2, 3, 4);
    Tensor<double, 3> r(2, 3, 4);

    t.setConstant(2.1);
    r.setConstant(2.1);
    t(1, 2, 3) = 2.2;
    std::cout << "Size: " << r.size() << "\n";
    std::cout << "t: " << t << "\n";
    std::cout << "r: " << r << "\n";

    Map<VectorXd> mt(t.data(), t.size());
    Map<VectorXd> mr(r.data(), r.size());

    std::cout << "Default isApprox: " << mt.isApprox(mr) << "\n";
    std::cout << "Coarse isApprox: " << mt.isApprox(mr, 0.11) << "\n";

    return 0;
}

PS/NB Regarding Eigen's built in GPU support... Last I checked it is fairly limited and with good reason. PS / NB 关于Eigen的内置GPU支持...最后,我检查了它的有限性并有充分的理由。 It is/was limited to fixed size matrices as dynamic allocation on a GPU is really something you want to avoid like the common cold (if not like the plague). 它被限制为固定大小的矩阵,因为在GPU上进行动态分配确实是您要避免的事情,例如普通感冒(如果不像瘟疫那样)。 I take it back. 我把它收回。 It looks like the Tensor module supports GPUs pretty well. 看起来Tensor模块很好地支持GPU。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM