title: Compare Eigen matrices in Google Test or Google Mock
body: <p>I was wondering if there is a good way to test two <a href="http://eigen.tuxfamily.org/index.php?title=Main_Page" rel="noreferrer">Eigen</a> matrices for <em>approximate</em> equality using <a href="https://code.google.com/p/googletest/" rel="noreferrer">Google Test</a>, or <a href="https://code.google.com/p/googlemock/" rel="noreferrer">Google Mock</a>.</p>
  
  <p>Take the following test-case as a simplified example: I am multiplying two complex valued matrices <code>A</code>, and <code>B</code>, and expect a certain result <code>C_expect</code>. I calculate the numerical result <code>C_actual = A * B</code>, using Eigen. Now, I want to compare <code>C_expect</code>, and <code>C_actual</code>. Right now, the corresponding code looks like this:</p>
  
  <pre><code>#include &lt;complex&gt;
  #include &lt;Eigen/Dense&gt;
  #include &lt;gtest/gtest.h&gt;
  #include &lt;gmock/gmock.h&gt;
  
  typedef std::complex&lt;double&gt; Complex;
  typedef Eigen::Matrix2cd Matrix;
  
  TEST(Eigen, MatrixMultiplication) {
      Matrix A, B, C_expect, C_actual;
  
      A &lt;&lt; Complex(1, 1), Complex(2, 3),
           Complex(3, 2), Complex(4, 4);
      B &lt;&lt; Complex(4, 4), Complex(3, 2),
           Complex(2, 3), Complex(1, 1);
      C_expect &lt;&lt; Complex(-5, 20), Complex(0, 10),
                  Complex(0, 40), Complex(5, 20);
  
      C_actual = A * B;
  
      // !!! These are the lines that bother me.
      for (int j = 0; j &lt; C_actual.cols(); ++j) {
          for (int i = 0; i &lt; C_actual.rows(); ++i) {
              EXPECT_NEAR(C_expect(i, j).real(), C_actual(i, j).real(), 1e-7)
                  &lt;&lt; "Re(" &lt;&lt; i &lt;&lt; "," &lt;&lt; j &lt;&lt; ")";
              EXPECT_NEAR(C_expect(i, j).imag(), C_actual(i, j).imag(), 1e-7)
                  &lt;&lt; "Im(" &lt;&lt; i &lt;&lt; "," &lt;&lt; j &lt;&lt; ")";
          }
      }
  }
  </code></pre>
  
  <p>What's wrong with this? Well, I have to manually iterate through all indices of the matrix, and then compare the real-part and imaginary-part individually. I would much prefer something along the lines of Google Mock's <code>ElementsAreArray</code> matcher. E.g.</p>
  
  <pre><code>EXPECT_THAT(C_actual, ElementsAreArray(C_expect));
  // or
  EXPECT_THAT(C_actual, Pointwise(MyComplexNear(1e-7), C_expect));
  </code></pre>
  
  <p>Unfortunately, the built-in capabilities of Google Mock only seem to work on 1-dimensional C-style, or STL-type containers. Furthermore, I need an <em>approximate</em> comparison for the complex values of my matrix.</p>
  
  <p><strong>My question</strong>: Do you know if (and how) it is possible to teach Google Mock to iterate over multiple dimensions, and compare complex floating point numbers to approximate equality?</p>
  
  <p>Please note, that I cannot just handle the data-pointers as C-style arrays, because the <a href="http://eigen.tuxfamily.org/dox/group__TopicStorageOrders.html" rel="noreferrer">storage layout</a> might differ between <code>C_expect</code>, and <code>C_actual</code>. Also, in reality, the matrices are larger than just 2x2 matrices. I.e. some sort of loop is definitely necessary.</p>