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 (4)

17
votes
7answers
1k views

Integers which are the sum of both two and three consecutive squares

This is a math problem I came across the other day: $365$ can be written as a sum of two and also three consecutive perfect squares: $$365=14^2+13^2=12^2+11^2+10^2$$ What is the next number with ...
6
votes
2answers
249 views

Wolfram Language symbols by their Ranks

I'd like to get the top 50, the bottom 50 and an ordered list of all Wolfram Language symbols based on their "Ranks". With ...
0
votes
1answer
136 views
9
votes
3answers
270 views

How to speed up auxilary DoolittleDecomposite function?

The Doolittle Decomposition algorithm as below: $$A=LU$$ where $A= \begin{pmatrix} a_{11} & a_{12} & \cdots a_{1n}\\ a_{21} & a_{22} & \cdots a_{2n}\\ \vdots & \vdots & \vdots ...
4
votes
2answers
100 views

Ordering and summing elements of a list based on their indexes

Suppose I have a vector. I assign to every vector index a four-element array, in such a way that index i corresponds to ...
6
votes
1answer
365 views

Speeding up the non-negativity algorithm for multilinear function with interval probabilities?

The algorithm is a branch-and-bound algorithm that calculates the non-negativity of a multilinear function with interval probabilities. The lines 9-11 has an optimization that is explained on the ...
30
votes
4answers
4k 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 ...
3
votes
0answers
67 views

Improving the speed of an optimization algorithm via compilation and parallellization

I have written an implementation of eight variants of the PSO-algorithm and I want to test them over a set of test functions. I am using parallelization as it improves the speed a lot and I have also, ...
4
votes
1answer
94 views

Huge overhead in RandomPoint[ImplicitRegion]

Commenting answers of this question @JasonB noticed the big overhead of RandomPoint ...
6
votes
1answer
97 views

Speed up the auxiliary function magicSquare when $n$ is doubly-even

Today, I saw a question about magic square (please see here). When $n$ is doubly-even, namely for Mod[n,4]==0 ...
2
votes
0answers
60 views

Follow-up to “How to differentiate formally?”: Efficiency concern

In link to "how to differentiate formally?" and particularly to the answer by @Jens, I want to do something like this: ...
6
votes
1answer
666 views
1
vote
1answer
78 views

Increase Performance in the Manipulate Command

The code below pretty much looks like and does what I want it to. However, it is sluggish. I realize I have many calculations, and perhaps this is the best I can hope for. But if anybody has any idea ...
4
votes
1answer
111 views

Fast way to get edge-list of graph in terms of vertex indices (not vertex names)

I am looking for the fastest way to achieve the following: Given a Graph, retrieve its edge list in terms of vertex indices (not actual vertices). For example, ...
1
vote
1answer
64 views

How to increase time-effeciency of plotting complicated function?

A newbie here: I am trying to use ContourPlot to plot Intensity in a strongly focus laser beam. However this intensity is a function of not so trivial integrals, so ...
2
votes
2answers
270 views

Good coding for a sequence of derivatives

People on this site keep telling me that good MMa practice avoids the use of loops. The code below calculates the same Table of derivatives two different ways, ...
0
votes
1answer
158 views

Efficiency problem with least square algorithm (Error in both variables)

I was informing myself about the least square algorithms with errors in x and y. I found this post and the top answer wasn't ...
4
votes
1answer
196 views

Select elements of list

I have a list of lists list={l1,l2,l3,l4,...}; and two elements e1,e2. I want to select from lists ...
14
votes
4answers
284 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 ...
11
votes
3answers
516 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"] ...
13
votes
4answers
361 views
7
votes
4answers
385 views

Speed up Schwarz-Christoffel mapping

I would like to ask your advice how to speed up Contour and Parametric plots in the following example. Let as start by defining a function ...
7
votes
1answer
83 views

fast complement with tolerance between large lists

I want a fast algorithm to get a list of elements to be kicked out of another list. But the kick out criterion is not the exact equivalence, however has a tolerance. Any two elements close enough are ...
3
votes
0answers
58 views

