Mathematica 8 introduced SymbolicC, a set of Mathematica functions that capture C language constructions in the familiar Mathematica tree-like fashion. Additionally, there are functions to write SymbolicC trees as straight C code to a file. The tutorial clearly explains the functions, but -as far as I know- it doesn't provide any examples of useful applications of this capability.

It sounds like it should be tremendously useful, but I have a hard time coming up with some creative uses of my own. A couple of years ago, I used Mathematica to 'flatten out' a recursive algorithm I had written in C to gain some speed. Mathematica wrote the whole program for me pasting C phrases together I had prepared in strings. SymbolicC would have been much neater, though not really necessary.

Yesterday, Leonid Shifrin came up with a very nice application of symbolicC to make an alternative of CForm that doesn't make calls to the auxiliary function Power(). That's a good start.

I'd like to ask the creative minds on StackOverflow to come up with more ideas and/or actual uses of SymbolicC. I'll accept the most upvoted answer.

EDIT Mar 30

The response to the question is a bit scant so far. So, we have this mma extension with more than 60 functions added and virtually nobody has an idea of how to use them? Come on guys, show that you're creative!


7 Answers 7


Check the CUDALink/OpenCLLink documentation, they have some creative applications




Also the webinars have other examples.

  • 3
    $\begingroup$ A good find! I feel it's remarkable that the SymbolicC guide itself doesn't have examples like this and that you have to turn to the CUDA and OpenCL docs to find some. $\endgroup$ Commented May 1, 2011 at 7:47
  • $\begingroup$ @Sjoerd, good morning. $\endgroup$
    – Mr.Wizard
    Commented May 1, 2011 at 7:57
  • 1
    $\begingroup$ @Mr.Wizard Thanks. I was already gone to my drumming lessons. Admit you set your alarm just to type this message... $\endgroup$ Commented May 1, 2011 at 11:45
  • 1
    $\begingroup$ @Sjoerd I don't have that kind of determination. $\endgroup$
    – Mr.Wizard
    Commented May 1, 2011 at 11:48

