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
0answers
34 views
0
votes
0answers
76 views

How to compile code that involve sparse array?

I have the following code ...
3
votes
2answers
145 views

Fast way to evaluate the PDF of a multivariable distribution

I'm trying to evaluate the PDF of a truncated multi-normal distribution. My distribution has 8 dimensions and each value must be ≥ 0. ...
13
votes
3answers
564 views

Particles bouncing in a 3D box

I made this Manipulate code which shows a pack of particles randomly moving inside a box. It has a huge performance issue. What should be the best way in doing "...
2
votes
0answers
66 views

Estimating processing time for commands [closed]

I am trying to evaluate some equations by using Taylor series of expansion (Series[], ExpandAll[], and Normal[] command), and using n equation and n unknown (Solve). Following is not full command (if ...
4
votes
2answers
117 views

Efficiently strip off coefficients in front of variables?

I am working with multivariate polynomials and need a very efficient way to decompose monomials into coefficients and pure monomials. for instance consider variables ...
1
vote
1answer
170 views

How I can get faster code for computing a fractional integral?

I use of this code Fractional Integral and wrote the following code: ...
4
votes
1answer
211 views

Mathematica 10.X benchmark report for high-end surface pro 4/surface book?

Recently, I am seriously considering purchasing the high-end surface device (with intel i7 and maximal RAM). Mathematica will be one of the main applications. Since it is not a full or traditional ...
1
vote
0answers
84 views

How to speed up the construction of a large matrix

As a part of a physics problem that I'm working on, I need to construct a rather large matrix. The matrix is a real symmetric $2^{m}\times 2^{m}$ matrix for some integer $m$, which we would like to ...
3
votes
2answers
91 views

Perform matrix/tensor contractions more efficiently

Problem: explicit index contractions for matrices using the Sum command take too long, and I want to improve the performance for complicated computations. Let me ...
10
votes
2answers
206 views

ParallelTable behaves differently when it is run inside a package

I've been butting my head against some very weird behaviour by ParallelTable, and its interaction with packages, over the past few weeks, and making relatively ...
1
vote
0answers
96 views

Need help solving a polynomial system [closed]

I need help solving the following system of equations: ...
0
votes
2answers
95 views

Asymptotic form of the “strange” function

I want to find the asymptotic form of this function ...
1
vote
1answer
108 views

How to make AppendTo faster

Function AppendTo seems to be very slow for generating a lot of scenarios, is there any chance to change it and make it faster? ...
8
votes
5answers
295 views

What is the most efficient way to replace items in a list at periodic intervals?

For example, if I want a list of $\phi(n)$ for $1 \leq n \leq 100$ but if $n$ is a multiple of 3 I want to show a 3 instead of $\phi(n)$. I tried ...
0
votes
0answers
43 views

Finding minima of a function: Ways to speed up the code [duplicate]

I am quite new to Mathematica. I tried to calculate all minima of the function of interest as follows: ...
1
vote
1answer
64 views

Fast way to compute intersection of equivalence classes

There is a set $S=\{1, 2, \dots, m\}$ and $n$ equivalence relations on the set: $a \sim_i b $, where $a,b \in S$, $i=1, \dots, n$. Now we define $a \sim b$ iff $ a \sim_i b$ $\forall i=1, \dots, n$. ...
1
vote
1answer
77 views

A custom function is too slow when I first time to run it

I make a function like this ...
10
votes
3answers
468 views

Radial distribution function

I have tried to rewrite my old IDL code to calculate the radial correlation function for a regular 2D crystal structure. The theory behind this function is given here: https://en.wikipedia.org/wiki/...
4
votes
2answers
106 views

Want Manipulate to be low resolution enough to render fast but no so low resolution that it is useless

When using Manipulate to graph a simple surface (sum of 2 sine waves) with Plot3D, setting PerformanceGoal->"Quality" makes it take about 1/3 second to render, while "Speed" sets it at such a low ...
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 ...
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
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 $\...
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. ...
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: ...
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 ...
-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 ...
6
votes
1answer
255 views
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 ...
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 ...
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
66 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
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
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. ...
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
250 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 ...
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
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: ...
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 ...
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 ...
3
votes
3answers
109 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 ...
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,...
1
vote
1answer
107 views

efficient element wise multiplication of SparseArray vector and matrix

I have a large SparseArray $P$ of dimensions $\{d,d\}$ and I want to multiply a vector (also a SparseArray) $v$ of length $d$ ...
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 ...
14
votes
6answers
464 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 ...
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 ...
1
vote
1answer
43 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, ...
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 ...