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

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 ...
430 views

### Highlighting pattern strings in a large text

I would like to apply Style to strings that are matched, for example, with one or more specified patterns or regular expressions, a string or a list of both, ...
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 ...
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 ...
96 views

### How to do this Function with compile

There is 4 lists, 3 of them are at same length (l0, l1 and l3). ...
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 (...
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 ...
897 views

### Parallelize evaluation of function with memoization

I have a complicated function that I need multiple times, so I want to memoize it and have the first evaluation done in parallel. Unlike in my example below it's not a continuous function, so ...
73 views

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

How can do it? ...
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.) ...
95 views

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

Turn on Needs["CompiledFunctionTools`"] First example ...
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 ...
280 views

### How to get “Signature” of each element of “Permutation” output

I need to sum over all possible permutations, multiplied by the Signature of each permutation of a given list of symbols. But, I don't know how to determine the <...
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 ...
96 views

### Fast construction and reshaping of large tensors

I'm trying to construct a large matrix which is derived from some higher rank tensor (the rank of interest to me changes case by case, so it needs to be a general method). Currently, the process of ...
53 views

### Timing and memory use is critical:fast partitioning of binary sparse array

I do experiments were I measure a signal, and I need to know at what time points it is above a threshold. If at a given time point, the signal is above the threshold, then a '1' gets put into the ...
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: ...
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 ...
714 views

### Why is calling Dot with many arguments so inefficient?

Say I want to multiply a reasonably large list of symbolic matrices. ...
160 views

### Efficient and fastest way to execute 4 nested loops

I have a question regarding the use of ParallelDo. I tried to parallelize the following code involving four nested Do loops by ...
121 views

### List consisting of positions of the matrix elements and the elements themselves

Given a 2D matrix I would like to make a list with elements consisting of positions of the matrix elements and the elements themselves. Let the matrix be ...
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: ...
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. ...
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, ...
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-...
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? ...
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 ...
1k views

### Slow import of multigigabyte TIF image stacks

Update 6: After contacting Wolfram Technical Support, I was advised that the frame-to-frame import timing variability can be eliminated by reading in the frame stack from frame to frame with the ...
367 views

### Why is multidimensional Reverse slow?

Bug introduced in 6.0 or earlier and persisting through 10.3.1 I have found that multidimensional Reverse is ~10 times slower than exact ...
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. ...
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 ...
84 views

### How to make this code involving Hypergeometric functions to run faster?

This question is followed up from this Question. I would like to thank Dr. Hintze and I_Mariusz for the comments and help. I am pretty new to mathematica ( I just learned it 4 days ago) so I would ...
117 views

### Speeding up Integrate and the numerical solution

I want to compute the drag of a plate Below is the code I am using to integrate the function ...
113 views

### Generate a 3D grid data table

I would like to generate a 3D Grid, not for displaying, but to do computations on it, so no draw functions I guess. I have a simple version of what I am thinking, however, it is terribly slow: ...
102 views

90 views

### Overlap between lists of sublists with different dimensions

I have two lists of sublists. The sublists in the first list contain three elements while the points in the second list contain four e.g.: ...
I need to get a matrix $\{a(x_i-x_j)\}$, where $x_i$ form a partition of an interval, $a(x)$ is a given function. I use ...