布尔乘法与 exclusive-or, not or (with Eigen Matrix Library)
bool multiplication with exclusive-or, not or (with Eigen Matrix Library)
我正在尝试实现汉明纠错码,为此,我需要获取一个布尔向量(数据)并将其与布尔矩阵(汉明生成器矩阵)相乘,执行异或运算(而不是看起来像 OR 作为特征的默认布尔行为)。在这个简单的教程中找到我正在做的事情的一个例子:http://michael.dipperstein.com/hamming/
我不一定必须使用本征,所以如果您有解决方案,请随时提出本征以外的建议。
例如,一些C++代码可以编译,但不能完全以正确的方式工作:
#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
但我需要:
不同之处在于,默认行为是乘法,然后是 OR 每次乘法。由于我需要 XOR 而不是 OR,想知道使用本征执行此操作的最佳方法是什么?
如果这没有意义,很乐意尝试详细说明。
顺便说一句,不确定这是否重要,但我正在使用 G++ 在 MacBook Air 上工作。今天刚刚下载了 Eigen,所以它可能是最新的可用 (eigen3)
谢谢
基思
更新:鉴于下面接受的解决方案,我想重新发布正确的代码作为人们的参考:
#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产品:
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 已经是列数组,则简化为:
result = (gm.colwise() * dm).colwise().redux(logical_xor());
你可以这样做。以下是概念验证。它包含使示例编译并提供所需结果所需的最小值。
它可能相当脆弱,而且它是从我周围的其他代码中分离出来的,所以它不会因为漂亮或惯用而获得任何分数。
基本思想是创建自己的bool
类型,其中加法是 XOR,并给出您需要的相关运算符和NumTraits
。
#include <Eigen/Dense>
#include <iostream>
using namespace std;
using namespace Eigen;
class mybool {
public:
bool b;
mybool() { b = false; }
mybool(bool b) : b(b) {}
mybool(int a) : b(a!=0) {}
mybool operator* (const mybool m) const {return m.b & b;}
mybool operator+ (const mybool m) const {return m.b ^ b;}
mybool operator+= (const mybool m) {b ^= m.b; return b;}
friend ostream& operator<<(ostream& os, const mybool& m);
};
ostream& operator<<(ostream& os, const mybool& m) { os << m.b; return os; }
namespace Eigen {
template<> struct NumTraits<mybool>
{
typedef int Real;
typedef mybool Nested;
enum {
IsComplex = 0,
IsInteger = 1,
IsSigned = 0,
RequireInitialization = 0,
ReadCost = 1,
AddCost = 2,
MulCost = 2
};
static Real epsilon() { return 1; }
};
}
typedef Matrix<mybool, 4, 7> Matrix4by7Bool;
typedef Matrix<mybool, 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;
}
相关文章:
- Eigen如何在容器循环中干净地附加矩阵
- 当在带有Eigen的C++中使用GDB时,我如何才能看到更多的大矩阵
- Problems with std::cin.fail()
- 使用Pybind11向Python公开Eigen::张量
- 如何在没有数据拷贝的情况下从指针创建一个Eigen VectorXd对象
- Eigen:调用lpNorm()的正确方法
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 如何调整 std::vector of Eigen::MatrixXd 的大小
- 如何将 Eigen::Ref 与 pybind11 一起使用?
- Eigen c++ 三角形来自
- 这对"With a stackless coroutine, only the top-level routine may be suspended."意味着什么
- Boost.TEST with CLion: "Test framework quit unexpectedly"
- 如何使用 Eigen::Tensor::convolve with Multiple Kernel?
- Compile Eigen + Intel MKL with CMake
- Eigen on ARM Cortex M3 with armcc
- Eigen norm() with Boost.Units
- 布尔乘法与 exclusive-or, not or (with Eigen Matrix Library)
- Using std::map with Eigen 3
- using eigen with android ndk
- Using Eigen::Vector3f with Opengl ES 2.0 VBO