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)

7
votes
0answers
140 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 ...
0
votes
0answers
43 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 ...
15
votes
1answer
218 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 ...
11
votes
3answers
460 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"] ...
21
votes
4answers
707 views

How to reduce the InterpolatingFunction building overhead?

I want a linear interpolation from the following example list: ...
2
votes
1answer
55 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 ...
28
votes
1answer
906 views

Fast Spherical Harmonics radiative transfer

This is a rather specific question and I apologize for spamming you with some lengthy code. But it could be interesting for some reader and maybe you can help out, so please bear with me. I am using ...
1
vote
0answers
53 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; ...
2
votes
1answer
79 views

How to handle Indeterminate TimeConstrain

I have to use Timeconstrained[aux] first for avoid some bigs calculation that dont need and it use a lot of time of calculate, so i can't do this. ...
2
votes
2answers
91 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. ...
3
votes
0answers
41 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, ...
5
votes
4answers
672 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: ...
14
votes
3answers
616 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 ...
0
votes
0answers
167 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: ...
14
votes
3answers
373 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 ...
6
votes
0answers
204 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 ...
5
votes
3answers
74 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
31 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
1answer
92 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 ...
9
votes
0answers
89 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
1answer
63 views

Pros/Cons to updating in place

There are often times when I have to update a list. The following is an example: ...
6
votes
1answer
320 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 ...
10
votes
3answers
363 views

Riffle and Partition

At some point I noticed that I was using Riffle and Partition together a lot. I would do things like ...
15
votes
2answers
139 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 ...
3
votes
1answer
180 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 ...
5
votes
1answer
85 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. ...
1
vote
1answer
80 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: ...
2
votes
1answer
70 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 ...
22
votes
7answers
2k views

How to improve the performance of solutions to Project Euler (#39)?

This is the problem 39 of Project Euler, which I asked in the chat room two days ago. My original code runs as slowly as snails, and finally I got two answers from JM and Rojo. Unfortunately, both of ...
3
votes
1answer
80 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 ...
4
votes
2answers
275 views

How can this DivisorSigma code be made fast?

Since Project Euler problems are now fair game for questions I have a question of my own. A certain problem* states: For a positive integer n, let σ2(n) be the sum of the squares of its ...
9
votes
2answers
298 views

Complement on pre-sorted lists

Is there any way to instruct Complement to skip the sorting part? If the answer is no (likely), the next question would be: how can I remove from a sorted list ...
6
votes
2answers
164 views

Speed and the MapThread function

Can anyone help explain why MapThread is so slow in this simple case? ...
5
votes
2answers
134 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
0answers
86 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 ...
10
votes
1answer
663 views

Modular arithmetic - efficiently calculating the remainders of factorials

When working on this question regarding the divisibility of the sum of factorials, I decided to write some code to test "small values" of the problem using the following code. ...
2
votes
1answer
145 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 ...
11
votes
5answers
410 views

Most efficient way of making pair list from a list

I have a bunch of datasets which looks like: 3 7 1 6 5 8 2 4 1 2 8 1 5 2 5 ... and I need to make a list of pairs such that the first element makes a pair with ...
16
votes
2answers
287 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 ...
1
vote
1answer
56 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 ...
19
votes
1answer
350 views

Why some built-in functions are slow

I just wonder why some built-in functions are slower compared to some other ways using more than one built-in functions for doing same job. consider this example: ...
9
votes
2answers
219 views

Relaxation of Mass-Spring System with Pinned Masses

Problem Assume that four masses are connected to each other by springs while two of them are pinned down at their initial positions. Find the position of masses when system is at its minimum ...
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. ...
4
votes
3answers
2k views

Definite integral takes a very long time

I'm trying to integrate the following function: $$r_e=\int_0^z\frac{dz}{\sqrt{\Omega_m(1+z)^3+\Omega_\Lambda}}$$ where $\Omega_\Lambda=1-\Omega_M$ and both $\Omega_\Lambda$ and $\Omega_M$ are real ...
1
vote
2answers
119 views

Slow program- Mathematica shuts down

I've been trying to draw dynamic pictures of level sets of certain polynomials. The code right now looks like that : ...
4
votes
2answers
121 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 ...
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 ...
2
votes
1answer
64 views
2
votes
1answer
95 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 ...
6
votes
0answers
95 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 ...