Questions on writing non-trivial programs in Mathematica. Do not use this tag for questions on plotting/graphics or for questions on doing mathematics with Mathematica, where the focus is more on the math than the program.
61
votes
14answers
2k views
What are the most common pitfalls awaiting new users?
As you may already know, Mathematica is a wonderful piece of software.
However, it has a few characteristics that tend to confuse new (and sometimes not-so-new) users. That can be clearly seen from ...
192
votes
10answers
21k 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 ...
54
votes
3answers
4k views
Alternatives to procedural loops and iterating over lists in Mathematica
While there are some cases where a For loop might be reasonable, it's a general mantra – one I subscribe to myself – that "if you are using a ...
52
votes
7answers
4k views
Struct equivalent in Mathematica?
I really miss having something like a struct in Mathematica. I know of (and regularly use) a couple of programming techniques which feel like a ...
32
votes
5answers
2k views
What does the construct f[x_] := f[x] = … mean?
This question mentions "x := x = trickery".
What does defining a function as f[x_] := f[x] = ... do and what is it good for?
23
votes
4answers
838 views
How do I evaluate only one step of an expression?
I am looking for a simple, robust way to evaluate an expression only one step, and return the result in a held form.
The definition of a single step is ambiguous, and this itself is probably worthy ...
39
votes
2answers
778 views
Flatten command: matrix as second argument
One thing I could never wrap my head around is how Flatten works when provided with a matrix as the second argument, and the Mathematica help isn't particularly ...
48
votes
1answer
1k views
Programmatic formatting for Mathematica code - possible?
It seems to be both an interesting programming challenge and a very useful practical application to have a Mathematica program which would allow one to pretty-print Mathematica code, so that it is ...
28
votes
6answers
923 views
Functions vs. patterns
Every time I define a new function, I wonder which construct I should use: true functions obtained by using Function, or rule-based syntax. For example, these are ...
30
votes
4answers
1k views
How can I implement dynamic programming for a function with more than one argument?
Dynamic programming is a technique for avoiding the repeated computation of the same values in a recursive program. Each value computed is immediately stored. If the value is needed again, it is ...
31
votes
7answers
887 views
Updating Wagon's FindAllCrossings2D[] function
Stan Wagon's Mathematica in Action (second edition; I haven't read the third edition and I'm hoping to eventually see it), demonstrates a nifty function called ...
29
votes
2answers
860 views
What is a Mathematica packed array?
A simple sounding question with a few sub questions:
What is the difference between unpacked vs packed array?
Are packed arrays more space efficent, how much so?
Are packed arrays more time ...
30
votes
3answers
2k 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 ...
11
votes
2answers
847 views
Open Excel file with Mathematica
I would like to open an Excel file and manipulate it as a COM object. While I'm able to open an instance of excel with
...
34
votes
8answers
3k views
How to check if a 2D point is in a polygon?
Background: I use code from An Efficient Test For A Point To Be In A Convex Polygon Wolfram Demonstration to check if a point ( mouse pointer ) is in a ( convex ) polygon. Clearly this code fails for ...
16
votes
2answers
408 views
Do I have to code each case of this Grid full of plots separately?
I have written some custom functions to draw multi-panel graphs like this one:
It's done by passing a matrix of (custom) plotting functions to a MultiPanelGraph ...
21
votes
3answers
2k views
ListPlot with each point a different color and a legend bar
I would like to generate a ListPlot with the color for each point in the plot corresponding to a particular value (not associated with the position in the plot). I'd then like to add a legend ...
16
votes
3answers
612 views
Reading periodic elements from a large file
I have a large binary data file (big endian) with 100+ million "rows" of 11 elements, combination of floats and integers.
This is the format:
...
30
votes
1answer
602 views
Once more on object orientation in Mathematica: does it have to be so hard?
Recently I came across a set of problems which would be solved most easily within an object-oriented approach. I first attempted to solve them by other means, but found the complexity of the code ...
24
votes
5answers
1k views
Are there any cases when For[] loops are reasonable?
To my mind the only reason for the existence of For[] loops in Mathematica is to allow new users with some experience in procedural programming languages to write ...
16
votes
5answers
456 views
Is this the most efficient way to round approximate integers to integers while leaving other Reals untouched?
This might seem like an overly simple question, but I need to specify custom plot tick marks as integers (no trailing decimal point) if they are approximately integers, but not if they are not. Using ...
11
votes
2answers
401 views
Using a list of tuples in a pure function
I want to use a list of tuples within a function to make assignments.
Say I want to make assignments of the form
value[i,j] = val
What I have is a list of ...
33
votes
2answers
847 views
Performance tuning for game solving (peg solitaire | senku)
There's a game I saw at a friend's yesterday, that I often see at people's homes, but never for enough time to think on it too hard. It's called peg solitaire (thanks @R.M).
So I came home and I ...
13
votes
2answers
369 views
Question about designing a particular data structure
Background: I store polygons with a color assigned at the face level. In some occasions I want to use vertex colors but I want to store data efficiently, i.e. don't want to duplicate data to the ...
15
votes
1answer
242 views
What can be used as the second argument to Return on your own functions?
Having found out about the second argument to Return I played a bit with it, and the following tests all resulted in ...
17
votes
3answers
738 views
Deploying Mathematica Content Online
I have the following program at home, which draws on a batch of pre-processed files (that are word frequency text files, compiled using ToLowerCase, ...
5
votes
1answer
190 views
Alternative to overloading Set
In the ideas shared in my answer to this post Struct equivalent in Mathematica? at the end I propose a solution that changes the way Set works in a particular case that can be expressed in plain words
...
10
votes
1answer
220 views
Is it possible to use Begin and End inside a Manipulate?
May be someone can explain why I can't use Begin["context`"] and End[] inside a Manipulate ...
5
votes
1answer
286 views
Coding mistake? [closed]
I have just started using Mathematica with v9.0. I am trying to follow a computation from a book on Fourier series with the function $f(x)=x$ on the interval $-\pi < x < \pi$.
Here is the code ...
23
votes
3answers
1k views
How to create a progress bar?
I would like to create a progress bar tool that allows me to see how my computations are going.
I found answers to this question on many sites, I even found a package for it ( ...
18
votes
6answers
811 views
18
votes
2answers
942 views
Cycles of length N in a graph
If I have an undirected graph represented with an adjacency matrix, how can I find all the subgraphs which are a cycle of length N?
I don't really know the math nor the programming language well, so ...
16
votes
1answer
358 views
What is the proper method to load a Mathematica package inside a DynamicModule
I have a DynamicModule that requires loading of Mathematica packages. The usual methods do not seem to apply (...
2
votes
2answers
140 views
Question about MapThread and Dynamic
I don't understand the following:
f[a_, b_] := a + b
ls = {1, 2, 3};
MapThread[f, {ls, {10, 20, 30}}]
This yields ( as expected ) {11,22,33}
If I change ...
18
votes
2answers
753 views
Debugging memory leaks
I've written my incredibly complex, incredibly elegant analysis function, that works great on small test data. But when I run it on my real (bigger) data set it keeps running out of memory. It turns ...
20
votes
4answers
543 views
What tools can help in realizing tail recursion?
I had nice discussions with Leonid and Rojo that got me interested in tail recursion. Tail recursion is not always easy to realize with Mathematica, so it would be nice to have some tools to help with ...
21
votes
6answers
869 views
Can this be written well, without loops?
Inspired by this question I would like to know if the following code can be written without explicit loops (For, While, etc.) in a clean, efficient and non-contrived way. I have been unable to do so.
...
7
votes
2answers
153 views
Restrict Sensitivity of Locators in LocatorPane
We can restrict the movement of locators in a LocatorPane as follows:
In the following example, the first locator's movement is confined to the x-axis and the ...
6
votes
1answer
338 views
Use Mathematica as a terminal
I love how notebooks work in Mathematica.
You can edit code in real time and hit Ctrl+Shift to run it.
Additionally you copy and paste different cells around to organize and test ideas.
Search ...
6
votes
2answers
386 views
Pass function or formula as function parameter
I can write either
Integrate[x^2, {x,0,3}]
or
f[x_] = x^2
Integrate[f[x],{x,0,3}]
and get the same computation. Suppose I ...
7
votes
2answers
398 views
ParallelTable and Table do not give same result
I have a question regarding the use of Parallelize.
In the help one can read that “Parallelize[Table[expr,iter, …]] (which is ...
2
votes
1answer
598 views
Using triangulation
I have been presented with 3 known points and the power densities at those points. I need to use those points to find the location of the actual antenna which is generating the signals.
Power ...
6
votes
5answers
406 views
Select/Delete with Sublist elements?
Probably easy and short question, I still didn't fully figure out how to easily select/delete sublists from a list. Example:
tt = {{2, 4}, {4, 8}}
I want to delete/select all the elements where ...
4
votes
3answers
136 views
Keep function range as a variable
Plot[2*x^2 - x + 2, {x, -1, 1}] plots a function of x from -1 to 1. As far as I can see, I cannot "save" this range in a variable:
...
2
votes
4answers
242 views
Using the result of Solve in subsequent calculations
I'd like to know how to use the results of Solve in later calculations. Here's what I'm doing now:
This is my expression
...
37
votes
1answer
2k views
Is there an open source implementation of Mathematica-the-language?
I've seen questions before such as "What is the best open-source equivalent for Mathematica?", but that specific question (and that line of inquiry in general) cares more about the computer algebra ...
24
votes
4answers
1k views
Can Mathematica be regarded as a software prototyping environment?
I am an undergraduate student, starting to use Mathematica to model simple physical chemistry problems, as a prelude to a summer internship in a computational/theoretical chemistry group. The solution ...
18
votes
3answers
643 views
Downloading files without using Import
A quick question, but which I don't believe has been asked here or at SO.
Does Mathematica have a simple way to just download a file from the web? i.e. if I have a list of PDF links (~ 2,000), can I ...
25
votes
3answers
450 views
Is there a convenient way to copy/paste text-interspersed SE code snippets into Mathematica?
Is there a way to copy and paste code snippets from SE to Mathematica if these snippets are interspersed with text?
Like e.g. in Morphing Graphics, color and location in both the question and answer, ...
12
votes
6answers
557 views
Splitting up delimited data in lists
One task that I frequently find myself doing in Mathematica is splitting lists into lists of sublists, using specific elements to define the break-points. This is particularly useful with imported ...
