Questions tagged [meta-programming]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
11
votes
4answers
975 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: <...
2
votes
1answer
54 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
1answer
67 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
0answers
93 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
3answers
108 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)) + ...
10
votes
3answers
270 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: ...
2
votes
2answers
164 views

Plot in a loop?

I have a list of objects, which I can plot manually: ...
9
votes
2answers
247 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 ...
20
votes
4answers
6k views

How can I implement object oriented programming in Mathematica?

Roman Maeder's object oriented programming package is nice, but I'm hoping someone can suggest a sleek and novel implementation that is easy to use.
7
votes
4answers
398 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
2answers
68 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 ...
9
votes
8answers
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? ...
5
votes
1answer
225 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....
3
votes
2answers
268 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 ...
4
votes
2answers
112 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 ...
16
votes
3answers
765 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 ...
6
votes
1answer
122 views

Applying And to a list inside a Function

And has HoldAll attribute and test its argument by sequence. But doing And@@{} will lost ...
18
votes
2answers
275 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, ...
39
votes
4answers
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 ...
7
votes
3answers
501 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: ...
25
votes
1answer
559 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 ...
3
votes
1answer
260 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 ...
14
votes
1answer
239 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: ...
8
votes
1answer
217 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,\...
2
votes
0answers
281 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 ...
3
votes
0answers
158 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
2answers
330 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
1answer
124 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: ...
10
votes
1answer
292 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: ...
73
votes
3answers
8k views

Metaprogramming in Mathematica

Being functional, and having no "quotation", i.e. essentially treating data and programs semantically same, Mathematica seems naturally capable of doing metaprogramming. But could anyone construct a ...
10
votes
2answers
965 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 ...
12
votes
3answers
311 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 ...
1
vote
1answer
296 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 ...
8
votes
1answer
430 views

A smarter nested With?

I often find myself writing code that looks a bit like this: ...
6
votes
2answers
394 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. ...
4
votes
1answer
447 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 ...
19
votes
1answer
846 views

List manipulation to build a functional expression

Since Mathematica deals with Head[a, b] as an expression in this way The first Part of Head[a, b] is ...
4
votes
1answer
317 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
1answer
165 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 ...
13
votes
2answers
797 views

How can I compile this function

I want to simplify my function f1 to f2, but f2 can't be compiled. How can I make it ...
18
votes
3answers
940 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 ...
2
votes
1answer
231 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 ...
2
votes
3answers
284 views

how can I generate a sequence of assignments?

I want to make a sequence like follows r[1]= r[2]= r[3]= r[4]= .... r[n]= The above r[i] represents coordinates which I have ...
22
votes
0answers
800 views

How can I trace a functional expression's evaluation visually?

Ok, so the goal is to visualize the execution of this expression foo /@ {3 + 1, bar /@ {1 + 2, {4 + 7}}} in the following fashion using nested rectangles with ...
14
votes
2answers
803 views

How to REALLY Hold arguments and capture FullForm?

I'm trying to write an expression deconstructor or FullForm-capturer; might even call it a parser, maybe, but that might be too glorious a word. I got some great ...