# What kinds of uses of SymbolicC (introduced in Mathematica 8) do you foresee? Are there any neat examples already?

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!

-

## migrated from stackoverflow.comJun 9 '12 at 4:52

This question came from our site for professional and enthusiast programmers.

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

http://wolfram.com/xid/0isq3flowdud5n74bny881he6-0wsz71

http://wolfram.com/xid/0d195cdqgdtuw44ioyvm6og9pu-bv64sq

Also the webinars have other examples.

-
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. –  Sjoerd C. de Vries May 1 '11 at 7:47
@Sjoerd, good morning. –  Mr.Wizard May 1 '11 at 7:57
@Mr.Wizard Thanks. I was already gone to my drumming lessons. Admit you set your alarm just to type this message... –  Sjoerd C. de Vries May 1 '11 at 11:45
@Sjoerd I don't have that kind of determination. –  Mr.Wizard May 1 '11 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.

-
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. –  Jacob Akkerboom Mar 19 '14 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:

http://www.geuz.org/gl2ps/

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):

http://reference.wolfram.com/mathematica/SymbolicC/ref/ToCCodeString.html

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.

-
Good idea, but, indeed, it seems like lots of work. –  Sjoerd C. de Vries Jul 1 '11 at 16:09
@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. –  Szabolcs Jul 1 '11 at 16:13
This would be great. I would help but I'm not sure I would be useful. I constantly have problems with PDF graphics. –  s0rce Jun 10 '12 at 21:46
@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. –  Szabolcs Jun 13 '12 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.

-
@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. –  Leonid Shifrin Mar 27 '11 at 8:41
@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 –  Daniel Chisholm Mar 27 '11 at 22:05
@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). –  Sjoerd C. de Vries Mar 29 '11 at 22:18
@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. –  Sjoerd C. de Vries Apr 20 '11 at 18:05
@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. –  Leonid Shifrin Apr 20 '11 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.

-
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? –  Leo Fang Jul 31 '13 at 20:41