Hot answers tagged code-generation
20
Leonid provides a nice method for doing this within "pure functions" but I think it should be pointed out that the common method for doing this is pattern matching.
I argue that destructuring is the foundational use of pattern matching in Mathematica.
Every replacement pattern, be it an explicit rule (:>, ->) or part of a definition (:=, =), that ...
10
You can use macros / code generation to be able to use the syntax you like. Here is one possibility:
ClearAll[withLiteralIndices];
SetAttributes[withLiteralIndices, HoldAll];
withLiteralIndices[code_, inds : {__Symbol}] :=
Block[inds,
Unevaluated[code] /.
MapIndexed[
Function[{i, pos}, pos /. {p_} :> (i :> #2[[p]])],
...
9
OK, FrontEnd:
After creating the "overview" button below just click first on your function definition and then on the button and under the function definition you will get (nearly) what you want, i.e. :
Part 1.
Step 1. Local variables
Part 1.
Step 2. Option variables
Part 2.
Step 1. Check for null Input
Part 2.
Step 2.:1 Check for groups
Part 2.
...
8
I would suggest something more simple-minded than Rolf's sophisticated implementation:
Clear[extractComments];
extractComments[boxes_] :=
StringJoin@
Riffle[
Cases[boxes,
r : RowBox[{"(*", ___, "*)"}] :>
StringJoin@Cases[r, _String, Infinity], Infinity],
"\n"];
and the palette:
CreatePalette[
...
7
I think Mr. Wizard provided a very thorough answer to the question. I would however like to add a slight example of wrapping this up nicely in a format similar to Function[] but using destructuring:
SetAttributes[dFunction, HoldAll]
dFunction[pattern_, body_][arg___] /;MatchQ[{arg}, pattern] := {arg} /. pattern :> body
This then allows you to have nice ...
7
Check the CUDALink/OpenCLLink documentation, they have some creative applications
http://reference.wolfram.com/mathematica/CUDALink/tutorial/Applications.html#818090
http://wolfram.com/xid/0isq3flowdud5n74bny881he6-0wsz71
http://wolfram.com/xid/0d195cdqgdtuw44ioyvm6og9pu-bv64sq
Also the webinars have other examples.
6
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/
...
5
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 ...
2
My first answer explains that Mathematica's replacement rules perform destructuring. This answer is intended to complement jVincent's method, which I see is appreciated.
My aim is to provide Attributes for the pattern-based function. This requires that the head evaluate therefore SubValues may not be used. Here are two separate approaches.
Module
A ...
2
You can use Subtract or Differences to get the differences in the index and use FreeQ to test if every element is True. This removes the need for the ugly #2[[1]]-#2[[2]] and also the Flatten and And@@ at the end.
triDiagonalQ[mat_?MatrixQ] :=FreeQ[MapIndexed[(#1 == 0 || Abs[Subtract @@ #2] <= 1) &,
mat, {2}], False]
The pattern test ...
2
Further possibilities that I can think of might be
Genetic Programming.
Code obfuscation
Individualization of source code in order to track piracy.
2
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 ...
Only top voted, non community-wiki answers of a minimum length are eligible


