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
0answers
27 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
1answer
272 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
105 views

Integrate over Piecewise function efficiently

I have this Piecewise function: ...
3
votes
1answer
61 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
81 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
106 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
107 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
319 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
98 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
708 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
176 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
33 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
395 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
96 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
68 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
84 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
636 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
84 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
83 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
166 views

Speed and the MapThread function

Can anyone help explain why MapThread is so slow in this simple case? ...
2
votes
0answers
87 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
149 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
64 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
96 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 ...
0
votes
0answers
29 views
16
votes
2answers
290 views

How to improve performance of BesselJ to the level of GSL?

Consider the following code: zs = N /@ Range[0, 12, 10^-5]; AbsoluteTiming[bessels = BesselJ[1, #] & /@ zs;] Length@zs I've tried to measure only computation ...
13
votes
0answers
195 views

Why is ListPlot so slow here?

While examining How can I monitor the progress of a Plot? I was surprised to discover that in some cases ListPlot in version 10.0 and 10.1 is orders of magnitude ...
4
votes
0answers
72 views

How to perform Bayesian optimization with Mathematica?

Does Mathematica have any Bayesian optimization methods for learning hyper-parameters? Practical Bayesian Optimization of Machine Learning Algorithms Jasper Snoek, Hugo Larochelle and Ryan P. ...
1
vote
1answer
79 views

Apply NonlinearModelFit to an image

I have several images (in FITS format), which I want to fit with a 2D-Gaussian distribution. The problem is that NonlinearModelFit expects data in the form of a ...
5
votes
1answer
120 views

Reap, Sow with Parallelize: bad performance, why?

I have a question about the performance of Reap and Sow with Parallelize. I am aware of the following questions How to collect result continuously (interruptible calculation) when running parallel ...
6
votes
0answers
100 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 ...
6
votes
3answers
176 views

Speed up derivative evaluation

I'm trying to calculate the normal vectors and the tangent vectors at the discrete points of a suface. For example: ...