Around 2004, Mathworks discontinued its support for C++ programs to directly call into the internals of Matlab via a C++ class called mwArray. You can still find websites telling people to use mwArray; seeing #include "matlab.hpp" is a clear sign of this problem. Instead, the Matlab Engine was intended to be the supported interface, via mxArray, which does not allow any direct computations. mwArray was reimplemented for the Matlab Compiler interface, which doesn't work with most of the Matlab code that I tried it on.

In 2010, this became my problem. As part of maintaining a large, legacy C++ application, I needed the to continue to use the original functionality of mwArray, but the only sensible way to do it was through the Matlab Engine. So I wrote a fairly complex C++ wrapper class that restored the functionality I needed via mxArray and the Matlab Engine. You can download the mxWrapper class here, or you can get it from the Mathworks File Exchange. Eventually, I modified the legacy application so that most of the challenging math was occuring inside Matlab, which was called via mxWrapper, and the legacy code focused on data acquisition, low level drivers, proprietary file format support, etc.

I included an example MFC program that demonstrates how mxWrapper works. It could still be useful for C++ programs that need to call the full functionality of Matlab, which isn't available through the Matlab Compiler. See screenshot to the side. If you replaced the Windows-specific multi-threading primitives (CCriticalSection), I'm pretty sure you could get mxWrapper to work under any Matlab platform (Apple, Unix, etc.).

For fun I wrote a Sudoku solver in C++. It can solve a decent number of Sudoku puzzles, but not all of them because the general Sudoku problem is NP-complete. Sudoku turns out to be equivalent to solving an exact cover problem, which Donald Knuth wrote about. I really can't compare to Knuth's brilliant algorithms, so my code is just a toy. I came up with the algorithms in the code from my own intuition.

Download the Sudoku solver here. It should compile with any recent C++ compiler. Some example problems are given in this input file, and the solutions are in in this output file.

This is a relatively easy Sudoku problem because recursive guessing isn't required to solve it:

_______________________ | 4 8 0 | 0 0 6 | 9 0 2 | | 0 0 2 | 0 0 8 | 0 0 1 | | 9 0 0 | 3 7 0 | 0 6 0 | ------------------------ | 8 4 0 | 0 1 0 | 2 0 0 | | 0 0 3 | 7 0 4 | 1 0 0 | | 0 0 1 | 0 6 0 | 0 4 9 | ------------------------ | 0 2 0 | 0 8 5 | 0 0 7 | | 7 0 0 | 9 0 0 | 6 0 0 | | 6 0 9 | 2 0 0 | 0 1 8 | -----------------------

The one and only solution to this problem is:

_______________________ | 4 8 7 | 1 5 6 | 9 3 2 | | 3 6 2 | 4 9 8 | 7 5 1 | | 9 1 5 | 3 7 2 | 8 6 4 | ------------------------ | 8 4 6 | 5 1 9 | 2 7 3 | | 5 9 3 | 7 2 4 | 1 8 6 | | 2 7 1 | 8 6 3 | 5 4 9 | ------------------------ | 1 2 4 | 6 8 5 | 3 9 7 | | 7 3 8 | 9 4 1 | 6 2 5 | | 6 5 9 | 2 3 7 | 4 1 8 | -----------------------

You can deduce the solution by placing 2s and 4s by negative constraints (only 1 per row, column, or box), and then applying positive constraints (e.g., there must be a 7 in every row, column, or box). My code solves this puzzle by alternating between these ad hoc algorithms (negative and positive constraints).

Coming soon.