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
3answers
291 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
4answers
213 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: ...
9
votes
1answer
484 views

Modular arithmetic - efficiently calculating the remainders of factorials

When working on this question regarding the divisibility of the sum of factorials, I decided to write some code to test "small values" of the problem using the following code. ...
15
votes
4answers
408 views

How to remove duplicates from set of machine precision 2D points?

I have a set of 2D points with machine precision coordinates. I need to remove all duplicates. Performance is important. This is the most obvious fast solution: ...
18
votes
3answers
2k views

Fastest way to calculate matrix of pairwise distances

It is a very common problem that given a distance function $d(p_1,p_2)$ and a set of points pts, we need to construct a matrix ...
11
votes
5answers
336 views

Faster way to convert triangle list to edge list with no duplicates

I have a list of triangles returned by a Delaunay triangulator, in the following format: ...
14
votes
5answers
698 views

Iterate until condition is met

I want to find the first 5 prime numbers of the form $n^6 + 1091$. I have used this code: Timing[Select[Table[n^6 + 1091, {n, 10000}], PrimeQ, 5]] Which gives ...
1
vote
1answer
120 views

PDF on TransformedDistribution of two BinomialDistribution too slow

I'd been doing my own convolutions of distributions for some calculations, decided to use built-ins. With ...
3
votes
2answers
192 views

Using a given colour table with Image?

Context I would like to represent large images with a given colour table. Now, if I use Image ...
27
votes
5answers
2k views

How to efficiently find positions of duplicates?

Is there an efficient way to find the positions of the duplicates in a list? I would like the positions grouped according to duplicated elements. For instance, given ...
11
votes
3answers
522 views

Efficient implementation of a linear complexity measure of binary sequences

For a implementation of testing the quality of random number generators I implemented the NIST test suite in Mathematica based on the nice workbook by Ilja Gerhardt. However I took up the challenge ...
7
votes
2answers
254 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 ...
6
votes
1answer
172 views

Does pass-by-value affect the performance of function calls?

I only have a little coding experience in C, and I remember I was told that pass by reference is more efficient than pass by value since the parameters don't need to be copied. Since there is no pass ...
5
votes
2answers
325 views

Variant of the cutting-stock problem in Mathematica

I'm pretty new to Mathematica and am trying to learn to solve problems in a functional way. The problem I was solving was to list the ways in which I could sum elements from a list (with repetitions), ...
6
votes
2answers
404 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 ...
8
votes
1answer
267 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 ...
2
votes
3answers
442 views

Selecting subsets of a list containing a specific element

I'm trying to generate all the subsets of a set containing a specific element. For example, for the set {1, 2, 3, 4}, with the specific element I want inside each subset being 4, the required answer ...
6
votes
2answers
241 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 ...
9
votes
2answers
302 views

Speed up 4D matrix/array generation

I have to fill a 4D array, whose entries are $\mathrm{sinc}\left[j(a-b)^2+j(c-d)^2-\phi\right]$ for a fixed value of $\phi$ (normally -15) and a fixed value of $j$ (normally about 0.00005). The way ...
4
votes
2answers
214 views

How can this DivisorSigma code be made fast?

Since Project Euler problems are now fair game for questions I have a question of my own. A certain problem* states: For a positive integer n, let σ2(n) be the sum of the squares of its ...
9
votes
4answers
727 views

Passing large list by reference

I have the following problem: I would like to control evaluation of a variable that points to a list. For example, frequently in the code I have functions of the form that are supposed to work on ...
9
votes
5answers
522 views

Counting the population of integers

Suppose that myData is a list of sublists. Each sublist has a length of one or greater and contains any number of replicates of the integers 1, 2, 3, and 4. I ...
13
votes
3answers
380 views

Quick multiple selections from a list

What is the fastest way to make multiple selections from a list? Compiled methods included. For example, here are two methods for selecting a subset, compared:- ...
5
votes
4answers
163 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: ...
6
votes
2answers
740 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 ...
11
votes
2answers
561 views

Vlookup function as Excel in Mathematica

How can we create a Vlookup function, similar to what we have in Excel? So if we have a table a and another b, we could add ...
1
vote
1answer
136 views

Understanding a slowdown associated with wrapping a set of nested Do loops with a ParallelDo expression

