Search code examples
iosobjective-copencveigenaccelerate-framework

NSLog matrix_float4x4 or simd::float4x4


Are there no Apple provided debugging tools of the Accelerate matrices similar to 'OpenCV' or 'Eigen'?

Example code of how Eigen and OpenCV print matrices

Eigen::Matrix4f matrix;
std::cout << matrix << std::endl;

Output of a 4x4 matrix

-0.483662   0.86859   0.10781   51.8456
 0.865028  0.455597  0.210137   29.6781
 0.133405  0.194894 -0.971709    192.69
        0         0         0         1

A rough approximation of what I want, but not nearly general enough.

Here is my quick-and-dirty version. I might eventually make it something more thorough where it handles C or C++ versions, but really I am hoping Apple provides it and I just haven't found the documentation.

void logSIMD(const simd::float4x4 &matrix) 
{
   std::stringstream output;
   int columnCount = sizeof(matrix.columns) / sizeof(matrix.columns[0]);
   for (int column = 0; column < columnCount; column++) {
      int rowCount = sizeof(matrix.columns[column]) / sizeof(matrix.columns[column][0]);
      for (int row = 0; row < rowCount; row++) {
         output << std::setfill(' ') << std::setw(9) << matrix.columns[column][row];
         output << ' ';
      }
      output << std::endl;
   }
   output << std::endl;
   NSLog(@"%s", output.str().c_str());
}

Has anyone got a general solution to the C/C++ printing of the various sized matrices and vectors within Accelerate or has Apple provided functions that I just haven't found?


Solution

  • There are no such functions provided by <simd/simd.h>. There are some good reasons for this, most importantly that people are pretty opinionated about how they want to format their vectors and matrices, so no provided format will be what more than a tiny fraction of users actually want, and it's straight-forward enough to write your own print routines that do exactly what you want. A fully generic formatter function that can handle arbitrary user-defined formats would be more reasonable, but would be extremely heavy-weight compared to the rest of the <simd/simd.h> interfaces, so it doesn't seem like a great fit either.

    That said, if you think that these would be useful as API, and that you can make a convincing case for why your preferred format is the right one to standardize, I would encourage you to report a bug to Apple requesting the feature.