Skip to main content

meta programming - Macro functions in GeneralUtilities?


Questions have been asked about the Macros package before (e.g., (83815)), but I'm interested if anyone has some examples of using the macro related functions in GeneralUtilities:


In[2397]:= Names["GeneralUtilities`*Macro*"]

Out[2397]= {"DefineLiteralMacro", "DefineMacro", "MacroEvaluate", \
"MacroExpand", "MacroExpandList", "MacroPanic", "MacroRules", \
"UseMacros", "$MacroDebugMode"}


I find their docstrings a bit cryptic:


enter image description here



Answer



The only official information about the Macro functions I am aware of can be found in a short segment in this presentation, which describes the "Macro System" as an essential part of the next compiler generation. However, the CreateMExpr shown there isn't included in Mathematica 10.4.1. (But maybe someone can find parts of it digging deeper into it?)


Nevertheless, one can already apply the Macro System practically (while being aware that this is an undocumented functionality with all the consequences involved) outside of that next generation compiler context.


First let's load the GeneralUtilities package and see what Macro functions are available as of version 10.4.1.


Needs["GeneralUtilities`"]
Names["*Macro*"]



{"DefineLiteralMacro", "DefineMacro", "MacroEvaluate", "MacroExpand", "MacroExpandList", 
"MacroPanic", "MacroRules", "UseMacros", $MacroDebugMode"}

First


?MacroRules

MacroRulesUsage


can be used to see how macros are defined within Mathematica. The most illustrating example is


MacroRules[DoWhile]


DoWhileMacro


One can get an overview of all predefined macros (that I found) using


MacroRules /@ List @@ GeneralUtilities`Control`PackagePrivate`$MacroHeadsPattern //
Grid[#, Frame -> All, Alignment -> Left] &

The Panels are how Quoted code is displayed in the Front End.


?Quoted

Quoded_Usage




Let's assume we don't like the common Increment syntax and want to use an incr function instead.


DefineMacro[incr, incr[n_] := Quoted[n++]]

incrMacro


incr itself doesn't have any DownValues, therefore


counter = 0;
Table[incr[counter], 5]


{incr[0], incr[0], incr[0], incr[0], incr[0]}




The macro can be applied using


MacroEvaluate@Table[incr[counter], 5]


{0, 1, 2, 3, 4}



To see what the macro does, one can use


MacroExpand@Table[incr[counter], 5]


MacroExpand_incr


and


MacroExpandList@Table[incr[counter], 5]

MacroExpandList_incr



Let's say we have some code that contains some Table expressions in the form Table[expr, n] and we want to replace them with Array and add 1 to every expr. To do this a macro can be defined in the following way:


DefineMacro[macro1, macro1[Table[expr_, n_?IntegerQ]] := Quoted[Array[1 + expr &, n]]]

macro1



Just applying macro1 to a Table does nothing special


macro1[Table[RandomReal[], 5]]


macro1[{0.0977249, 0.987251, 0.484049, 0.712003, 0.358444}]



but


MacroEvaluate@macro1@Table[RandomReal[], 5]



{1.60094, 1.53355, 1.49432, 1.74318, 1.648}



does make use of the macro.


MacroExpand[macro1[Table[RandomReal[], 5]]]

MacroExpand_macro1


MacroExpandList[macro1[Table[RandomReal[], 5]]]

MacroExpandList_macro1


If one tries to apply this macro to a Table having a different syntax pattern a Failure message is created.



MacroExpand[macro1[Table[RandomReal[], {i, 5}]]]

FailureMessage


Using replacement rules instead of a macro is less convenient, as the syntax is more complex.


Unevaluated@
Table[RandomReal[], 5] /. {Verbatim[Table][expr_, n_?IntegerQ] :> Array[1 + expr &, n],
___ -> $Failed}


{1.14275, 1.55058, 1.13318, 1.05475, 1.75428}





Let's assume we have defined a function


f[n_] := Table[RandomReal[], n]

and now we realize that for big integers n this implementation does perform much worse than RandomReal[{0, 1}, n]. A macro can be used to refactor our code.


DefineMacro[f, f[n_] := Quoted[RandomReal[{0, 1}, n] + 1]]

(The + 1 is just added to directly judge by the output, if the macro has been applied.)


For small n the overhead of using the Macro System does outweigh the performance increase of not using Table,



Table[RandomReal[], 10]; // AbsoluteTiming
RandomReal[{0, 1}, 10]; // AbsoluteTiming
RandomReal[{0, 1}, 10] + 1; // AbsoluteTiming
MacroEvaluate[f[10]]; // AbsoluteTiming


{0.0000126977, Null}
{8.35374*10^-6, Null}
{0.0000110269, Null}
{0.00018278, Null}


but becomes neglectable for big n.


f[1000000]; // AbsoluteTiming
MacroEvaluate[f[1000000]]; // AbsoluteTiming


{0.0549422, Null}
{0.0149402, Null}

One interesting thing to note is that the macro gets automatically applied when f is used to define an other function. For example



g[m_] := f[m]

DownValues@g


{HoldPattern[g[m_]] :> RandomReal[{0, 1}, m] + 1}



That this will be happening in other cases too becomes evident by looking at


 ?? f


Using the Macro System is also very helpful in conjunction with Compile.


cf = MacroEvaluate@Compile[{{n, _Integer, 0}},
f[n]
]

CompilePrint@cf

CompilePrint@cf


Here using MacroEvaluate has two effects:




  1. The macro for f gets applied and therefore the more efficient version is compiled.

  2. f[n] can directly been put into Compile.


Without the Macro System directly using f[n] inside Compile results in a call to MainEvaluate.


Compile[{{n, _Integer, 0}},
f[n]
] // CompilePrint

CompilePrint


Comments

Popular posts from this blog

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]

plotting - Filling between two spheres in SphericalPlot3D

Manipulate[ SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, Mesh -> None, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], {n, 0, 1}] I cant' seem to be able to make a filling between two spheres. I've already tried the obvious Filling -> {1 -> {2}} but Mathematica doesn't seem to like that option. Is there any easy way around this or ... Answer There is no built-in filling in SphericalPlot3D . One option is to use ParametricPlot3D to draw the surfaces between the two shells: Manipulate[ Show[SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], ParametricPlot3D[{ r {Sin[t] Cos[1.5 Pi], Sin[t] Sin[1.5 Pi], Cos[t]}, r {Sin[t] Cos[0 Pi], Sin[t] Sin[0 Pi], Cos[t]}}, {r, 1, 2 - n}, {t, 0, Pi}, PlotStyle -> Yellow, Mesh -> {2, 15}]], {n, 0, 1}]

plotting - Mathematica: 3D plot based on combined 2D graphs

I have several sigmoidal fits to 3 different datasets, with mean fit predictions plus the 95% confidence limits (not symmetrical around the mean) and the actual data. I would now like to show these different 2D plots projected in 3D as in but then using proper perspective. In the link here they give some solutions to combine the plots using isometric perspective, but I would like to use proper 3 point perspective. Any thoughts? Also any way to show the mean points per time point for each series plus or minus the standard error on the mean would be cool too, either using points+vertical bars, or using spheres plus tubes. Below are some test data and the fit function I am using. Note that I am working on a logit(proportion) scale and that the final vertical scale is Log10(percentage). (* some test data *) data = Table[Null, {i, 4}]; data[[1]] = {{1, -5.8}, {2, -5.4}, {3, -0.8}, {4, -0.2}, {5, 4.6}, {1, -6.4}, {2, -5.6}, {3, -0.7}, {4, 0.04}, {5, 1.0}, {1, -6.8}, {2, -4.7}, {3, -1....