# Tagged Questions

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.

663 views

### How to find all the positions of max value of a list efficiently?

For Example, list=RandomInteger[{1,100},2000]. Yes,I know Position[list,Max[list]] can do. But it's based on pattern matching!...
695 views

### Nearest neighbor search in n-dimensions

Suppose a list of items such as: L1 = RandomReal[{0, 100}, 1000]; I need to find the position of each k-neighbor in the original list. And very quickly if ...
3k views

### Division by zero is slow

Suppose I have two arrays of data, which I want to divide: ...
947 views

### Speeding up random walk for many particles

I am trying to speed up this code for many particles to take a random walk. I'm not sure why it is so slow for such a simple task. I got a few hints from colleagues to reduce the precision of the ...
470 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:- ...
520 views

### Better way to test that a matrix is upper-triangular?

Today, I got to thinking about how to test that a matrix was upper-triangular. So I had a try at it. Algorithm $\quad \quad a_{ij}=0$ Or $i \leq j$ $\Rightarrow$ $True$ My solution is ...
464 views

### Given a large binary matrix, find the largest submatrix containing non-zero elements

I'm looking for a way to reduce a binary matrix containing zeros at some positions into a matrix that contains no zeros by deleting rows and columns of the original matrix until only non-zero values ...
739 views

### Space-efficient null space of sparse array

I have a roughly 100,000 × 3,000 matrix (as a SparseArray) that I'd like to find the kernel (null space) of. It has about 500,000 nonzero entries, all -1 or 1. ...
440 views

### Removing calls to MainEvalute when using inlined compiled closures

This question is tightly related to the answer Shaving the last 50 ms off NMinimize. There @OleksandR shows how inlined closures can be used to eliminate calls to ...
292 views

### Fast Selections Testing on Inequality

Is there a functional idiom for fast selections testing on inequality? For example, finding values from list a & b where the key is less than or equal to a value, in this case 4 or 9. An ...
252 views

### Fastest way to sum the upper triangle

I feel like this is an recurring question: if there's a symmetric matrix whose diagonal is not all 0, how could I get the sum of the part of it that's above the diagonal as fast as possible? Small ...
1k views

### How to speed up text recognition

I have a large corpus of text to analyse. I've managed to automate splitting it into individual lines because I've found that each line in this relatively old scanned document needs to be dealt with ...
1k views

### Efficient conditional Mean[] on a large data set

Consider a list, AK6, that has 382 sublists of length varying from 2500 to 3000. Each "subsublist" is as such : ...
370 views

### How to evaluate the second argument of Cases only once

I'd like to find a fast but readable way to achieve the following: ...
1k views

### Function does not compile with Greater in it

I have to speed up a matrix-calculation and would like to use Compile for it, though it fails and produces an unknown error message: ...
510 views

### Collatz optimization

I'm VERY new to Mathematica programming (and by new I mean two days), and was solving Project Euler question 12, which states: Which starting number, under one million, produces the longest [...
373 views

A friend of mine showed me this example, it's a test comparing LengthWhile to a self-made lengthwhile written in a direct and ...
528 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 ...
484 views

### With versus Function

I've seen people compare Block with Module and With as scoping constructs, and try to figure ...
564 views

### Particles bouncing in a 3D box

I made this Manipulate code which shows a pack of particles randomly moving inside a box. It has a huge performance issue. What should be the best way in doing "...
597 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: ...
692 views

### Considerations when determining efficiency of Mathematica code

I have two segments of code that do the same thing and I want to determine the which is more efficient. What are the considerations when determining efficiency of Mathematica code? Correctness/...
1k views

### What compilers are “suitable” for CompilationTarget->“C”?

The documentation for CompilationTarget says, "if you do not have a suitable C compiler then you will not be able to set CompilationTarget to C". Well at this time, I don't have a compiler installed ...
1k views

### What is the most efficient way to map a linked list?

In order to work with lists effectively using common recursive patterns from functional programming, it's often most convenient to worked with the "linked list" representation, that looks like this: <...
309 views

### Why does partitioning increase Dot execution speed?

