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.

learn more… | top users | synonyms (2)

8
votes
1answer
291 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
1answer
322 views

Compiled version of (Norm/@coordinates)

Here I define (NormCoordinates). ...
8
votes
3answers
348 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 ...
8
votes
1answer
164 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 ...
7
votes
4answers
514 views

Loop optimization

I wrote this to generate a 6D distribution of particles. ...
7
votes
5answers
1k views

Something faster than Do loops with AppendTo

I am an "old" programmer used to Fortran and Pascal. I can't get rid of For, Do and While ...
7
votes
4answers
211 views

Efficient way of identifying the indices of first occurrences

It seems to me like identifying the first occurrence of unique elements should take about the same amount of time as it takes to delete duplicates. My data sets are much larger than this (...
7
votes
3answers
302 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
4answers
199 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: ...
7
votes
2answers
1k 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, ...
7
votes
5answers
311 views

LengthWhile until first duplication

I have the need to trim each of a large number of potentially very long lists (10^6+ elements) to the length where the last occurrence of any distinct element happens, or said another way, where the ...
7
votes
3answers
255 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: ...
7
votes
2answers
248 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 ...
7
votes
6answers
223 views

Faster list duplicate 'limiter'

Given some list, e.g., {5, 3, 2, 3, 1, 0, 1, 3, 5, 5, 4, 5, 1, 2, 5, 2, 2, 0, 1, 4}, I need to limit the duplicates, if any, to an arbitrarily chosen number. The ...
7
votes
2answers
182 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
588 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
233 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
239 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
3answers
360 views

Suppress connection to Wolfram|Alpha when making unit conversions

I want to speed up calculations with unit conversions. I suspect primarily that some units evaluate from Wolfram|Alpha. Any way to prevent this automatically, so that Mathematica throws ...
7
votes
1answer
179 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
640 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
380 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
344 views

Exporting a sequence of graphs as video efficiently

I have a sequence of graphs that are being created within Mathematica which I'm exporting to create an animated plot of the graph, as follows. ...
7
votes
1answer
221 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
155 views

Improving the performance of a package for working with rational functions

As Mathematica gets slow for large symbolic calculations, the cost of putting terms over a common denominator (Together), in particular, gets too high. It occurred ...
7
votes
0answers
223 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 ...
6
votes
3answers
832 views

Simulate MATLAB's meshgrid function

Here is MATLAB's meshgrid. I came up with these implementations in Mathematica: ...
6
votes
2answers
224 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
176 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
446 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
2answers
282 views

Memory management and speed for Fast GCD

Let's say that we have some $300\,\text{K}$ digits (arbitrary function) and want to trial factor with $100{,}000{,}000$ first prime numbers. ...
6
votes
2answers
110 views

Integrate perfomance

I have a notebook in Mathematica 4. I'm trying to convert it to use in Mathematica 9. One of the problem is the long computation of definite integral in the new version of Mathematica. Here's the ...
6
votes
1answer
418 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
420 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
143 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
2answers
86 views

Consecutive Print to target same output Cell

Is it possible to conveniently direct the output of consecutive Print statements to the same output without actually accumulating the output in a temporary string/variable. This would be useful in ...
6
votes
3answers
162 views

Enumerate the 1-factors (perfect matchings) of $K_n$

Introduction I would like to enumerate the 1-factors, or (near-)perfect matchings, of the complete graph Kn. The adjacency list representation for Kn is basically { (x, y) | 1 ≤ x < y ≤ n }. For ...
6
votes
1answer
93 views

Better way to get Fisher Exact?

I want to perform a Pearson's $\chi^2$ test to analyse contingency tables; but because I have small numbers, it is recommended to perform instead what is called a Fisher's Exact Test. This requires ...
6
votes
1answer
492 views

Fastest way to do vector / matrix multiplication with constant matrix

Suppose we have a fixed n x n matrix, call it $B$. What is fastest way to evaluate $v^t B\;v$ over many different vectors $v$? Since $B$ is a constant matrix, does this allow the number of operations ...
6
votes
2answers
442 views

Trying to extract information from an MDB file. Import is too slow

I am running a Linux box and as usual the documentation for this type of box is not great. I am trying to open up and extract and possibly include later on information from a "mdb" file. The file ...
6
votes
2answers
357 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
253 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
163 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
2answers
564 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
123 views

Poor CUDALink performance, when calling a kernel multiple times

When working on a project of mine I noticed that CUDALink shows very poor performance when calling CUDA kernels multiple times in a loop-construct. To demonstrate this I put together this little ...
6
votes
0answers
67 views

Slow NIntegrate over complicated domains (sometimes)

Overview I have a very large matrix I need to calculate where each element requires the integration of a product of two functions over some complicated domain. I would like to find a set of options ...
6
votes
0answers
92 views

Compiled function crashes kernel when trying to list over different length arguments

I'm trying to speed up a tally/binning operation that I have to run many times. I have many lists of between 1 and 10 numbers from the range [1,5] and I want to ...
6
votes
0answers
213 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
365 views

Optimizing NIntegrate for higher PrecisionGoal

By default, NIntegrate works with MachinePrecision and its PrecisionGoal is set to ...
5
votes
4answers
154 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: ...