This forum has been archived. All content is frozen. Please use KDE Discuss instead.

A function with MatrixBase and DiagonalWrapper inputs

Tags: None
(comma "," separated)
chourouch
Registered Member
Posts
1
Karma
0
OS
Hello,

to make writing unit tests easier, I'd like a function which checks whether two matrices (say data and ref) are equal or not.

It looked simple at first, just do

Code: Select all
((data.array() - ref.array()).abs() < eps).all()


However, I could not get a working function which does that, with both Matrix and DiagonalWrapper as inputs.

For instance, the following template would not match a DiagonalWrapper input
Code: Select all
template <typename Derived0, typename Derived1>
bool MatrixBaseIsEqual(
  const Eigen::MatrixBase<Derived0> &data,
  const Eigen::MatrixBase<Derived1> &ref,
  float eps)
{
      return ((data.array() - ref.array()).abs() < eps).all();
}


and changing MatrixBase for EigenBase make the array() method vanish.

I think I could specialize the function for these combinations, but I guess there is a better, more generic way.

Here follow some attempts of my.

Code: Select all
#include <iostream>
#include <Eigen/Core>

template <typename Derived0, typename Derived1>
bool DenseBaseIsEqual(
  const Eigen::DenseBase<Derived0> &data,
  const Eigen::DenseBase<Derived1> &ref,
  float eps)
{
  return ((data.array() - ref.array()).abs() < eps).all();
}

template <typename Derived0, typename Derived1>
bool EigenBaseIsEqual(
  const Eigen::EigenBase<Derived0> &data,
  const Eigen::EigenBase<Derived1> &ref,
  float eps)
{
  Derived0 dataArray = data;
  Derived0 refArray = ref;
  return ((dataArray.array() - refArray.array()).abs() < eps).all();
  //((data - ref).cwise().abs().cwise() < eps).all();
}

template <typename Derived0, typename Derived1>
bool MatrixBaseIsEqual(
  const Eigen::MatrixBase<Derived0> &data,
  const Eigen::MatrixBase<Derived1> &ref,
  float eps)
{
      return ((data.array() - ref.array()).abs() < eps).all();
}

int main()
{
  Eigen::Vector4f v1248;
  v1248 << 1, 2 , 4, 8;
  Eigen::Matrix4f m1248;
  m1248 = v1248.asDiagonal();
  Eigen::Array44f a1248;
  a1248 = m1248.array();
  //Eigen::Matrix4f m1111 = Eigen::Matrix4f::Zero();
  // works
  MatrixBaseIsEqual(m1248, m1248, 1e-6);

  // does not compile: error: no matching function for call to ‘MatrixBaseIsEqual(Eigen::Matrix4f&, const Eigen::DiagonalWrapper<const Eigen::Matrix<float, 4, 1> >, double)’
  //MatrixBaseIsEqual(m1248, v1248.asDiagonal(), 1e-6);

  // does not compile: error: no matching function for call to ‘MatrixBaseIsEqual(const Eigen::DiagonalWrapper<const Eigen::Matrix<float, 4, 1> >, const Eigen::DiagonalWrapper<const Eigen::Matrix<float, 4, 1> >, double)’
  //MatrixBaseIsEqual(v1248.asDiagonal(), v1248.asDiagonal(), 1e-6);

  // does not compile: error: ‘const class Eigen::DenseBase<Eigen::Matrix<float, 4, 4> >’ has no member named ‘array’
  //DenseBaseIsEqual(m1248, m1248, 1e-6);

  // does not compile:  error: no matching function for call to ‘DenseBaseIsEqual(Eigen::Matrix4f&, const Eigen::DiagonalWrapper<const Eigen::Matrix<float, 4, 1> >, double)’
  //DenseBaseIsEqual(m1248, v1248.asDiagonal(), 1e-6);

  // does not compile: error: no matching function for call to ‘DenseBaseIsEqual(const Eigen::DiagonalWrapper<const Eigen::Matrix<float, 4, 1> >, const Eigen::DiagonalWrapper<const Eigen::Matrix<float, 4, 1> >, double)’
  //DenseBaseIsEqual(v1248.asDiagonal(), v1248.asDiagonal(), 1e-6);

  // works
  EigenBaseIsEqual(m1248, m1248, 1e-6);
  EigenBaseIsEqual(a1248, a1248, 1e-6);
  EigenBaseIsEqual(m1248, v1248.asDiagonal(), 1e-6); // I do not understand why it compiles
  // does not compile: error: conversion from ‘const Eigen::EigenBase<Eigen::DiagonalWrapper<const Eigen::Matrix<float, 4, 1> > >’ to non-scalar type ‘Eigen::DiagonalWrapper<const Eigen::Matrix<float, 4, 1> >’ requested
  //EigenBaseIsEqual(v1248.asDiagonal(), v1248.asDiagonal(), 1e-6);

  return 0;
}


I'd gladly welcome any hint, as well as some advice on a good template meta-programming book. As you might have guessed, I could make use of it ;)

Cheers.


Bookmarks



Who is online

Registered users: Bing [Bot], claydoh, Google [Bot], rblackwell, Yahoo [Bot]