# Tagged Questions

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.

276 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 ...
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, ...
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 ...
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. ...
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 ...
163 views

### How to optimize my own BezierFunction?

A few months ago, I asked a question about the implementation of Bezier surface. Please see here for a full detail. I also know that BezierFunction[] is built-in ...
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 ...
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 ...
198 views

### Slow JoinAcross performance

Bug introduced in 10.3 I am using JoinAcross to combine two datasets, where the key is an Integer. ...
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: ...
289 views

### Why is ListPlot[] so slow for coordinate pairs? Can we speed it up?

I compared the time it took to create a plot in two different ways. I expected the first method to be slower, but I was surprised at how much. Is this an inherent limitation or the function, or are ...
59 views

### why URLFetch is slow when encapsulated by module and how to fix it [closed]

I'm using Mathematica 10.0.2 on Win7. I'm writing some program to crawl etymology of words on http://www.etymonline.com/. Below lies the example code: ...
117 views

### Memory limit hit: optimize code for finding twin primes

I need some help optimizing a Mathematica code so that it'll not max out RAM. Here's the code: Cell 1 ...
37 views

### Access variable middle of the run [duplicate]

I execute a Table[] that need large time to be finished. If I abort it I loose what evaluated until now. Does it possible to stop running and save what evaluated ...
98 views

### How to improve speed of the following code

The following code do what I needed, but it takes too long ...
59 views

### Is this problem parametrically solvable or it can just be solved for specific $\sigma$ and $\theta$?

In order to solve This problem: What is the maximum value of coefficient fv with the constraint that the matrix is positive semi-definite?, I have used the following code (Determinant is computed by ...
199 views

### How to efficiently find all combinations of the letters in an alphabet given a condition

Problem: I want to find all unique expressions with n number of terms that contain all the digits (or characters) in alphabet. ...
90 views

### Is it possible to recover the efficiency of ListConvolve when generalizing the convolution?

I've noticed that, while ListConvolve is very fast, it becomes much slower when you use anything but the standard Times,Plus as ...
157 views

### Why is Random faster than RandomReal?

As of version 6, Random[] was superseded by RandomReal[ ] etc, but both still work perfectly well. Playing with some timing ...
498 views

### Partitioning a list when the cumulative sum exceeds 1

I have a long list of say 1 million Uniform(0,1) random numbers, such as: dat = {0.71, 0.685, 0.16, 0.82, 0.73, 0.44, 0.89, 0.02, 0.47, 0.65} I want to ...
90 views

### FindInstance—why doesn't it work with my expression?

I want to find arbitrary solution of a system of equations: ...
140 views

### Efficiently generating tuples with Outer

I'm trying to efficiently generate tuples of lists of objects that satisfy a given criterion. The similar questions that I have found on this website end up finding a specific workaround for the given ...
74 views

...
66 views

### Manipulate aborts the computation of NDSolve with the Piecewise function after curtain time [closed]

I'm facing the problem with performance: Manipulate is working fine till end time =60 sec, after that it aborts. There is NDSolve inside Manipulate with Piecewise function, and the plot of the ...
69 views

### NelderMeadMinimize for 2D Gaussian fitting

I would like to fit a large number (10^5-10^6) of small images to 2D gaussians by taking advantage of the NelderMeadMinimize package from Oleksandr: Shaving the last 50 ms off NMinimize. The ...
121 views

### What are the time and space complexities of Classify/Predict?

Does anyone have knowledge of the O(n) time & space complexities for various model types supported by Classify[] and ...
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 ...
123 views

### How can I make this code to count Hamiltonian paths faster?

I have this very basic code to count Hamiltonian paths in a graph: ...
496 views

### Generating Tuples with restrictions

I am interested in a certain subset of all tuples. Generating all with Tuples and then filtering is wasteful, and will "blow up" very quickly. For a concrete ...
50 views

### Replacing elements of a matrix

I'm trying to write a program that takes a given matrix $A$ of 1's and 0's and flips a designated amount of 1's to -1's. ...
2k views

### What would you ask Mathematica to do on a big system?

Our hardware supplier has given us the opportunity to test Mathematica on a big Windows system. The specs will be 1 to 4 TB of working memory and 12 sockets with 18 cores each. I got 1 full week of ...
59 views

### Solving a polynomial equation with an approximate constraint is slower than the symbolic solution

Why do these two operations have such different timing, when the only difference is specifying r as approximate $0.1$ vs arbitrary-precision $1/10$? Here are the ...
192 views

I need to process through 600 images that are stored as png-files. Each file has 8.3-ish Mb and 2400x2400 pixel (Real32). For a single processor it takes 0.93sec per image to be loaded with Import (...
170 views

### Finding mean brightness, standard deviation, count of pixels in gray scale images

I have many sequential gray scale images (2048*2048 pixels, 8bit). To determine the brightness, standard deviation, and count of pixels with in two bins, zero and non-zero brightness, I use the code ...
381 views

### Implementation of tensor product formula

For the Bézier surface, which owns the following matrix definition: \begin{align*} \mathbf S(u,v)&=\sum_{i=0}^m \sum_{j=0}^n \mathbf P_{i,j} B_{m,i}(u) B_{n,j}(v)\\ &=\small \begin{pmatrix}...
63 views

### Why is a LibraryLink function, which returns a list, not parallelizing with ParallelTable?

First example: librarylink function return a list of two element ...
634 views

### Easiest/most efficient way to delete duplicates from one list at the positions of another?

This question is somewhat related to How to efficiently find positions of duplicates? , although I'm interested in something a little bit simpler since I'm not really interested in a list of all the ...
84 views

### How can I speed up the evaluation of a Dynamic Locator acting on a Region?

I write this code ...
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: ...
361 views

### Mandelbrot set—efficiently iterate over a list of initial points

OP edit: This is a Mathematica-specific question about an approach it attempted for a fractal visualization problem described HERE. I'm using the Mandelbrot set there and here as an example, but the ...
96 views

### How to do this Function with compile

There is 4 lists, 3 of them are at same length (l0, l1 and l3). ...
273 views

### Histograms of gray scale images

I have a large set of subsequent 8bit gray scale png images (50 Hz). They show the noise of a CMOS camera, with the lens covered. A subset of 50 images is available here: https://goo.gl/Sa3P9P I ...
145 views

### How to speed up filling a matrix as much as possible in a loop?

In order to find the solution of an $n\times n$ system of nonlinear equations by the method $x_{k+1}=x_k-D_k^{-1}f(x_k)$ where $D_k$ is an approximation of the Jacobian and defined by ...
249 views

### Why does Outer sometimes return a packed array, and sometimes not?

For example In[342]:= test3 = Flatten[Outer[Plus, Range[1000], Range[1000]], 1]; // AbsoluteTiming Out[342]= {0.0198735, Null} returns PackedArray (...
92 views

### Manipulating Some Lists in Compile

Consider a list of lists, such as: Z={{1,4,3},{2,1,3},{3,1,4}} Z is an $n \times n$ matrix (here $3 \times 3$) which ...
99 views

### Product of $N\ 2 \times 2$ matrices and subsequently solving an equation dependent on the product

I have $N\ 2 \times 2$ matrices, each one containing one variable energy which may be complex. I want to muliply them all together to a single matrix ...
75 views

### Get position from one list and choose element of another list in Compile

How can do it? ...
28 views

### Coefficient of function with general argument

I would like to obtain the coefficients of a general function f in a list. For example, the coefficient list of ...