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)

3
votes
1answer
154 views

How can I optimize the search between two lists, one that is changing length?

I'm trying to identify unique arrays by comparing two lists, one which is growing in length. The number of elements in the list grows as 2^n^2, where ...
3
votes
2answers
143 views

Summing matrix products

I need to compute a double sum over a weighted matrix product: $L[M]=\sum_{i,j}^{N}\Lambda[[i]]\;\omega[[i]].M.\omega^{\dagger}[[j]]$. $\Lambda$ is a list of with N complex values(weights) and $\...
35
votes
5answers
3k views

Is it possible to speed up ContourPlot on multi-core machines?

It is not very difficult to face a function for which ContourPlot works too slow. And it seems natural that this function can be parallelized well. Anyway, naive <...
0
votes
0answers
95 views

NDSolve with default settings solves much faster than classic Runge-Kutta 4

NDSolve with default parameters takes 0.25 s for the first run. With Runge Kutta 4 specified, it takes about 4.6 s, ...
3
votes
0answers
62 views

EvaluationMonitor slows down NDSolve considerably

Hi guys I'm solving a nonlinear system of ODEs with NDSolve. Since the dimension of the system ranges from 2700 to 20000 equations, depending on the parameters, the time required to solve the entire ...
4
votes
0answers
45 views

Is there a clean way to parallelize multi-index tables?

The documentation of ParallelTable indicates that if you want to parallelize the evaluation of a table of more than one dimension, such as ...
5
votes
0answers
79 views

Improve recursive function (memoization)

These days I am trying to rewrite the following recursive function, in order to gain some speed. It is crucial because I end up using 4-5 similar functions, and I need to decrease execution time. ...
9
votes
6answers
507 views

Efficiently finding the positions of a large list of targets in another, even larger list

I have a big list. It contains nearly 60,000 sub-lists. It's structured like bigList = {{x1,y1,z1},{x2,y2,z2},{x3,y3,z3},.....,{x60000,y60000,z60000}}; I have ...
2
votes
0answers
188 views

Best approach for 'manual' common subexpression elimination

I have working code, but am looking for ways to make it more elegant. I have a fairly large expression with plenty of repetition and structure. A shortened toy example is ...
4
votes
2answers
216 views

General solution for a linear ODE set with complicated coefficient

