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
1answer
116 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 ...
15
votes
5answers
341 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 ...
0
votes
0answers
37 views

FindInstance—why doesn't it work with my expression? [on hold]

I want to find arbitrary solution of a system of equations: ...
3
votes
0answers
102 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 ...
-2
votes
1answer
61 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 ...
3
votes
0answers
50 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 ...
6
votes
0answers
83 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 ...
0
votes
1answer
41 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 ...
12
votes
1answer
130 views

How to count the number of the tube

There is a picture Get the binary image binimg = ColorSeparate[img][[2]] // Binarize[#, 0.25] & // Thinning Visulize the result of regconization ...
4
votes
2answers
86 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: ...
14
votes
3answers
424 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 ...
3
votes
1answer
62 views

How to put the polyhedron base

We get the tetrakaidecahedron like this: ...
0
votes
0answers
47 views

Solving a transcendental equation wth a power series term [on hold]

I have the following code for a transcendental equation that i am trying to solve for E2 using the Erzek Root Finder. However, there is a slight complication owning to the presence of an infinite ...
3
votes
1answer
41 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. ...
35
votes
9answers
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 ...
1
vote
0answers
49 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 ...
5
votes
1answer
142 views

Fast loading of png files

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 ...
6
votes
1answer
117 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 ...
3
votes
2answers
107 views

Implementation of a matrix formula

For the Bezier surface, which owns the following matrix definition: where, $B_{n,i}$ denotes Bernstein-basis. ...
3
votes
0answers
38 views

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

First example: librarylink function return a list of two element ...
15
votes
5answers
613 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 ...
2
votes
2answers
64 views
3
votes
3answers
285 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 ...
1
vote
1answer
89 views

How to do this Function with compile

There is 4 lists, 3 of them are at same length (l0, l1 and l3). ...
0
votes
1answer
107 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 ...
3
votes
1answer
138 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 ...
11
votes
1answer
242 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 ...
1
vote
1answer
89 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 ...
2
votes
1answer
85 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 ...
1
vote
1answer
23 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 ...
14
votes
1answer
246 views

Why is SatisfiabilityCount faster than SatisfiableQ?

Consider the following (large) Boolean expression, which arose in a SAT problem. (The expression is not satisfiable.) ...
1
vote
0answers
43 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: ...
1
vote
1answer
67 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: ...
10
votes
2answers
277 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 ...
5
votes
5answers
119 views

How to find the derivative value at $(\pi,0)$ for this implcit 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 ...
9
votes
1answer
200 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
0answers
119 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 ...
4
votes
0answers
78 views

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

Turn on Needs["CompiledFunctionTools`"] First example ...
4
votes
1answer
90 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 ...
0
votes
1answer
50 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 ...
9
votes
1answer
168 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: ...
5
votes
0answers
70 views

How does Internal`PolynomialFunctionQ work?

I found that Internal`PolynomialFunctionQ performs much better than PolynomialQ. Here is a huge random polynomial in 12 ...
1
vote
0answers
36 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, ...
6
votes
1answer
95 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. ...
3
votes
1answer
112 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 ...
5
votes
1answer
180 views

How to compile this code?

Could somebody teach me how to compile this code? ...
8
votes
2answers
180 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. ...
4
votes
1answer
102 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 ...