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.
6
votes
2answers
206 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
226 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
140 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 ...
2
votes
1answer
95 views
6
votes
2answers
181 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
202 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 ...
0
votes
0answers
108 views
A simple For loop visibly slowing down. Advice to fix?
I have run into a unique problem. I am going to try and describe my code and what is happening as understandably as possible.
I have a For loop that does the same set of calculations over and over ...
3
votes
2answers
139 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 ...
8
votes
4answers
291 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 ...
8
votes
5answers
336 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
270 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
124 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
323 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 ...
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 ...
1
vote
1answer
97 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, ...
5
votes
2answers
144 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 ...
6
votes
2answers
363 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
312 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
400 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 ...
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 ...
15
votes
1answer
201 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:
...
18
votes
4answers
497 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
212 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
191 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:
...
10
votes
4answers
490 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
189 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
0answers
150 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 ...
25
votes
3answers
906 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
405 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
223 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
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[] ...
5
votes
3answers
376 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:
...
11
votes
4answers
413 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
...
0
votes
0answers
138 views
Best Parallel Kernel Configuration
I wrote a code snippet which counts prime numbers in any given interval because the built-in PrimePihas a limited bounds.
...
2
votes
0answers
139 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
155 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
98 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 ...
13
votes
2answers
288 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:
...
2
votes
1answer
236 views
Reading in Huge Text Files
I would like to read in really large text files (about 2GB) into Mathematica. The structure of the file is such that the first row and the first column are text i.e strings. Rest is all numbers. Is it ...
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
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 ...
8
votes
3answers
130 views
How can I use the overhang argument to Partition to avoid using PadLeft and PadRight here?
Say I have a list of values v = {v1, v2, v3, ... }. I need to partition these values to perform operations with a sliding window (think ...
2
votes
1answer
113 views
Computing closest set of points to each point in large set — running out of memory (arrays unpacking)
I have a large set of points in 3D and I'm trying to identify all the points that lie within a certain distance of each point. Then using this data store the vector between the pairs of points.
I ...
3
votes
2answers
309 views
Fast Trial Division
I'm going to write a custom Trial Division primality test. I know that PrimeQ will first try trial division and then switches to ...
4
votes
1answer
193 views
speed up iteration with conditionals plus optimize memory usage
Given list1 and list2 whose elements are vectors of a certain (fixed) dimension, I am interested in the behaviour of a scalar ...
4
votes
3answers
174 views
Extracting Pareto elements from a list
How can I extract the Pareto elements from a list?
For example, for that list:
...
5
votes
2answers
229 views
Fast Karatsuba Squaring, how to?
I want to improve timing of squaring for big numbers.
So I asked What is the fastest way to split a number into parts and What is the most efficient way to add zeros to end of a number with several ...
16
votes
3answers
611 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:
...
9
votes
1answer
306 views
FiniteFields package is very slow. Any fast substitute for Mathematica?
I want to compute the inverse of matrix, say with dimensions $100 \times 100$, defined over a large finite field extension such as $GF(2^{120})$. I am using the package FiniteFields, but Mathematica's ...
1
vote
0answers
165 views
Vector computation instead of iterating over a list
I am 'Map'ping over a list. I would like to do the computation in one single iteration. Is it possible? Can 'Apply' be used here? In what way is it different from 'Map' especially in this case?
...

