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)

9
votes
1answer
146 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 ...
2
votes
0answers
62 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: ...
4
votes
1answer
154 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, ...
0
votes
0answers
49 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
303 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
641 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 ...
4
votes
1answer
95 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 ...
11
votes
2answers
325 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 ...
0
votes
1answer
43 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 ...
1
vote
0answers
47 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
76 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
93 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: ...
4
votes
0answers
112 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 ...
0
votes
1answer
89 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 ...
5
votes
3answers
183 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 ...
8
votes
0answers
88 views

More efficient way to generate lattice graph with edges between visible vertices?

Given a nXn lattice, I need to generate the graph such that there is an edge between two vertices iff they are directly "visible" to each other, i.e., there is no vertex between them. I'm using ...
3
votes
0answers
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 ...
16
votes
1answer
1k views

Numerical integration — Mathematica vs Python (w/ Scipy) performance

I'm about to tackle a problem that involves a lot of (multi-dimensional) numerical integrations and also subsequent optimizations, and so I want to make sure at least the integration step is as fast ...
6
votes
1answer
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 ...
7
votes
2answers
296 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 ...
24
votes
2answers
615 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 ...
3
votes
2answers
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, ...
1
vote
1answer
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: ...
7
votes
0answers
127 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}.$ ...
9
votes
4answers
412 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 ...
9
votes
1answer
221 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 ...
23
votes
1answer
694 views

Efficient lazy weak compositions

In Mathematica all weak k-compositions of integer n can be generated using permutations of integer partitions: ...
19
votes
5answers
675 views

How to delete subsets quickly?

I have some data like this: ...
4
votes
3answers
170 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: ...
10
votes
1answer
95 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 ...
6
votes
2answers
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 ...
1
vote
2answers
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 ...
13
votes
1answer
183 views

MathLink (WSTP) vs LibraryLink performance

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: ...
3
votes
0answers
119 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 ...
18
votes
1answer
650 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 ...
17
votes
3answers
471 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 ...
12
votes
3answers
253 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) & & ...
0
votes
0answers
61 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: ...
1
vote
1answer
124 views
11
votes
4answers
182 views

Faster position-based duplicate removal in a ragged array?

Take a ragged 2D array: rArray={{4, 2, 1, 0}, {2, 2}, {0, 0, 3, 3, 3}, {4, 0}, {3, 4}} I need to "deduplicate" this, where a duplicate is any element of a ...
14
votes
2answers
247 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 ...
6
votes
0answers
77 views

Fast Partition of list at depth 2

I am looking to partition a list up at a certain depth level. Following code should demonstrate what I mean: ...
2
votes
1answer
98 views

Clean & efficient bounding box non-intersection test

I would like a quick test to tell me that segments $ab$ and $cd$ cannot intersect in the plane. If the maximum $x$-coordinate of $a$ and $b$ is less than the minimum $x$-coordinate of $c$ and $d$, ...
0
votes
0answers
63 views

Hook Length Formula and Ferrer's Diagram

So, I am new to using Mathematica, and trying to write some code that needs to use hook length of each cell in Ferrer's diagram of a partition. I first thought of using for loops to manually count ...
0
votes
0answers
43 views

Is there a way to pre-calculate a slow animation and then run easily with no lag? [duplicate]

I have an animation of a triple pendulum on a moving base, however even after trying to change the whole coding to make it faster by using module and all it is still pretty bad as I need it to be ...
28
votes
1answer
323 views

Solve slower in versions 9 and 10

With some systems of equations, Solve is much slower in versions 9 and 10 than in earlier versions, apparently because it is doing more simplification of the ...
2
votes
1answer
115 views

Simplifying Expressions for FindMinimum

I am currently trying to apply the FindMinimum function to find the lowest energy of an expression. My current expression as of now is relatively complicated. It is the summation of 48 similar terms, ...
2
votes
2answers
161 views

Efficient List of Lists

I'm running some analysis that requires the creation, row by row, of a large list of lists. The big list is called "Nodes" and it will ultimately contain hundreds of thousands of rows. The first few ...
1
vote
1answer
98 views

Image manipulation is too slow

When I hit shift+enter at a document, I must wait for a long time. But AbsoluteTiming function indicates it just spent under 1 second. as follows. I think it shows that only 0.24sec took to ...
4
votes
1answer
78 views

Fishing for monomials in a nested or partially factored polynomial stream

I have a problem where I'd like to be able to take a multivariate polynomial whose variables are nonscalar and is not written explicitly as a sum of it's nonzero monomial terms and determine which ...