I have noticed that multiplying a list of matrices can be significantly sped up by partitioning the list, calculating the product of the partitions matrices, and then multiplying the results. ...
394 views

### Are there rules of thumb for knowing when RandomVariate is more efficient than RandomReal?

Please consider the following: From a fresh Mathematica kernel, RandomVariate is more efficient for NormalDistribution but <...
230 views

### Unexpected slowness checking for empty list?

I thought I had a firm understanding of the differences between Equal and SameQ, but this has me puzzled: ...
844 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 ...
353 views

### Why is NeighborhoodGraph so slow?

To get neighboring vertices I first tried: gg = GridGraph[{10, 10, 10, 10}]; VertexList[NeighborhoodGraph[gg, 1, 1]] // AbsoluteTiming {5.539308, {1, 2, 11, ...
244 views

### How do you determine the optimal autocompilation length on your system

When you pack lists there is an overhead therefore packing a list with, say, 2 elements is likely to cost more than you get back in efficiency. Mathematica has default list lengths for which functions ...
387 views

### How to speed up the plotting of B-spline curve?

Three months ago, I asked a quesion about B-Spline basis function here, Today, I used this function to plot B-spline curve. The definition of $N_{i,p}$ ...
312 views

### Wiggly and imprecise animated gif output

Trying to make animated gif of zooming into famous Ford circles. With the code ...
553 views

### the more effective method to find 21 digits armstrong number

In recreational number theory, a narcissistic number (also known as a pluperfect digital invariant (PPDI), an Armstrong number(after Michael F. Armstrong) or a plus perfect number) is a number that is ...
335 views

### Parallelization of distinct array write access from subkernels

I'm working on an implementation of a multivariate FFT, which is (or at least should be) highly parallelizable due to the row-column-algorithm. However, i can't figure out how to implement that. The ...
356 views

### Memoization of Rounded inputs

Memoization is a technique for improving performance by having a function remember its previous arguments. For example, f[x_]:=f[x]=mySlowFunction[x] will be ...
2k views

### 3D Random Walk with Periodic Boundary Conditions

I am working on a 3D random walk with periodic boundary conditions and have written a program that will do this for me, but it is extremely slow. Does anyone have any suggestions on how I could speed ...
475 views

### How would you efficiently implement BinListsBy?

Let's say I have a list like this: data = {{21, "Alice"}, {27, "Bob"}, {22, "Charles"}, {34, "Dora"}, {75, "Eliza"}} containing ages and names of persons. What I ...
556 views

### How to speed up Colorize

I have a list of about 500 images similar to the following: A = Import["http://i.stack.imgur.com/1bV4O.png"] ...
357 views

### Taking Top K Occurrences

Having millions of numbers in a list, I want to take top K occurrences. MMA Commonest is very slow so I write my own versions: <...
435 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: ...
5k views

### How to calculate the mode of a probability distribution

I was wondering if there is any command to get the mode of the probability distributions. Calculating manually, the mode is (alpha-1)/(alpha+beta-2) While the mean, median or variance of the beta ...
262 views

### Fractal basins of attraction in a Magnetic Pendulum

I am trying to write a Mathematica program that realizes a graphical approximation of the basins of attraction in a Magnetic pendulum subject to friction and gravity, in which the three magnets are ...
256 views

### How to implement a more efficient inverse triangular recursion?

Consider the following inverse triangular formula \left( \begin{array}{ccccc} & & & & N_{i-p,p}\left(u_0\right) \\ & & N_{i-2,2}\left(u_0\right) & & ...
202 views

### Faster trimming of leading/trailing zeroes in rectangular array

Given a (large) rectangular array, e.g. (small for example case): ...
212 views

### why MeanFilter is nearly 100 times slower than MovingAverage for 1D array?

I used to average over my data with MeanFilter before I learn about MovingAverage and thought that these two functions are similar except that MeanFilter is more powerful for its ability to deal with ...
645 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 ...
308 views

### Efficient conversion between two partition representations

I have a list of distinct expressions. We can represent a partitioning of list in two ways: As a list of sublists ...
581 views

### Riffle and Partition

At some point I noticed that I was using Riffle and Partition together a lot. I would do things like ...
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 ...