Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Is there any way that I can utilize Mathematica's matrix multiplication in a C++ program? I'm making a 3D graphics engine (for class) in C++ and I would really like to use Mathematica for all of my math.

Thank you.

I have Mathematica 8, and Visual Studio 2010 that I am using to develop with.

share|improve this question
4  
I wouldn't. The vast majority of the 3D matrix math you need can be easily coded in c++, see flipcode for examples. But, if you do not want to code it yourself, use something like uBLAS or Blitz++. Blitz has the advantage of giving you an interface that is like matrix math as you'd do it with pen and paper. –  rcollyer Oct 31 '12 at 5:29
    
I do realize I can code it myself, and very easily. I thought it would be "fun" to do it with mathematica though. However, if it is unnecessary work then I will not. –  kjmccarx Oct 31 '12 at 5:58
1  
I'd say, use Mathematica for prototyping your algorithms, nothing more. You'll want to use a native C++ library if you're multiplying matrices in C++; don't use a hammer for turning screws. –  J. M. Oct 31 '12 at 10:29

1 Answer 1

up vote 3 down vote accepted

Yes, you can. This would involve transferring the matrix to Mathematica, invoking the multiplication function, then transferring the result back.

Doings this for the kinds of small (4 by 4 and 3 by 3) matrices that come up in your application area is going to be

  • slower than either using pure Mathematica or pure C++ (due to the transfer)

  • take more coding and learning effort than either writing your own matrix multiplication in C++ or using a C++ library

Therefore I would strongly discourage doing this. Matrix math is simple and there are lots of excellent libraries for it in all low-level languages. Save Mathematica for calculations that are not easily done in C++.

If you still want to do it, you need to learn about the MathLink.

Please see here and in particular here on how to call Mathematica from C (or C++).

I'd recommend you familiarize yourself with how MathLink works first by playing with it in pure Mathematica code. It's going to be easier to write the C version once you're comfortable with how MathLink the protocol works.

share|improve this answer
1  
Welcome back Szabolcs! We've missed you. :-) –  Mr.Wizard Nov 10 '12 at 8:45
    
It's also worth mentioning that Mathematica won't do anything with matrices to multiply besides passing them to Intel MKL (software.intel.com/en-us/intel-mkl), a high speed implementation of the BLAS (Basic Linear Algebra System) that most software uses for matrix stuff. So it wouldn't gain you very much that you couldn't do by calling MKL yourself. –  librik Nov 13 '12 at 5:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.