Questions on optimizing Mathematica code for higher performance. This may mean faster execution, lower memory usage or both. Not to be confused with mathematical optimization.
8
votes
2answers
454 views
Mathematica rule substitution memory
First I present the specific programming question, and then I provide further background details.
Given an expression in Mathematica, say f=3x+7y, there are two ...
8
votes
1answer
263 views
Making a “cached” version of `Manipulate[]`?
I have a function that is a bit too slow for Manipulate[]. Right now I plan to cache outputs outside the manipulation and just have the manipulator select among ...
8
votes
1answer
228 views
Optimizing the performance of an algorithm
First - a bit of an introduction. If you're only interested in the code, you can skip this section.
The following question is drawn from Dennis E. Shasha's Puzzling Adventures, and is listed under ...
8
votes
1answer
239 views
Vlookup function as Excel in Mathematica
How to create a Vlookup function, similar to what we have in Excel. So if we have a table a and another b, we could add columns ...
8
votes
2answers
192 views
Correct way to generate large data sets (i.e.forward yield curve )
I would like to generate a set of forward yield curve matrix of size 1000 x 100. First I defined my SparseArray of 1000 x100:
...
8
votes
1answer
376 views
GeoDirection and GeoDistance Memory Leaks: How to Recover the Memory?
The functions GeoDirection and GeoDistance have memory leaks. To see this, run the example functions below and watch as the Physical Memory Available in the Windows Task Manager, Performance tab, ...
8
votes
1answer
417 views
Rapidly binning 3D data
I have a set of data (about 20 million datapoints for my real data) of the form {x, y, z, m}. There are 3 spatial coordinates x, ...
8
votes
1answer
233 views
Speed up DensityHistogram rendering (display)
When I tried to present some pre-calculated density histograms inside Manipulate I noticed that the display of DensityHistogram is extremely slow. Here is a simple ...
8
votes
0answers
178 views
Using Compile to speed up Function with PermutationProduct
I have the following fairly simple routine dot involving PermutationProduct that I wish to speed up using ...
7
votes
8answers
327 views
Thread over list in different levels
I have these two lists, both with the same length in the first dimension:
...
7
votes
4answers
386 views
7
votes
3answers
215 views
Making sure that Timing[] isn't thrown off by caching
In playing with and comparing the various answers to my question here, I tried applying Timing[] to see how fast things ran, but it seemed that some of the answers ...
7
votes
3answers
403 views
PolarPlot render oddities
I apologize if this is a obvious question and answer, I don't often use Mathematica to display plots or graphics in general to be honest. So, I was tutoring my cousin yesterday in relation to Polar ...
7
votes
2answers
174 views
Manually specifying distance metric to Nearest slows down calculation
Why does manually specifying the distance metric to Nearest slow down the calculation so much?
This is relevant to my previous question. I've been using ...
7
votes
2answers
392 views
Working with large data, pass-by-reference, compiled functions
I think I know the answer to this, but after two days of Googling, this is my last effort before going down an unwanted path...
I have a function that takes lots of large data as arguments as well as ...
7
votes
2answers
156 views
Speed up Fourier for Booleans
I'm calculating a bit of the power spectrum of a two color cellular automaton using:
...
7
votes
2answers
140 views
Solving variant of the knapsack/money-changing problem
I'm trying to solve a variant of the knapsack/changing money problem where I have a set of a few numbers and I'm trying to find the linear (integer) combinations of them which are close to a given ...
7
votes
1answer
162 views
Compiling Map over expression that yields a ragged array
I'm trying to speed up a function that looks in the neighborhood of each 3D point in a large dataset and finds all the points within 1 unit in each direction, x, y, z.
I've started by using ...
7
votes
2answers
102 views
7
votes
1answer
164 views
Why do images in a PopupMenu sometimes make a program load sluggishly?
In a demonstration, I would like to display a PopupMenu with a list of small PDF images, each weighing in at approximately 50k. The code of interest is below:
...
7
votes
1answer
141 views
Efficiently filling area with disks located at certain points
Starting from a set of points, I want to fill an area using disks. Each disk's center should be one of the points and the disks should not overlap. I've managed to write a function that, given a list ...
7
votes
1answer
410 views
How to speed up Minimum Spanning Tree algorithm?
How to speed up MST algorithm? I work with graphs and I have adopted Prim's algorithm for my needs, from here.
The graphs which I use are very dense (number of vertexes: $V=500$ and every vertex is ...
7
votes
1answer
251 views
Very fast way to do a coordinate frame transform
I need a function that rotates and translates a huge amount of line segments.
For example, I have a set of line segments in the form {{x0,y0,z0},{x1,y1,z1}}
...
7
votes
1answer
295 views
7
votes
1answer
187 views
how can I avoid CPU waste (by master kernel) using WaitNext?
I'm using WaitNext in Mathematica for parallel processing on Mac OS X. I have 4 processors on my MacBook and LaunchKernels[] ...
7
votes
0answers
62 views
How to create internally optimized expression for computing with high WorkingPrecision?
I have large dataset and need to fit rather complicated function on it with different values of one of its parameters (this parameter must be fixed in every fit). I use the ...
6
votes
3answers
286 views
With versus Function
I've seen people compare Block with Module and With as scoping constructs, and try to figure ...
6
votes
4answers
155 views
Why does array packing in Table behave like this?
Consider the following example, which generate two identical arrays. Why sometimes the array is packed and sometimes it doesn't? And why they perform differently?
Here the arrays are packed:
...
6
votes
2answers
182 views
Fastest way to test if a numerical array has complex elements
I am looking for the fastest way to test whether an n-dimensional numerical (NumericQ) array (in the ArrayQ sense) is complex or ...
6
votes
2answers
194 views
Compiling a spatial predator-prey model and optimising it for speed
I just made a spatial predator-prey model but it runs a little slow and I am seeking some advice on how to compile it to optimise it for speed.
I first define matrix with 0 for an empty site, 1 for a ...
6
votes
2answers
119 views
How can this AbsoluteTime call be made faster?
I need to convert tons of date strings into absolute times. This generates an example data set of 10,000 strings:
...
6
votes
2answers
370 views
Easier program for period of Fibonacci sequence modulo p
For a little project I need to calculate the period of a Fibonacci sequence modulo p, for which p is a prime number. For example, the Fibonacci sequence modulo 19 would be:
$$0, 1, 1, 2, 3, 5, 8, 13, ...
6
votes
2answers
308 views
Web scraping and multi-threading in Mathematica
I'm working in some web scraping using Mathematica, and today, to speed-up the process, I used bash commands (xargs with parallel options with curl and wget) using ...
6
votes
1answer
272 views
Speeding up numerical Fourier Transform
I wrote this function NFourierTransform, which takes a function $f(k)$ and numerically calculates the fourier transform integral for discrete values of $k \in ...
6
votes
2answers
335 views
Very long Refine/Solve batch run - is my code broken, or just complicated?
So I'm trying to run some Mathematica code in batch mode from my university cluster. Specifically, I'm trying to find the equilibria of a system of ordinary differential equations. Inspired by the ...
6
votes
1answer
134 views
Efficient conditional division
I was curious about the following little problem. Suppose $n \in \mathbb{N}$ is some large number and $p$ is a (possibly large) prime, and I want to update $n$ according to
$$n := \begin{cases} n/p, ...
6
votes
1answer
265 views
Compiling a recursive formula
My question is related to computing what is called "invariant measure" for a particular well known fractal - the Sierpinski triangle.
We have an array m of four two by two matrices, say
...
6
votes
1answer
128 views
Elementwise, compilable min function
I am trying to implement efficiently a transfer-matrix like algorithm. On each iteration, I have two vectors $x=\{x_1,\dots,x_n\}$, $y=\{y_1,\dots,y_n\}$ with real numbers and I need to compute the ...
6
votes
1answer
279 views
Tridiagonal symmetric matrix eigenvalue using bisection
I know that Eigenvalues is already quite well implemented in Mathematica, nor am I foolishly trying to improve on it. In order to improve my programming skills, I ...
6
votes
2answers
419 views
Speeding up mathematica by subsitituting numerical values
According to this Wolfram Blog post, one can speed up Mathematica code by substituting numerical values as soon as possible.
How does one substitute before the main expression is evaluated? The ...
6
votes
2answers
207 views
CorrelationFunction for vectors
Mathematica 9 has a new CorrelationFunction. Sadly the joy does not last long, as I can't get it to work with vectors. I would like to make a velocity correlation ...
6
votes
1answer
189 views
1D Random Walk with Excluded Sites and Variable Time-Step
Here is the physical problem I would like to simulate:
Consider the following variations to the 1d random walk:
allow for certain, random sites to be excluded with probability 1/2.
allow for the ...
6
votes
2answers
143 views
Control which variables get embedded inside Manipulate
While trying to analyse several different datasets I got what I thought was a good idea: define a "function" that returns a Manipulate object. But this has some ...
6
votes
3answers
286 views
Why is AstronomicalData so slow?
I'm having trouble getting some pretty simple functions using AstronomicalData to perform at anywhere near the speed I need them to.
For example, I have many ...
6
votes
2answers
325 views
Elegant way to handle columns operations
Inspired by this question from @sjdh and by my recurrent use of columns operations in database sets, I was looking for one way to make columns operations more symetric, so I can handle with matrix and ...
6
votes
0answers
146 views
Faster simplification of linear combinations of some specific functions
After some profiling I've found that my program spends most of its time in the simplification step. The program uses my simplification function many times (over different data). My calculation is not ...
6
votes
0answers
219 views
Optimizing NIntegrate for higher PrecisionGoal
By default, NIntegrate works with MachinePrecision and its PrecisionGoal is set to ...
5
votes
4answers
125 views
Performance: Collapsing repeated contiguous rows & cols from a matrix
In this answer I needed to remove contiguous zero-valued cols and rows from a matrix, leaving only two of them in place, no matter what the original number was.
I made up this code:
...
5
votes
3answers
377 views
Fast Sieve Implementation
I'm going to write a code to compete in performance with NewPGen. The goal is to sieve numbers that have small factors. Mine is very slow in compare with NewPGen.
Here's my code:
...
5
votes
3answers
159 views
Benchmarking expressions
It often happens that I'd like to know which of two or more alternative expressions (that should evaluate to the same value) is fastest. For example, below are three ways to define a function:
...

