# 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.

305 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 ...
642 views

### In a list of points, how to efficiently delete points which are close to other points?

Consider a list of points: pts = Partition[RandomReal[1, 10000], 2]; ListPlot[pts] I'd like to delete points so that the minimum distance between two points is ...
177 views

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

Bug introduced in 8.0 and fixed in 10.0 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 ...
98 views

### Speed up rendering of ContourPlot3D object

Please can you advise me how to speed up the rendering of the following two planes? When I click and drag to rotate the plot, its movement is quite jerky. Presumably it is working harder than I need ...
635 views

### Efficiently generating 2-D Gaussian random fields on the sphere

Context A couple of years ago I posted this question for an efficient code to generate an n-D Gaussian random field (sometimes called processes in other fields of research), which has applications in ...
44 views

### Plotting precise intersections involving singular functions

I have pseudo-elliptic functions defined on a parallelogram within $\mathbb{C}$ and I would like to clearly highlight the path within this parallelogram which satisfies the real part of my pseudo-...
186 views

### How to populate a large sparse array efficiently

I am trying to populate a large 2D sparse array and sum up all the columns at the end. I tried generating a set of rules and then generate a matrix using SparseArray or break the data into chunks but ...
49 views

### Evaluation of likelihood very slow

I am trying to implement a simple statistical model for the first time in Mathematica. My model is made my one random variable which is the sum of background and signal. Background is an exponential ...
78 views

### Closing over array values makes Compile choke with CompilationTarget -> “C”

When I attempt to Compile a closure which contains an array of the appropriate type, it works just fine with ...
95 views

### Recursive calculation is very slow

I've got a recursive definition of Arcs that shares equal length and a several other dependencies to each respective predecessor. (Imagine a book being bent.) I formulated all necessary things here: ...
91 views

### Optimization of a voxel projection function

I implemented a function to check if a voxel (3D pixel) is well projected on the space of a image (black and white silhouette) through a perspective camera. The function does the work but It need more ...
96 views

### How to speed up summing a large number of vectors

First let me explain what I am trying to do in my code. Assume that I send a very short chirp (2,000 ms long) into a domain filled with 20,000 particles randomly scattered in the domain. The ...
133 views

### Strategy to build efficiently an image using parts of many images

I have about 1000 images of 1000px $\times$ 1000px and a list which contains the coordinates of the pixels to select, for each of the 1000 images. Each pixel is used once, so the final image is a full ...
298 views

### How can this resampling code be made faster?

Suppose I have a matrix: sbt = RandomInteger[{0, 2^16 - 1}, {5202, 3465}]; It represents an image and I would like to do "pixel binning" on it, where I take non-...
205 views

### Slightly increasing complexity of function increases calculation time by magnitudes

I am trying to calculate an impedance value from voltage and current functions. The calculation method is based on how an actual measurement device performs it. I have two different current function, ...
679 views

### How to delete subsets quickly?

I have some data like this: ...
3k views

### Efficiently generating n-D Gaussian random fields

I am interested in an efficient code to generate an $n$-D Gaussian random field (sometimes called processes in other fields of research), which has applications in cosmology. Attempt I wrote the ...
382 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}$ ...
55 views

### NIntegrate option for FourierSeries

I would like to use FourierSeries but it is very slow. My guess is that it is because it uses symbolic integration: ...
414 views

### Looping through 3 million elements of a list to 'Set' values is slow

I am looping through the 3 million elements of a time-series (v1, o1) and corresponding values (v2, o2). v1 contains a uniformly increasing timestamp (e.g. 1 to 3,000,000 in increments of 1) o1 ...
128 views

### Is this a reliable method for getting a list of very large primes?

As noted here, both PrimePi and Prime are documented as having their limits somwhere around $10^{15}.$ ...
232 views

### Finding planetary conjunctions with Mathematica (project-level) [closed]

Summary: What's an efficient way of finding planetary conjuctions with Mathematica using the SPICE kernels, and without using AstronomicalData (which I believe is less efficient). NASA publishes ...
5k views

### File-backed lists/variables for handling large data

Background Currently I am working with some large data (most of it generated by Mathematica itself). I usually find it a hassle to do this. For example, I just exported a large amount of data to ...
215 views

### Is there a PrimeQ whose accuracy guarantee you can adjust?

