Linked Questions

579
votes
19answers
147k views

Where can I find examples of good Mathematica programming practice?

I consider myself a pretty good Mathematica programmer, but I'm always looking out for ways to either improve my way of doing things in Mathematica, or to see if there's something nifty that I haven't ...
46
votes
5answers
4k views

How to create symbols from strings and set values for them?

I am trying to convert a list of string names into symbols, which will then be used to store data. I have 24 files (where the name of each file is a member of the list mentioned above) that I need to ...
24
votes
4answers
2k views

How to match a pattern with a pattern?

UPDATED This question is about: "What is the pattern to match a pattern-definition, exactly as it is written?" My original question is now split according to Mr.Wizards insightful answer below. This ...
6
votes
4answers
248 views

Put local variables for Block in a variable

Is is possible to assign {x = 2, y = 3, z = 4} to a variable var so that one can write ...
79
votes
3answers
9k 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 ...
19
votes
3answers
925 views

Generating assignments and transforming code inside held forms when generating code

I have been playing with slightly above trivial code manipulations in Mathematica. I thought Mathematica would be great for this kind of work because of rule replacement and expression manipulation, ...
21
votes
2answers
679 views

Should I always be using Module in functions unless I explicitly want global variables?

I define functions a ton. In the past, I've mostly just defined my functions like: myFunc[var1_]:=( var2=var1^2; Return@var2; ) And I haven't run into too ...
20
votes
2answers
544 views

Clean and handy Options management/filtering

the case I want to be able to create a function with some default options but also without need to add full explicit list of options available for it. And then inside I want to be able to filter ...
13
votes
2answers
2k views

Why can't I change the value of MaxRecursion in NIntegrate when integrating BesselJ?

Bug introduced in 8.0.4 or earlier and persisting through 11.0.1 or later I am trying to evaluate this integral numerically $$ \int_0^{\infty } J_0(q R) \tanh(q) \, \mathrm{d}q $$ for large values ...
4
votes
2answers
442 views

Defining a sequence slot programmatically

This question is related to: Composition of functions using slots `Slot` (#) interfering with evaluation Consider a toy example: ...
4
votes
2answers
407 views

Comparing LetL and Module efficiency

I was recently introduced to the LetL macro thanks to Leonid's answer to one of my prior questions. I was, needless to say, impressed by the simplicity of its ...
3
votes
2answers
76 views

Scoping construct for dependent constants

I sometimes want to write a function which needs to apply a bunch of transformations that are not functions of a single parameter only: ...
3
votes
2answers
141 views

make a slider for a list of matrices

I have a list of matrices, called $matList$, which are sequentially multiplied by another matrix $G$. I created a slider for $matList$, but the slider does not work. Here is the ...
2
votes
2answers
321 views

Local variables whose definitions depend on each other [duplicate]

Consider the following (pseudo-)function definition: f[l_] := Let[{ a = Length[l], b = First[l], c = b/a, }, c*10] So for ...
1
vote
2answers
138 views

Getting past "infinite expression 1/0 encountered"

The following function ...
119
votes
1answer
7k views

File-backed lists/variables for handling large data

Background Currently I am working with some large data (most of it generated by Mathematica itself). I usually find it a hassle to do this. For example, I just exported a large amount of data to ...
26
votes
1answer
327 views

SyntaxInformation cannot reproduce syntax coloring of Block

Is there any way to have the same syntax coloring with SyntaxInformation that Block has? ...
25
votes
1answer
984 views

A debug utility to print or extract intermediate data from a program

Is there a way to print variables using their names, or to extract the corresponding information for later analysis ?
23
votes
1answer
460 views

Bug with iterated With: Referring to function arguments invalidates previous definitions

Thank you for your report. I have forwarded your report to the appropriate team for review. - WRI ...
18
votes
1answer
581 views

Removing calls to MainEvalute when using inlined compiled closures

This question is tightly related to the answer Shaving the last 50 ms off NMinimize. There @OleksandR shows how inlined closures can be used to eliminate calls to ...
11
votes
1answer
220 views

Proper Syntax highlighting for multi-argument With?

For the case of Block and Module one can choose to define values in the body for the variable names being scoped. So there is no ...
9
votes
1answer
472 views

A smarter nested With?

I often find myself writing code that looks a bit like this: ...
8
votes
1answer
113 views

Local variables in sums and tables - best practices?

Stumbled on Local variables when defining function in Mathematica in math.SE and decided to ask it here. Apologies if it is a duplicate - the only really relevant question with a detailed answer I ...
8
votes
1answer
234 views

Replace nested external definitions in Compile

f = #^2 &; Compile[{{x, _Real, 1}}, f /@ x, CompilationOptions -> {"InlineExternalDefinitions" -> True}] // CompilePrint works fine. ...
6
votes
1answer
164 views

Where can I put my conditions?

I just learned in this answer that you can put conditions inside a With and thus use computed values in them, like this: ...
6
votes
1answer
725 views

Functional programming Style & Performance

Hello i have been spending time to convert my R tools into Mathematica Packages mainly because i like the functional programming style in Mathematica. In doing so it seems that i run into one ...
3
votes
1answer
361 views

Is there any way to implement a "sequential" With[] in Mathematica? [duplicate]

I want the equivalent of Scheme's let*, or basically, a sequential With that works like this: ...
3
votes
1answer
502 views

Permuting indices to form fully symmetric tensor with repeated indices

I have written a function (version 8) that takes as an input a list of indices such as {i,j,k} and outputs a fully symmetric tensor function containing $p_i$ and $...
2
votes
1answer
66 views

LetL Breaks in ParallelEvaluate

This was a wild head-scratcher for me to hunt down, but I now am sure LetL is the culprit. Here's a small example: ...
2
votes
1answer
101 views

Block assignments not quite sequential?

With scalars, Block acts like Scheme's let*, that is, the assignments (appear to) happen sequentially: ...
2
votes
1answer
282 views

Reuse local variable in declaration within With [duplicate]

Considering the richness of Mathematica.SE, I am quite surprised I could not find my answer... I am very sorry if it is a duplicate. Consider the following example: ...
1
vote
1answer
67 views

Weird behaviour of If inside of Block definitions?

I am on Mathematica 9 and I have discovered, that, when I use If inside of the definitions of the Block variables, then other, ...
0
votes
1answer
58 views

Variables referring to other variables within Module

The following doesn't work: Lol[x_, y_] = Module[ {w=x+1, z = w + 1}, z] because z refers to ...
2
votes
0answers
326 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 ...
1
vote
0answers
50 views

Dependent variables in With operator [duplicate]

I want write something like this With[{x=1, y=x+1}, y] and get 2 as an answer. What is the correct way to do this?
1
vote
0answers
104 views

How to implement nested With as a single construct using functional programming (Fold)?

It is possible to define my own With construct where each local constant can depend on the previous defined local constants? I wish to replace something like this ...
1
vote
0answers
80 views

What's the best practice for nested local constants? [duplicate]

Possible Duplicate: How to avoid nested With[]? I have many situations where I have a constant that is local to a function, with other constants that are computed from it. I see that I can ...