I have a set of set of nested Do loops that require data from three different arrays (though the position from each array is fixed per cycle of the outer Do loop). Let's call these arrays: ListA, ...
6
votes
2answers
292 views

Is there a Break[] equivalent for short-circuiting in Table?

I am running Mathematica 7. The following is admittedly a silly example, but suppose that I have a list list whose entries are either ...
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, ...
8
votes
2answers
615 views

Why is Mathematica's expansion of expressions with square roots so slow?

Mathematica expansion of expressions with symbols isn't slow, but with square roots it is very slow. In Maple or Mupad both are fast: with square roots they both take less than a second. Here is an ...
3
votes
3answers
1k views

Definite integral takes a very long time

I'm trying to integrate the following function: $$ r_e=\int_0^z\frac{dz}{\sqrt{\Omega_m(1+z)^3+\Omega_\Lambda}} $$ where $\Omega_\Lambda=1-\Omega_M$ and both $\Omega_\Lambda$ and $\Omega_M$ are real ...
7
votes
1answer
268 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 ...
14
votes
1answer
285 views

Adding three integer sparse matrices is very slow. Adding only two is fast

Adding more than two sparse matrices in one step in Mathematica 9 is very slow (in fact I couldn't even wait for it to finish). Here's an example. Let's generate a large sparse matrix: ...
20
votes
4answers
704 views

Efficient circular buffer?

I wish to create an efficient circular buffer. That is, I wish to keep a fixed length list while appending a new value and dropping the oldest, repeatedly. As we know lists in Mathematica are ...
5
votes
3answers
348 views

Efficiently extracting an array subset given a separate array

I have two arrays. The first array consists of tens of millions of rows and three columns with a string, a number, and a string. ...
8
votes
2answers
251 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: ...
11
votes
4answers
543 views

How to write this without For loop

Suppose I have a few lists of numbers and want to exponentiate element-wise, then sum up everything into a polynomial. For example, if I have ...
0
votes
1answer
253 views

How can I improve my code with efficient mathematica built-in functions?

I have written a module for my assignment like below. V[n + 1] = rk[x[n], y[n] ,z[n], V[n]]; vs = V[n].V[n+1]; V[n + 1] = I want to modify it with efficient ...
1
vote
1answer
246 views

Faster GCD Implementation

Is there any chance to write a faster GCD than the built-in one in Mathematica? @Mr.Wizard has written one in this question (although it's not for this purpose) which is 6 times slower on a 100k ...
30
votes
3answers
1k views

xkcdConvert routines perform slower in Mathematica 9

I am experiencing some rather large performance decreases in Mathematica version 9.0 using the xkcd-styled plotting routines. I had used the xkcdConvert code ...
3
votes
3answers
526 views

Speed optimization

I have a part of a program that I have to repeatedly call many times (about 2000 to 10000 times) ...
4
votes
2answers
271 views

How can I find the lines that go through?

I have a 80 x 80 x 50 dataset. From this dataset, I get some curves. These curves are randomly seeded, by which I mean that their starting points have been chosen randomly. Below, you can find an ...
7
votes
1answer
228 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[] ...
8
votes
3answers
497 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: ...
12
votes
4answers
489 views

Can you recommend an efficient method for finding the least integer satisfying my inequality?

I tried find the minimum make $\frac{1}{2}+\frac{1}{2+\sqrt{2}}+\frac{1}{3 +\sqrt{3}}+...+\frac{1}{n+\sqrt{n}}>15$ Following code is so slow when I use condition ...
2
votes
0answers
253 views

FindRoot - Speed and errors

I am using FindRoot[] to solve a complicated equation. It seems I get the correct answer even though I get errors about an ...
3
votes
1answer
394 views

How to do the polynomial stuff over finite fields extensions fast?

This question is raised from the problem of package FiniteFields being very slow (please, see the corresponding question): I have had an evidence that Mathematica ...
3
votes
2answers
113 views

Is there a faster way to compute the endpoint for a DateRange given the start, step, and length?

Say I am given a date start an increment step (possibly mixed radix) and a desired number of steps ...
14
votes
2answers
421 views

Why does iterating Prime in reverse order require much more time?

Say I would like to display the $10$ greatest primes that are less than $10^5$. I could do the following: ...