# 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.

70 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 ...
66 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 ...
120 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 ...
83 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: ...
486 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 ...
57 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 ...
183 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 (...
154 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 ...
338 views

### Implementation of a matrix formula

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

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

First example: librarylink function return a list of two element ...
631 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 ...
82 views

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

I write this code ...
336 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: ...
353 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). ...
246 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 ...
248 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 ...
73 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 ...
270 views

### Why is SatisfiabilityCount faster than SatisfiableQ?

Consider the following (large) Boolean expression, which arose in a SAT problem. (The expression is not satisfiable.) ...
54 views

### Compilation error: “Could not complete external evaluation at…”

I'm pretty new to trying to use Compile[], and I just can't see why I'm getting this error. I have two functions to define beforehand; one of them is a bit odd: <...
70 views

### How can I obtain the same result without using any loop?

I have a piece of code as below: For[i = 1; t = x, i^2 < 10, i++, t = t^2 + i; Print[t]] and the result is: ...
297 views

### How to “inform” successive ContourPlot calculations?

I need to draw some contour plots of very non-linear functions. As a simple example, take a Mandelbrot Set divergence contour near $z\approx i$. (Just to be clear, I'm not trying to write a ...
168 views

### How to find the derivative value at $(\pi,0)$ for this implicit function $n$ times?

I am trying to take the implicit derivative at $\sin(x+y)+\sin(x)=y$ and substitute $x=\pi$ and $y=0$ at least 6-7 times since I need to find the Taylor series for this function. Since I barely ...
227 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 ...
134 views

### Call Magma in Mathematica

I've written some code in Mathematica, related to some group theory questions I have. Subsequently, I found that part of the code could be significantly sped up if I could just call a routine in ...
95 views

### Is it possible to prohibit unnecessary CopyTensor in Compile? Just like .noalias() in Eigen library?

Turn on Needs["CompiledFunctionTools"] First example ...
137 views

### How to increase the evaluation speed of this somewhat complicated table / matrix operation?

I'd like to calculate the following one-dimensional array: OneDimArray = Table[(Norm[Sum[ MatrixA[[i,j]]VectorB[[j]],{j,N}]])^2,{i,N}] However this takes a very ...
56 views

### Invoking functions in other kernels

Suppose one wants to parallelize as much as possible N tasks, each task produces a graphics via ParametricPlot or something like that. If the system is allowed to ...
172 views

### What kind of patterns in a list of rules would benefit from being Dispatched?

I have a long list of rules for manipulating expressions. They are of the form: ...
103 views

### How does InternalPolynomialFunctionQ work?

I found that Internal`PolynomialFunctionQ performs much better than PolynomialQ. Here is a huge random polynomial in 12 ...
41 views

### Speedup ListCorrelation when using generalized correlation function

ImageCorrelation provides several general function that can be used to replace the default Dot function. For example, in the documentation of ImageCorrelation, ...
134 views

### Extracting PNG images from AVI movie

I have written the following code to extract all images from an uncompressed AVI movie and to save them as sequential PNGs. ...
120 views

### Randomly generate points on a van der Waals surface

I'm looking to randomly place an atom $\mathbf{A}$ onto a van der Waals surface formed by atoms $\mathbf{B}$, such that the distance between the randomly-generated atom $\mathbf{A}$ and the nearest ...
204 views

### How to compile this code?

Could somebody teach me how to compile this code? ...
196 views

### Evolution of a trait in an asexual population (performance tuning)

I just coded a simple simulation module that looks at the evolution of a continuous trait in a haploid asexually reproducing population under density dependent competition in discrete time (i.e. non-...
137 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 ...
306 views

### Which elements of a large array are part of a set?

I have a large integer packed array arr. My array is multidimensional, but for simplicity let's consider 1D arrays for now. ...
202 views

### Tracing 2d coordinates by using Nearest function

In 5 digital images I have determined the 2d coordinates of detected objects. The resulting coordinate list can be downloaded from here: http://pastebin.com/7bE0CEJ5 ...
89 views

### Faster square test for integers

This question was asked already in Jan '12 and the most recent answer is from Oct '12, so it's several Mathematica versions out of date. What is a faster test for whether an integer is a perfect ...
177 views

### Optimizing a series of basic matrix operations—scaling doesn't seem right with matrix size

I have a simple operation that I'd like to apply to an n×n matrix. My problem (well, one of them...) is that it runs in, say, 4 seconds for a 100×100 matrix, but it took five and a half minutes to run ...