One use case that I've been exploring has been to replace Mathematica's own Compile functionality with (potentially much faster) generated C which can be compiled using CCompilerDriver` and linked back in using LibraryFunctionLoad. The built-in Compile is a very nice piece of functionality, but if you examine the generated C output for many programs--especially ones that rely on iterating over multi-dimensional arrays--you'll see that there's a lot of room for improvement because Mathematica generates code which is very generic. This makes sense given how Compile is intended to be a general purpose tool, but you can do better.

One key thing is that, in order to set an element a multi-dimensional array to a new value, the generated code will use a WolframLibraryData object which contains a pointer to a function MTensor_setReal. This mechanism is functionally identical to a virtual function call in C++, and it's not the sort of thing you really want in a tight loop because your compiler will almost certainly have no hope of inlining the call. However, it's possible, in C code, to get a pointer to the elements of a Mathematica tensor, which will be laid out exactly as you'd expect: it's a bunch of machine doubles (or integers, where appropriate) in row-major order. You can blaze through that, and either easily insert the code that you want inline where you want it, or rely on the compiler to inline static functions that you generate in the same file.

  • $\begingroup$ This corresponds very much to my own experience. I have quite often started translating my own "compilable & procedural mma code" to SymbolicC. The lack of good tools to do this was a bit frustrating to me and in the end most of the time I just wrote C code instead. $\endgroup$ Commented Mar 19, 2014 at 9:23

Yves Papegay has developed SymbolicC in order to automate the conversion of his models to C. This is used by Airbus.

For more information see his presentations in the Wolfram Library Archive http://library.wolfram.com/infocenter/search/?search_results=1&search_person_id=6281 especially this one http://library.wolfram.com/infocenter/Conferences/7477/

It can give ideas, but there are still many gaps to fill !

Also take a look at the installation folders Mathematica\8.0\AddOns\Applications\CCodeGenerator and Mathematica\8.0\AddOns\Applications\SymbolicC for examples.

Edit: I think that using SymbolicC under the hood with Compile and Mathematica code combined with my answer to Compiling more functions that don't call MainEvaluate you can compile a lot of things to C without explicitely using the SymbolicC package.


This is a very very specific example.

There's something I wanted to do for a while but I didn't have time yet.

Exporting 3D graphics into PDF produces lots and lots and lots of triangles (for the gradients), which aren't joined properly. The result is that rendering time in a PDF reader is very long, the PDF size is very large, and the whole result just looks ugly on screen because the triangle edges are visible. This is also the case when using gradients in 2D plots, or using things like RegionPlot.

There's a C library that converts OpenGL calls to a vector format output, supports PDF, and uses gradients properly in PDF instead of breaking everything down to tiny little triangles:


There's an example in the Mathematica docs on how to convert a Graphics3D expression to OpenGL calls (but it's incomplete - check under Applications):


[ Update: Here's the link for the v8 documentation. The examples have been removed in v9. ]

It'd be nice to join the two and have good looking high quality PDF output for 3D graphics.

This is a lot of work though because 2D objects and text need to be handled properly too (GL2PS does have support for this). But I think it's possible to do it if one has a few days to dedicate to the task.

  • $\begingroup$ Good idea, but, indeed, it seems like lots of work. $\endgroup$ Commented Jul 1, 2011 at 16:09
  • $\begingroup$ @Sjoerd, I think it would only take a couple of days. No need to re-implement every single thing Graphics3D can do to have something useful. $\endgroup$
    – Szabolcs
    Commented Jul 1, 2011 at 16:13
  • $\begingroup$ This would be great. I would help but I'm not sure I would be useful. I constantly have problems with PDF graphics. $\endgroup$
    – s0rce
    Commented Jun 10, 2012 at 21:46
  • $\begingroup$ @s0rce I think Graphics3D are a bit more complicated than I imagined when I wrote this, but this should still clearly be doable (in a week or so?), even if a bit of work. Unfortunately I don't really have time for it now. $\endgroup$
    – Szabolcs
    Commented Jun 13, 2012 at 7:11

Actually Leonid didn't use SymbolicC in his answer yesterday, he mentioned it as another more powerful way to potentially deal with my question.

I would assume that at some stage of Compile[...,CompilationTarget->"C"] that Mathematica builds an internal expression using SymbolicC. It would be nice to be able to "intercept" it at this stage. To solve my problem from yesterday it would have been brilliant to be able to get the "core" C code that Mathematica generates with Compile. But when Compile produces source code for a C function it of course also adds function definitions, setup code and exit code to make it work as a Mathematica-compliant DLL. If I could programmatically strip that out I would be left with some beautiful (if only machine readable! ;-) highly optimized C code.

For what it's worth even though I was "complaining" about what CForm was giving me I really must say that the C code that Mathematica's Compile[] generates is good. Common subexpression elimination of course, but also it implements raising a real quantity to an integer power as inlined loop of powers-of-2 successive multiplication C code.

  • $\begingroup$ @Daniel I actually added another solution to your problem using symbolic C, later, in response to @Sjoerd's comment to my solution. I marked it Edit 2. You probably only saw the first part of the answer. $\endgroup$ Commented Mar 27, 2011 at 8:41
  • $\begingroup$ @Leonid thanks; you're right I did miss "Edit 2", now I'm going to go back and have a thorough look at it. I ended up using your first "Edit" quite successfully $\endgroup$ Commented Mar 27, 2011 at 22:05
  • $\begingroup$ @Daniel You can already get SymbolicC code from compile. SymbolicCGenerate does this. But what would you do with this code? I'm looking for neat new ideas, and I haven't seen any (except Leonid's). $\endgroup$ Commented Mar 29, 2011 at 22:18
  • $\begingroup$ @Leonid It looks like my symbolic-C questions doesn't yield many new ideas. Could you repost your earlier response here so that I can close this question by selecting your response? IMHO it is the best one on this topic. $\endgroup$ Commented Apr 20, 2011 at 18:05
  • 1
    $\begingroup$ @Sjoerd Are you sure you don't want to wait some more? These are new things, I'm sure people will eventually come up with some great ideas. If you close it, no one will be able to add answers (if I understand correctly). I think, this is a very important direction, and keeping your question as a repository, like it happens with "What is in your Mathematica toolbag", would be great. I have a few ideas myself, but they are too raw at the moment. I think, accumulating such answers just needs time, as in the "toolbag" case, and it may take the longest to get the first few answers. $\endgroup$ Commented Apr 20, 2011 at 19:55

Further possibilities that I can think of might be


SymbolicC is incredibly effective for writing boilerplate code, or really anything that is largely repetitive. You can use the powerful and intuitive pattern matching and manipulation functions built-in to Mathematica to build up a computation tree using SymbolicC symbols, then transform the tree into a fully-functional C file by passing the tree to ToCCodeString.

  • $\begingroup$ just found this thread and it seems that you're suggesting using SymbolicC to establish a workflow. Is it possible for you to give a concrete yet simple example on how to do this? $\endgroup$
    – Leo Fang
    Commented Jul 31, 2013 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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