Questions tagged [meta-programming]
The meta-programming tag has no usage guidance.
58
questions
0
votes
2
answers
56
views
Streamlining Dataset Select meta-programming
Imagine you are deep in studying the Powerball Winning Numbers starting from 2010:
...
8
votes
1
answer
445
views
Rule rewriting behavior
This is kind of meta-programming attempt. Below are two snippets:
Snippet 1:
...
1
vote
3
answers
120
views
Self modifying association (in a search of "THIS" keyword)
Imagine we have the following structure (like a module)
...
3
votes
2
answers
474
views
Including literals into compiled C code
I have a function that I want to Compile into C for speed. Inside this function is a certain long expression called x, which has been computed earlier in a Mathematica session. However, if you call x ...
19
votes
1
answer
425
views
Programming language prototyping in Mathematica
Are you aware of any projects using the Wolfram language resp. Mathematica as an environment to explore the design of programming languages - in particular languages with a focus on mathematics (...
9
votes
1
answer
353
views
How to write a simple interpreter / DSL with Wolfram Language?
I am specifically trying to write an interpreter for Mathematica predicates to MongoDB aggregation stages, but I wanted to phrase the question a bit more generically because I can see many times one ...
9
votes
1
answer
151
views
How to define a version of Table[] which localizes iteration variable by Module[]?
For reusable code it's good practice to use Module[] to localize iteration variables used by Table[], as explained by this post.
...
1
vote
1
answer
166
views
How to use Through by default on numeric functions?
I've used the following code to simulate defining operations between functions:
...
14
votes
4
answers
540
views
Can we make an "Outerable" Attribute?
We can declare a function to have the Listable Attribute so that it automatically threads over lists in a particular way. For functions of multiple arguments it ...
4
votes
0
answers
143
views
What is the best coding style for performance?
The problem of Mathematica is there are too many ways to perform one task. That leads to confusion for new users because the performances of these methods are so different.
I'm learning the best ...
5
votes
1
answer
113
views
Turn definitions into rules
I know that this seems to be unnecessary, but here I have a bunch of assignments, like around 50, and I want to create rules for all these to have the freedom to apply these rules only on certain ...
3
votes
1
answer
106
views
How to do meta-programming inside Compile?
I'd like to do this: solve some linear systems, compiling the task of getting the building up the matrices from parameter values, and then I have a compiled implementation of LinearSolve. The problem ...
12
votes
4
answers
1k
views
Defining a Function programmatically
I need to create a function programmatically. For example, suppose I've got:
mon - a Symbol
mons - a List of Symbols
vars - another List of Symbols (same Length)
and want to make a function as:
<...
4
votes
1
answer
183
views
Functions that remember some arguments while not remembering other arguments
I would like to some programming that is very generic. Particularly I am interested in the following:
Let's say I want to write a function
...
3
votes
1
answer
83
views
Reducing boilerplate with associations
I'm looking for advice either on alternative ways to approach my problem in general, or specifically on meta-programming (which seems to me to be a way around this). In my work I have databases of ...
2
votes
0
answers
101
views
Get matching types for pattern
I want to take a pattern and get a set of matching types. The set of types does not need to be complete, but it needs to satisfy, say, 90% of the cases.
Here's is a (definitely not complete) set of ...
1
vote
3
answers
224
views
How to randomly generate Tree or Nested List of Function in Mathematica?
In detail,
I want to generate randomly something like
Divide[Add[X,Times[X,Y]],Times[X,3]]
or
{Divide,{Add,X,{Times{X,Y}}},{Times,X,3}}
(Specified Function(# of argument is from 2 to 5)) + ...
2
votes
2
answers
364
views
Plot in a loop?
I have a list of objects, which I can plot manually:
...
3
votes
2
answers
119
views
How to rewrite a function in a more compact and generalized way?
Consider a function as
...
8
votes
5
answers
552
views
Mathematica function which formats Physics calculation
One small step toward automating homework :)
My lecturer doesn't accept calculations done in Mathematica code as homework. I have 30 pages of small calculations such as the one below. I don't want to ...
2
votes
2
answers
85
views
How to block list of variable that is of string form?
For example, I have
varList={"a1","a2",...}
a1, a2,... are all variables, they maybe ...
10
votes
3
answers
307
views
How to improve the creation of tables of code and comments
While writing a response to a certain MSE question I made a function that tabulates code and comments. (See the definition below.)
Here is an example:
...
4
votes
2
answers
2k
views
How to set all variables in function as local
Here is an example code:
a = 1;
b = 1;
c = a + b;
f[x_, y_] := Module[{a = 3, b = 2, c}, c = (x + y)*(a + b)];
a,b,c are ...
5
votes
1
answer
272
views
Meta information about function parameters
I'm working on a project that allows for executing arbitrary, user-defined Mathematica scripts. It's an internal tool running on a trusted, local network. It's basically a web interface to Mathematica....
10
votes
2
answers
487
views
Which operators are missing from the official precedence table?
I often use the Operator Input Forms page as a reference for operator precedence as well as which (used or unused) operators exist. However, I just noticed that at least one operator is missing from ...
8
votes
3
answers
605
views
Metaprogramming: creating compiled functions from inter-dependent code blocks
I want to create a compiled function from inter-dependent code blocks.
Here is a minimal example showing the salient features based on strings:
...
4
votes
2
answers
139
views
Handle Format when importing package file content unevaluated
This question and its answers explains two methods which allow to import the content of a package file as held expressions. That is very useful as one then can manipulate that imported held code as ...
7
votes
1
answer
134
views
Applying And to a list inside a Function
And has HoldAll attribute and test its argument by sequence. But doing And@@{} will lost ...
4
votes
1
answer
371
views
Generate assignment code to symbols from string
I want the function to take in string, then assign a value to its corresponding symbol. An implementation using ToExpression is
...
17
votes
1
answer
411
views
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:
...
2
votes
0
answers
361
views
Best approach for 'manual' common subexpression elimination
I have working code, but am looking for ways to make it more elegant.
I have a fairly large expression with plenty of repetition and structure. A shortened toy example is
...
8
votes
1
answer
239
views
How to efficiently write code with many functions that have variable number of iterators/arguments/
I am finding myself having to code functions like this:
$$\text{answer} = \sum_{k_0 = 0}^{\color{Blue}n_0} \sum_{k_1=0}^{\color{Blue}n_1}\cdots\sum_{k_{N-1}=0}^{\color{Blue}n_{N-1}} \frac{P_{k_0,k_1,\...
4
votes
0
answers
272
views
Delimited Continuations: Easy? or Fundamentally Difficult?
Apologies in advance for the length of this question, but this is as short as I have been able to make it.
The Prescription
I'm looking at the following tutorial on delimited (or composable)
...
6
votes
2
answers
393
views
Add semicolon at the end of code line programmatically
Suppose we have several lines of code in a cell:
a
b;
Module[{}, c
d]
e
How can we wrap them in a CompoundExpression i.e. ...
1
vote
1
answer
131
views
How to freeze and test a list of programs?
I have a list of problems for my students and I'm trying to use the new v10 feature TestResults like so:
...
12
votes
1
answer
378
views
What's the goal of the Macros` package?
I see in the Mathematica install a Macros` package. In windows the paths is for example:
...
13
votes
2
answers
1k
views
How to programmatically covert a function-like rule to a pure function?
Is there a programmatic way to convert a Mathematica rule that acts like a typical mathematical function of one variable into the equivalent pure function? For example, the code should convert
...
16
votes
3
answers
798
views
How would I return a random Mathematica command?
I'm doing some metaprogramming. How would I make a Mathematica function that returns a random Mathematica command? Is there a list of command names that I could use ...
11
votes
1
answer
535
views
A smarter nested With?
I often find myself writing code that looks a bit like this:
...
6
votes
2
answers
419
views
How to write complex function definitions at run time?
(Posting as a self-answered question to share a technique I discovered)
In a certain project, I have many function definitions in which all parameters should be optional and come with default values. ...
5
votes
1
answer
489
views
How to use Mathematica for traditional programming
I am new to the structure of Mathematica. I understand that in the notebooks, one enters the symbolic expressions and evaluates them. But I need to generate some simulated data and do that a few ...
12
votes
3
answers
424
views
Find all heads a function is defined on
So say I've got a function g defined as follows:
g[x_Integer] := x + 1
g[s_String] := s <> "!!!"
How would I write a ...
27
votes
1
answer
738
views
Can code optimize code?
Are there functions, programs or packages that deal with optimizing mathematica code i.e. re-writing ineffecive or convoluted code.
-This question has haunted me since I joined SE.
The background for ...
19
votes
2
answers
307
views
How to find out what files are touched when Get is called?
Is there a way to capture all package files touched whenever a package is called via Get, Needs, ...
4
votes
1
answer
409
views
Nested NDSolve with WhenEvent: setting up new equations and discarding old ones
I am checking a complex situation where a function x1[t1] only exit in a finite time span, and after this a new function x2[t2] ...
1
vote
1
answer
362
views
using a Mathematica function to define a new function
I'd like to define a function of three variables which produces a new, named function of a single variable, where this final variable is not a member of the first three. So I'd like something where I ...
9
votes
8
answers
3k
views
Better code to find Narcissistic number
My code finding Narcissistic numbers is not that slow, but it's not in functional style and lacks flexibility: if $n \neq 7$, I have to rewrite my code. Could you give some good advice?
...
1
vote
1
answer
206
views
Simple Macro or Meta Programming for Data Analysis [duplicate]
I have a large number of data variables that are represented in MMA as symbols set to lists. For example:
x = {1, 2, 3};
y = {4, 5, 6};
I want to be able to ...
43
votes
4
answers
3k
views
Compiling more functions that don't call MainEvaluate
I would like to use Compile with functions defined outside Compile.
For example if I have the two basic functions ...
2
votes
1
answer
268
views
Examine function parameters programmatically
I write a lot of scripts in Mathematica and I'd like to eliminate the boilerplate that parses command-line arguments and assigns them to variables of the correct type before doing the real work. I was ...