Questions tagged [meta-programming]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
8 votes
1 answer
421 views

Rule rewriting behavior

This is kind of meta-programming attempt. Below are two snippets: Snippet 1: ...
user avatar
1 vote
3 answers
99 views

Self modifying association (in a search of "THIS" keyword)

Imagine we have the following structure (like a module) ...
user avatar
3 votes
2 answers
462 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 ...
user avatar
15 votes
1 answer
311 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 (...
user avatar
8 votes
1 answer
247 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 ...
user avatar
  • 710
7 votes
1 answer
134 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. ...
user avatar
  • 489
1 vote
1 answer
164 views

How to use Through by default on numeric functions?

I've used the following code to simulate defining operations between functions: ...
user avatar
  • 403
14 votes
4 answers
536 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 ...
user avatar
4 votes
0 answers
130 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 ...
user avatar
  • 1,641
4 votes
1 answer
104 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 ...
user avatar
3 votes
1 answer
100 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 ...
user avatar
  • 127
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: <...
user avatar
  • 17.7k
4 votes
1 answer
137 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 ...
user avatar
  • 123
3 votes
1 answer
79 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 ...
user avatar
  • 710
2 votes
0 answers
99 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 ...
user avatar
  • 45.5k
1 vote
3 answers
210 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)) + ...
user avatar
  • 1,139
2 votes
2 answers
313 views

Plot in a loop?

I have a list of objects, which I can plot manually: ...
user avatar
  • 215
3 votes
2 answers
118 views

How to rewrite a function in a more compact and generalized way?

Consider a function as ...
user avatar
  • 3,113
8 votes
5 answers
509 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 ...
user avatar
2 votes
2 answers
81 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 ...
user avatar
  • 15.6k
10 votes
3 answers
302 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: ...
user avatar
3 votes
2 answers
1k 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 ...
user avatar
  • 2,266
5 votes
1 answer
262 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....
user avatar
  • 153
10 votes
2 answers
427 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 ...
user avatar
  • 8,564
7 votes
3 answers
573 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: ...
user avatar
  • 767
4 votes
2 answers
136 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 ...
user avatar
  • 23.1k
7 votes
1 answer
133 views

Applying And to a list inside a Function

And has HoldAll attribute and test its argument by sequence. But doing And@@{} will lost ...
user avatar
  • 7,661
3 votes
1 answer
346 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 ...
user avatar
  • 7,661
15 votes
1 answer
349 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: ...
user avatar
  • 30.4k
2 votes
0 answers
336 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 ...
user avatar
8 votes
1 answer
238 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,\...
user avatar
  • 19.1k
4 votes
0 answers
237 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) ...
user avatar
  • 8,571
6 votes
2 answers
374 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. ...
user avatar
  • 52.4k
1 vote
1 answer
130 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: ...
user avatar
  • 30.4k
11 votes
1 answer
352 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: ...
user avatar
  • 10.7k
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 ...
user avatar
  • 2,483
16 votes
3 answers
789 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 ...
user avatar
10 votes
1 answer
487 views

A smarter nested With?

I often find myself writing code that looks a bit like this: ...
user avatar
  • 5,921
6 votes
2 answers
416 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. ...
user avatar
  • 8,571
5 votes
1 answer
474 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 ...
user avatar
  • 473
12 votes
3 answers
384 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 ...
user avatar
27 votes
1 answer
712 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 ...
user avatar
  • 1,697
18 votes
2 answers
296 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, ...
user avatar
4 votes
1 answer
387 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] ...
user avatar
  • 1,419
1 vote
1 answer
344 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 ...
user avatar
9 votes
8 answers
2k 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? ...
user avatar
  • 4,932
1 vote
1 answer
205 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 ...
user avatar
  • 733
42 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 ...
user avatar
  • 10.7k
2 votes
1 answer
255 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 ...
user avatar
  • 269
25 votes
3 answers
1k views

Comparing Mathematica expressions like diff

I am looking for a way to compare (or "diff") two Mathematica expressions, similarly to how to diff utility can compare two text files and report the differences. Has anyone already written such a ...
user avatar
  • 225k