Extending Bresenham's algorithm for subpixel rendering

Original Bresenham's line drawing algorithm it limited to drawing lines between two integer pixel positions and does not allow subpixel rendering. However with my reformulation of this algorithm as a ...
16
votes
2answers
275 views

How to implement Bresenham’s algorithm as a system of equations?

Bresenham's line drawing algorithm is usually implemented via loops. But in Mathematica we can take advantage of its ability to solve Diophantine equations. From educational viewpoint it is quite ...
14
votes
6answers
1k views

Generating an Ulam spiral

An Ulam Spiral is quite an interesting construction, revealing unexpected features in the distribution of primes. Here is a related topic with one answer by Pinguin Dirk, who has provided one ...
21
votes
1answer
325 views

Progress bar for FullSimplify

Sometimes FullSimplify takes a really long time and it is not really clear if it makes sense to wait another half an hour in hopes for it to finish, or if it will ...
1
vote
0answers
73 views

General suggestions to accelerate Simplify and FullSimplify

To my understanding, the function Simplify[] and FullSimplify[] work by applying a series of built-in transformation rules to ...
4
votes
0answers
97 views

Eigenvalues FEAST method - performance is very variable

I am running the FEAST method option for Eigenvalues on sparse matrices of dimension around 500,000. I look for about 50 ...
5
votes
1answer
87 views

Fast 1D BinCounts Alternative

I have lots of data which looks like this example: ...
7
votes
1answer
123 views

Speeding Up RandomVariate

Consider the following code where we first take 1000 independent samples from a Poisson distribution, and then take 1000 independent samples from 1000 different Poisson distributions: ...
9
votes
1answer
123 views

Sorting eigenvectors according to its projection

The problem I'm trying to calculate the eigenvalues and eigenvectors of a matrix that depends on a parameter x. As x changes, I ...
0
votes
0answers
39 views

Number triangle programming challenge, separate columns

I am about to enter this number triangle https://oeis.org/draft/A262964 in the oeis, but I don't know how to program it properly. This is partly a mathematics question so I could ask it at math se ...
4
votes
2answers
170 views

PixelValues along a straight Line [duplicate]

UPDATE to the question I have reformulated the question in order to make it distinct from the one suggested as a duplicate: "Line Intensity Profile From Image." The key difference is that I'm ...
17
votes
3answers
320 views

sudden increase in timing when summing over 250 entries

I see a sudden increase of Timing by a factor of thousands when I sum over 250 elements of a matrix rather than over 249. So for instance, this table contains sums ...
2
votes
0answers
64 views

Mathematica computes too slowly at the start of a session [closed]

Everyone, I'm a Mathematica new user. I find it a problem that, when I start a new Mathematica session, it executes too slowly for the first command (say, "1+1", it may take a few seconds before it ...
2
votes
0answers
59 views

Why is this function taking so much more time when its argument reaches 250? [duplicate]

I am observing something which I do not understand with the following function function: ...
11
votes
2answers
303 views

How to make geometric intersection faster for geographic borders?

I have developed a solution to identify geographic regions bordering a region in this post (94723). Can this functionality be made to execute any faster? Find which entities in a set border other ...
32
votes
5answers
7k views

Finding Local Minima / Maxima in Noisy Data

I'm trying to find local minima / maxima in noisy data, consisting of data values taken at certain time intervals. Ideally, the function should take a pair of lists (one containing time values and one ...
0
votes
0answers
42 views

NIntegrate within NMinimize - how to optimize the code?

How I can optimize these numerical calculations? Most annoying is encountering instabilities (numerical errors). I have a predefined function: ...
12
votes
2answers
256 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 ...
11
votes
4answers
629 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 ...
7
votes
1answer
170 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 ...
4
votes
1answer
76 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 ...
24
votes
2answers
579 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 ...
0
votes
1answer
42 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 ...
5
votes
4answers
173 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 ...
1
vote
0answers
44 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 ...
6
votes
1answer
71 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 ...
1
vote
1answer
90 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: ...