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)

2
votes
1answer
64 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
37 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
40 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 ...
17
votes
1answer
144 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 ...
0
votes
0answers
44 views

Why is kernel quitting during Manipulate and CurrentImage

I have a Manipulate that captures a camera image using CurrentImage, does some (fairly complicated) processing on the image, and ...
1
vote
1answer
60 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
140 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
74 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 ...
2
votes
0answers
31 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 ...
19
votes
2answers
322 views

Speeding up the built-in Rudin-Shapiro and Thue-Morse sequence functions

Version 10.2 introduced two well-studied sequences as functions: the (Golay-)Rudin-Shapiro sequence (RudinShapiro[]) and the (Prouhet-)Thue-Morse sequence (...
2
votes
3answers
108 views

Integrate over Piecewise function efficiently

I have this Piecewise function: ...
3
votes
1answer
62 views

The fastest way of raising indices of high rank tensor

I have some high dimensional high rank tensors, let's say $$F_{ijkl}$$ and I need to find $$F^{abcd}=g^{ai}g^{bj}g^{ck}g^{dl}F_{ijkl}.$$ Here $g^{ij}$ is the contravariant metric. Simple summation ...
4
votes
0answers
116 views

CompilationTarget to C takes long compilation time

Consider I have a function, with one version that compiled to the virtual machine and one that compiled to C: ...
7
votes
1answer
84 views

How to compile procedural program effectively

Consider these four function in different form: ...
1
vote
1answer
52 views

Adding another looping element with a list

I have code that is looping on to elements and is working shown below ...
1
vote
2answers
76 views

Replace For-Loop to speed up code

I need to speed up this code ...
5
votes
1answer
107 views

Composition vs serial assignment, which performs better?

I have a giant block of code that manipulations an expression in many different ways. Currently it looks like ...
6
votes
0answers
242 views

Visualization of traces of moving objects

An example movie which is made with the listed mathematica code below shows traces of moving objects: https://youtu.be/8jN2uuSHCEg (Melting ...) http://youtu.be/5ET62846C-I (Crystallisation ...) ...
3
votes
2answers
108 views

Image smoothing with own smoothing function

I have thousands images similar to the following one: This image was produced with the following color table: ...
0
votes
0answers
48 views

Compilable function with the functionality of SingularValueList

I'm doing something that involves finding the singular values of a lot of matrices. Basically I need to find singular values of $n$ matrices of dimension $n\times n$, where $n$ is as large as ...
19
votes
1answer
327 views

Faster binary Hamming weight for big integers?

While working on an answer to Count the sequences in an array I found that DigitCount was the bottleneck in my code when used as ...
2
votes
1answer
57 views

Increasing Plot3D speed when plotted function is Eigenvalue

This is probably a really simple fix, but I'm fairly new to Mathematica and am not sure how to do this properly. I know that trying to plot eigenvalues directly can be computationally expensive, so ...
1
vote
0answers
58 views

Improve speed of evaluating a sum over four indices

I am trying to implement the Fox-H function with several variables as sum of residues. I have arrived at the following function; ...
3
votes
0answers
47 views

Why is the Max function so slow when operating on lists of dates?

Example: dates = DateRange[DateObject[], DatePlus[DateObject[], 15]]; First[AbsoluteTiming[Max[dates]]] (* 21.364247 *) As a workaround, ...
2
votes
2answers
99 views

How to Optimize Solution? (Project Euler #14) [duplicate]

Link to Question: https://projecteuler.net/problem=14 I began by defining the following functions below. ...
6
votes
4answers
711 views

Finding number of pixels with certain RGB color in an image

I have the following image: To determine the number of black pixels (nBlackPixels) I use: ...
0
votes
0answers
177 views

Why does my calculation of a probability take too much time?

I am calculating the value of the following function for given argument values: ...
5
votes
3answers
77 views

Conditional subset generation

I am trying to accomplish the following task: Given a list of coordinates of n points in the plane, I need to find subsets of all points that share the same ...
0
votes
0answers
34 views

Numerical minimization hangs at some steps

Let me briefly explain what I observe and cannot really explain by myself. I occasionally use NMinimize to find optimal parameter sets and sometimes it happens that ...
2
votes
2answers
160 views

Optimize evaluation time for a Quantum Walk simulation

Here's the problem. I'm trying to simulate a noisy Quantum Walk to obtain results similar to these, experimentally obtained. I wrote my code, and I get results which are in accordance to what I ...
14
votes
3answers
396 views

Solve a trig equation system

Background of the problem In the same plane, P is a fixed point, A,B,C are moving point, PA=a, PB=b, PC=c, find the maximize perimeter of △ABC. let ∠BPC=A, ∠CPA=B, ∠APB=2*Pi-A-B, then the ...
9
votes
0answers
97 views

Mathematica is getting slow when repeatedly calling an external program

I found a strange phenomenon that Mathematica is getting slow when I repeatedly called an external program by using ReadList. For example, the following code will display a significant increase of CPU ...
2
votes
0answers
69 views

Pros/Cons to updating in place

There are often times when I have to update a list. The following is an example: ...
3
votes
1answer
183 views

How can I understand this short example?

First launch kernels LaunchKernels[4] {KernelObject[5, "local"], KernelObject[6, "local"], KernelObject[7, "local"], KernelObject[8, "local"]} Then I calculate ...
6
votes
1answer
88 views

Slow-down encountered for RandomChoice in large lists

I was wondering if anyone could help me to speed up this code snippet which creates simple (sparse) networks by preferential attachment. The algorithm adds a vertex and an edge each time interval. ...
15
votes
2answers
151 views

HDF5 importing large arrays

I have some computations that output a 300,000,000 element array of long unsigned ints, so about 2.4GB, in an h5 file. Mathematica handles files south of 2GB fine, albeit taking very long to import ...
1
vote
1answer
87 views

Unexpected inefficiency when calculating matrix product by Dot

There is an unexpected inefficiency when I calculate matrix product by Dot. The only relevant part of my code is presented as follows: ...
14
votes
3answers
637 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 ...
3
votes
1answer
85 views

Solve an eigensystem faster and eliminate Root[…] from the solution

I have a symbolic, 2-parameter, 8x8 matrix to solve, but Mathematica takes something like 20 minutes to solve it and returns a very large output containing expressions of the form ...
2
votes
1answer
85 views

Memory usage optimization for a large Association/SparseArray

Reformulating my question in simpler terms. Take two lists list1 = RandomInteger[{1, 100000000}, 25000000]; list2 = RandomReal[{1, 10}, 25000000]; Make an ...
6
votes
2answers
168 views

Speed and the MapThread function

Can anyone help explain why MapThread is so slow in this simple case? ...
2
votes
0answers
88 views

When does Mathematica automatically use dispatch tables for function evaluation? [closed]

I've read the documentation for Dispatch, and it says: Lists of rules produced by assignments made with = and := are automatically optimized with dispatch ...
5
votes
2answers
144 views

Speed up image coloring with ReplaceImageValue

I have about 200000 rgb-images of such format (2160*577 pixels): I want to colorize in each image some parts in white color: ...
2
votes
1answer
150 views

How to determine whether an expression can act as a “good” function?

It's not easy to see whether an expression can act as a "good" function. A good function refers to an operater that transforms symbolic entities like 1+x^2 or ...
1
vote
1answer
59 views

How to perform multi-columns selection and/or !MemberQ on large lists with speed?

This question (2 subquestions) is connected to How to speed up Select with MemberQ for large lists? Q1: I would like to have an alternate version of the form below to do a multi-columns selection on ...
1
vote
0answers
54 views

Efficient way for solving a matrix equation

I am looking for a more effcient method to perform the following analysis. I did my best, but running the following code on my machine takes 9.766s. I need to perform this analysis several times. ...
6
votes
1answer
91 views

SameQ for unsorted lists

I have to compare two or more lists and check if they are the same, but their elements can be in different position. For instance ...
5
votes
2answers
124 views

How to speed up Select with MemberQ for large lists?

I have two large lists: list1 and list2. I would like to select elements of list1 only if they appear in list2. Since the list1 has dimensions of (335000, 2) and list2 : (122000, 4), it's taking a ...
2
votes
1answer
66 views

Performance of a bootstrap - time of computation not linear with number of iterations

I'm doing a bootstrap based on this thread with the following code (file.txt here): ...
2
votes
1answer
97 views

Compiling a summation

I have a sum over Legendre polynomials which I'm trying to speed up. I've come across the suggestion of using Compile to speed up code in previous answers around ...