0

I have an Eigen::Matrix<bool,Dynamic,Dynamic> H (p,b) as result of the equivalent matlab operation H = (K*W)>0; (see this question for more details).

However, I have to compute the hamming distance between each row of H and a vector v. Since it's easy and fast to compute it between two std::bitset a and b through (a^b).count() I was wondering if it's possible to implement H = (K*W)>0; where H is Eigen::Array<std::bitset,Dynamic,1>.

As alternative, reading this question I have the feeling that I can use redux() for Eigen::Matrix<bool,Dynamic,Dynamic> H (p,b) but I cannot figure out how to define the reduction function

Community
  • 1
  • 1
justHelloWorld
  • 6,478
  • 8
  • 58
  • 138

1 Answers1

0

Using std::bitset could be difficult as you need to treat std::bitset as a customized scalar type and define scalar operations on it.

https://eigen.tuxfamily.org/dox/TopicCustomizingEigen.html#CustomScalarType

Hamming distance on the other hand is easy, although .count() here may be slower than std::bitset::count().

#include <iostream>
#include <Eigen/Eigen>
using namespace Eigen;

int main() {
  const int p = 5;
  const int b = 10;
  Eigen::Matrix<bool, Dynamic, Dynamic> H(p, b);
  Eigen::Matrix<bool, 1, Dynamic> V(b);
  H.setRandom();
  V.setRandom();

  std::cout << "H:\n" << H << std::endl;
  std::cout << "V:\n" << V << std::endl;
  std::cout << "distance(H, V):\n"
      << (H.array() != V.replicate(p, 1).array()).rowwise().count()
      << std::endl;
  return 0;
}
kangshiyin
  • 9,681
  • 1
  • 17
  • 29