使用“异或",“非"或“布尔"乘法(使用本征矩阵库) [英] bool multiplication with exclusive-or, not or (with Eigen Matrix Library)

查看:317
本文介绍了使用“异或",“非"或“布尔"乘法(使用本征矩阵库)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现汉明纠错码,为此,我需要获取布尔矢量(数据)并将其与布尔矩阵(汉明生成器矩阵)相乘,执行XOR操作(而不是看起来像什么)像OR一样是Eigen的默认bool行为).在这个简单的教程中找到了我正在做的一个例子: http://michael.dipperstein.com/hamming/

I'm trying to implement hamming error correcting codes, and to do this I need to take a bool Vector (the data) and multiply it with a bool Matrix(hamming generator matrix), performing XOR operations (instead of what looks like OR as Eigen's default bool behavior). An example of what I am doing is found in this simple tutorial: http://michael.dipperstein.com/hamming/

我不一定必须使用Eigen,因此,如果您有解决方案,请随时提出除Eigen之外的其他建议.

I don't necessarily have to use Eigen, so please feel free to suggest something other than Eigen if you have the solution.

例如,一些C ++代码可以编译,但不能以正确的方式正常工作:

So for example a bit of C++ code that compiles, but does not quite work the right way:

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

using namespace std;
using namespace Eigen;

typedef Eigen::Matrix<bool, 4, 7> Matrix4by7Bool;
typedef Eigen::Matrix<bool, 1, 4> Vector4Bool;
int main()
{
Matrix4by7Bool gm;
gm << 0,1,1,1,0,0,0,
      1,0,1,0,1,0,0,
      1,1,0,0,0,1,0,
      1,1,1,0,0,0,1;

Vector4Bool dm;
dm << 1,0,1,0;

cout << dm * gm;
}

当前导致:1 1 1 1 0 1 0
但我需要:1 0 1 1 0 1 0

currently results in: 1 1 1 1 0 1 0
but I need: 1 0 1 1 0 1 0

区别在于,默认行为是先相乘,然后再相乘.因为我需要XOR而不是OR,所以想知道用Eigen做到这一点的最佳方法是什么?

The difference is that the default behavior is to multiply and then OR each multiplication. Since I need XOR instead of OR, wondering what the best way to do this with Eigen is?

如果这没有道理,很乐意尝试详细阐述.

Happy to try and elaborate if this does not make sense.

顺便说一句,不确定是否重要,但是我正在使用G ++在MacBook Air上工作.今天刚刚下载了Eigen,所以它的问题可能是最新的(eigen3)

BTW, not sure if it matters but I'm working on a MacBook Air, using G++. Just downloaded Eigen today so its prob the newest available (eigen3)

谢谢你,
基思

Thank you,
Keith

更新:鉴于以下可接受的解决方案,我想重新发布正确的代码以供人们参考:

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

using namespace std;
using namespace Eigen;

typedef Eigen::Array<bool, 4, 7> Array4by7Bool;
typedef Eigen::Array<bool, 4, 1> Array1by4Bool;

struct logical_xor {
  bool operator() (bool a, bool b) const
  {
    return a != b;
  }
};

int main()
{
  Array4by7Bool gm;
  gm << 0,1,1,1,0,0,0,
        1,0,1,0,1,0,0,
        1,1,0,0,0,1,0,
        1,1,1,0,0,0,1;

  Array1by4Bool dm;
  dm << 1,0,1,0;

  cout << "result: "  <<  (gm.colwise() * dm).colwise().redux(logical_xor()) << endl;
}

推荐答案

您可以使用广播和部分归约来模拟matrix_vector乘积:

You can mimic a matrix_vector product using broadcasting and a partial reduction:

struct logical_xor { bool operator(bool a, bool b) { return a != b; }
result = (gm.array().colwise() * dm.transpose().array()).colwise().redux(logical_xor());

如果您将变量声明为Array,并且dm已经是列数组,那么它将简化为:

If you declare your variables as Array and dm is already a column array, then this simplifies to:

result = (gm.colwise() * dm).colwise().redux(logical_xor());

这篇关于使用“异或",“非"或“布尔"乘法(使用本征矩阵库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