Say I have a list of a million integers each with a million digits, and I want a crude sieve to see which have a chance at being prime. Mathematica has a PrimeQ function, which appears to be slow ...
190 views

Has anyone benchmarked LibraryLink vs MathLink when passing large arrays? Specifically I would like to know how performance compares when using the following to pass and return a large array: ...
708 views

### Efficient lazy weak compositions

In Mathematica all weak k-compositions of integer n can be generated using permutations of integer partitions: ...
173 views

### My evaluation is taking a lot of time

I was evaluating the roots of a transcendental equation, when I noticed that Mathematica never finishes but stays in the running state. The following is the code that I was using: ...
116 views

### Linear interpolation over 2 variables [duplicate]

I'm tuning a function to data over 4-5 variables using Differential Evolution. The objective function calls frequently interpolation function. I came to a point, where compiling the objective function ...
262 views

### Modifying the elements of a list according to a given probability

Let list be a list of lists, the elements of which are random integers in the interval $[1,c]$. Each element $i$ has a probability ...
2k views

### Can Mathematica's random number generation be improved?

Performance on Random Number generation is intolerable. Whenever you need many deviates from the same distribution but with different parameters it takes forever. Here is a thread on Poisson ...
96 views

### How to speed up the evaluation of a function with a large LeafCount but many repeated sub-functions for use in NIntegrate

General Context Apparently, the memory leak in NIntegrate is unavoidable pre-MMA-10.2. I have some (two-dimensional) numerical integration to do on a complicated ...
190 views

### How can I generate a $2n$-point moving maximum of a list as efficiently as MaxFilter?

This is based on the prior question, Moving maximum function?, where @rasher provided two winning solutions (i.e. clearly the most efficient): MaxFilter[list, 1] ...
2k views

### Happy 2K prime question

This being the Q number 2K in the site, and this being the day we got the confirmation of mathematica.se graduating soon, I think a celebration question is in order. So... What is the fastest way to ...
101 views

### How can I speed up this code with 4 variables sum

I have to make a sum over 4 variables. My code is very very slow. I want to know how to speed up this code. This problem is related to but different from one previous problem. Any help or suggestion ...
210 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 ...
507 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 [...
473 views

### Improving speed of code computing number of nonrepeating partitions

I need to answer the following for a number of parameters: How many ways can the integer $k$ be written as a sum of $n$ different integers ranging from $1$ to $m$? My initial attempt was the ...
91 views

### Calculating eigensystem of a matrix containing very small numbers [duplicate]

I'm dealing with a matrix containing very small numbers like 10^(-370). When I compute the eigensystem of my matrix, it takes too much time, though the matrix size is not large. Can anyone explain ...
251 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 ...
123 views

### Importing large number of frames form Video - improved method?

I am trying to work with large numbers of frames from a video (30000+). I see there are a number of answers out there, but I find none of them satisfying - see below for details. I understand from ...
237 views

### Speed up MinimalPolynomial

My Mathematica code runs slowly MinimalPolynomial[Sqrt[2] + Sqrt[3]+ Sqrt[5]+ Sqrt[7]+ Sqrt[11]+ Sqrt[13], x] runs slowly, but the Maple version ...
73 views

### Performance of Extract-Transform operations

I'm doing simple data analysis, where I load some "date-time" data from a file. The data is a simple list, but there are 41,000 values in total. Here's a short sample: ...
656 views

### Use Compile to improve performance

I've recently discovered Compile as a way to optimize my code. I wrote a program to learn something new about Compile. I have a ...
334 views

### How can I speed up my compiled RBF interpolating function?

Overview I have a function that acts like the Interpolation on sparse $n$-dimensional data using a simple implementation of RBF interpolation method. I want my ...
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) & & ...
281 views

### Using a Mathematica index as a DiscreteVariable in NDSolve when solving a coupled set of ordinary differential equations

Context Since the explanation below of the problem to be solved is lengthy, let me preamble this by saying that I have code that works to solve the problem, but I don't know whether (1) it's ...
65 views

### Simplifying sums of rational trigonometric functions

After simplifying the trigonometric functions as advised before (cf. Simplifying Expressions for FindMinimum), I obtained a sum of 62 similar expressions, the first three as in the below: ...
125 views

...