This is the original problem that motivated me to ask this question. I encountered it when trying to reproduce the deduction in this paper. (I'll paste the relevant part below to make this question ...
10
votes
1answer
118 views

What are the main differences between RawArray and PackedArray?

What I know: PackedArray is documented (to some extent) in "Developer Utilities Package", RawArray is just mentioned in ...
2
votes
1answer
71 views

Code optimizations with functions inside Module

I wrote a small code by using functions inside Module: ...
22
votes
0answers
373 views

Can code optimize code?

Are there functions, programs or packages that deal with optimizing mathematica code i.e. re-writing ineffecive or convoluted code. -This question has haunted me since I joined SE. The background for ...
-3
votes
1answer
178 views

How to turn off depth sorting of 3D curves for Manipulate?

Is there a way to turn off the default depth sorting of curves in a 3D output of several complicated curves ? Currently, I suspect that depth sorting has a very strong impact on performances on my ...
5
votes
3answers
134 views

Lazy form of Tuples/Outer to loop over list of lists

This is less a question and more asking if someone has implemented this already, with more skill. I need to perform the Outer-like generalized outer product of a ...
2
votes
0answers
67 views

Data acquisition from TCP socket

At last (since 10.3) we have straightforward/documented access to TCP sockets. It's been a long wait. So far there is very little discussion of the new feature. Although some older posts are still ...
0
votes
1answer
280 views

Speed up RegionPlot

I want to plot a system of equalities and inequalities. I used a combination of ImplicitRegion and RegionPlot (since RegionPlot cannot plot lines or points). But this method is very slow than using ...
4
votes
3answers
133 views

Indexing all elements of permutations $S_n$

I have this Table that gives all elements of $S_n$, I would like to know how to improve efficiency. ...
0
votes
0answers
32 views

Solving for limit of integration with a piecewise function (FindRoot vs NSolve) [duplicate]

I am trying to solve for the limits of integration using a piecewise function. I'm doing this in a loop over a list of data, mostly because I'm not well-versed enough in Mathematica to make this a ...
4
votes
5answers
134 views

How to efficiently stream words and whitespace from a large text file

I want to tokenize a large (~20 MB) file, opening it and streaming the words, but preserving the whitespace as well. Example: ...
6
votes
1answer
142 views

Faster way to perform SameQ[Reduce[…], Reduce[…]]

I'm have some expressions that I need to confirm the equality of. SameQ[Reduce[...], Reduce[...]] works like a charm for the more simple expressions, however when ...
3
votes
1answer
141 views

Why does Compile not make my function faster?

I need to speed up a large program that repeatedly calls the module below (I've not trimmed the code for this question to ensure there is enough calculation to show a speed improvement.) The obvious ...
5
votes
2answers
252 views

Speed-up the computation of this sum of small matrices

Given a matrix mat with a large number of rows (a few thousands) and a few columns (between 2 and 10), I'd like to compute the sum of the "small" matrices obtained ...
10
votes
2answers
231 views

Primes Race (Mathematica Efficiency)

I am currently working with a paper that deals with this concept of Prime Races. You essentially create a large list of prime numbers and then split that list into two teams. You are assigned to ...
11
votes
0answers
198 views

Slow JoinAcross performance

Bug introduced in 10.3 I am using JoinAcross to combine two datasets, where the key is an Integer. ...
3
votes
3answers
111 views

Counting runs in bits, along with their starts and lengths, in functional style

Here is some code to visualize Project Euler Problem 502. The code scans for runs of bits in the binary representation of an integer, and outputs the beginning position of each run and the length of ...
14
votes
6answers
466 views

Given a large binary matrix, find the largest submatrix containing non-zero elements

I'm looking for a way to reduce a binary matrix containing zeros at some positions into a matrix that contains no zeros by deleting rows and columns of the original matrix until only non-zero values ...
9
votes
2answers
571 views

Improve speed in this tree - memoization won't suffice

I have two functions, one of which calls the other. I need to do some calculations but I was unable to optimize my code to do it efficiently. In search I came across the memoization technique, which ...
1
vote
1answer
161 views

Problem with convergence of limiting value in Manipulate

I created a nice Manipulate box of the magnetic field outside and inside a spherical shell placed in an homogeneous magnetic field. The code is working great, but it has a problem at a limiting value,...
4
votes
1answer
251 views

How to speed up calculation of this equation (FindRoot)

I have come up with the following functions, and I need to calculate a value, in order to have a condition valid. In my problem, all data are given, and I need to calculate variable l. I am using ...
1
vote
1answer
102 views

Computing the coefficients for Gaussian integral

I wrote a small script to find the Gaussian integral points and weights: ...
5
votes
2answers
233 views

How to improve efficiency when classifying lists according to certain laws

I have a function that generates lists: f[n_] := Join @@ Permutations /@ PadLeft@IntegerPartitions[n]; For example f[3] <...
24
votes
1answer
745 views

How to work with Experimental`NumericalFunction?

This question is intimately connected with previous one: "How to create internally optimized expression for computing with high WorkingPrecision?" Oleksandr R. correctly states in the comment: A ...
2
votes
1answer
216 views

Error with FindMinimum. The Message is FindMinimum::cnpcons

I want to run a rather demanding check of feasibility. I have 3565 non-negativity constraints for my variables, and one that makes them sum to a certain budget they shall not surpass. I also have ...
6
votes
2answers
370 views

Memory management and speed for Fast GCD

Let's say that we have some $300\,\text{K}$ digits (arbitrary function) and want to trial factor with $100{,}000{,}000$ first prime numbers. ...
16
votes
4answers
641 views

How to remove duplicates from set of machine precision 2D points?

I have a set of 2D points with machine precision coordinates. I need to remove all duplicates. Performance is important. This is the most obvious fast solution: ...
3
votes
0answers
45 views

Prepending a sequence with zeros gives `FindGeneratingFunction` hard time

Asking for FindGeneratingFunction[{1, 3, 11, 43, 171, 683, 2731, 10923, 43691, 174763}, t] gives the answer (namely ...
10
votes
2answers
275 views

What is the fastest way to compute digits of $\pi$ using Mathematica?

There are a lot of ways to calculate digits of $\pi$ using Mathematica. The most naïve way I can think of is N[π, 100000000] Of course, there are a lot of fast ...
4
votes
2answers
229 views

Elegant way to partition list to be faster than `ParallelTable` (using `ParallelSubmit`), or some alternative?

Suppose you have a function fun[x_] := (Pause[.05*x]; x^2); of which you know that its evaluation time increases with its argument - in this case obviously ...
11
votes
1answer
102 views

Speed up Dynamic for graphical interface

I am trying to create a user interface where I can click on certain points in a set of data and save them into a list that can be used later in my code. The problem is it is currently sluggish. Is ...
1
vote
1answer
44 views

Customized `ParallelTable` - Automate `ParallelSubmit` - Possibly an issue with `HoldAllComplete` attribute

Related to this question and in particular its answer I am trying to wrap everything into a new parallelTab function that takes into account some cost per job, ...
3
votes
4answers
186 views

Find the minimum positive-integer value which meet the requirement

If the equation is Mod[515 Sum[10^(3 d), {d, 0, n}], 53] == 0 Then I can get the minimum positive-integer value of n. ...
0
votes
1answer
86 views

Slow sum computation

I'm doing a simple OLS regression and I strikes my attention that this very simple computation takes several seconds to perform in Mathematica ...
2
votes
2answers
337 views

How to optimize this code?

I am implementing a code that works correctly but it takes too much time. I did not see how to optimize it to be run quickly. Here is my code: ...
9
votes
1answer
229 views

Speed up construction of a dense matrix

I wish to find the numerical eigenvalues of a relatively small (100 dimensional) Hermitian matrix. While the eigenvalue calculation itself is fast, the construction of the matrix is taking longer than ...
5
votes
1answer
138 views

Find random $n$ combinations of values with a given sum

This problem is described in the related StackOverflow question: Find all combinations of coins when given some dollar value. I would like generate a list of $n$ combinations of values that sum up to ...
22
votes
0answers
389 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 ...
1
vote
0answers
61 views

Speeding up ImageData[image,“Byte”]

For my image analysis I need the Byte values of many sequential 8bit PNG images. To obtain the Byte data I use: ...