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 (3)

1
vote
1answer
33 views

Speed up the auxiliary function magicSquare when $n$ is doubly-even

Today, I saw a question about magic square. Please see here When $n$ is doubly-even.Namely, Mod[n,4]==0 ...
0
votes
2answers
82 views

Is it possible to sum the results of a Map - Apply?

I am fairly new to functional programming and would like to replace a For[] loop that sums the results over a Dataset. This may be a bit different since the ...
2
votes
0answers
43 views

Optimization of custom probability distibutions?

Consider the two tests: Timing[Do[RandomVariate[NormalDistribution[0.5, 0.3]], {200}]] {0.001188, Null} ...
0
votes
0answers
22 views

How can I find all stationary points? i.e Something like MinDetect but for saddle points (that applies to data arrays of arbitrary rank)

I would like to find all stationary points of a data set of arbitrary rank. MinDetect and MaxDetect are great for finding minima and maxima but I can't find a good way of finding the saddle points. ...
2
votes
1answer
62 views

Faster variant of Reduce for finding zeros of holomorphic functions in a region

I'm currently using Reduce to find zeros of a function in a region on a complex plane: ...
8
votes
4answers
125 views

ImageApply Performance Difference

How come the timing difference between these calls is so big? (0.2 and 39 seconds on my computer) ImageApply[q3, img] ImageApply[q3[#] &, img] Where ...
1
vote
1answer
75 views

Improving Performance - Finding Polynomial Roots [duplicate]

I'm fairly new to Mathematica. In the past, my usage has mostly been limited to solving the occasional equation, making some plots, and working with small scaled statistics. None of these have been ...
10
votes
1answer
143 views

Faster trimming of leading/trailing zeroes in rectangular array

Given a (large) rectangular array, e.g. (small for example case): ...
7
votes
2answers
140 views

why MeanFilter is nearly 100 times slower than MovingAverage for 1D array?

I used to average over my data with MeanFilter before I learn about MovingAverage and thought that these two functions are similar except that MeanFilter is more powerful for its ability to deal with ...
2
votes
1answer
79 views

Efficient way to perform local Otsu thresholding on image

I have images of cell nuclei that have locally varying intensity distributions. Therefore, I need a robust local threshold algorithm. LocalAdaptiveBinarize works ...
9
votes
5answers
370 views

Most efficient way of making pair list from a list

I have a bunch of datasets which looks like: 3 7 1 6 5 8 2 4 1 2 8 1 5 2 5 ... and I need to make a list of pairs such that the first element makes a pair with ...
0
votes
0answers
97 views

How can I speed up this NM optimization? Most delay seems to be due to long analytic expression

I am struggling with computational speed of a NelderMead optimization. Nevertheless my impression is that I lose lots of speed not only due to the NM optimization itself but rather to for instance ...
-1
votes
1answer
63 views

Looping within linear programming

I am trying to do some network programming around optimization. Here is a reduced sample: ...
7
votes
3answers
202 views

How to speed up auxilary DoolittleDecomposite function?

The Doolittle Decomposition algorithm as below: $$A=LU$$ where $A= \begin{bmatrix} a_{11} & a_{12} & \cdots a_{1n}\\ a_{21} & a_{22} & \cdots a_{2n}\\ \vdots & \vdots & \vdots ...
2
votes
1answer
81 views

speeding up creation of SparseArray

I have to create the following sparse arrays, which are reshaped in a two dimensional matrix, so that i can use the build in Dot[] function to do a kind of ...
6
votes
1answer
60 views

DatePlus and DayPlus perform much worse than my user-defined-function

I was debugging a function speed, and discovered that the time bottleneck was in DatePlus. I tried to change to DayPlus and get ...
6
votes
2answers
130 views

How can I generate a $2n$-point moving maximum of a list as efficiently as MaxFilter?

This is based on the prior question, Moving maximum function?, where @rasher provided two winning solutions (i.e. clearly the most efficient): MaxFilter[list, 1] ...
13
votes
1answer
157 views

Unexpected slowness checking for empty list?

I thought I had a firm understanding of the differences between Equal and SameQ, but this has me puzzled: ...
3
votes
1answer
128 views

How to accelerate my code?

Given a 2D $M \times N$ matrix with non-negative values, I would like to find a path going from top to bottom such that the sum of the path is minimum. My function is defined as follow: ...
3
votes
1answer
168 views

Very slow mathematica finite differences

I have a simple code which solves an equation by an explicit method (FTCS). It takes mathematica several minutes (mathematica 10.0.2) to finish the calculation while the same code in Fortran runs less ...
2
votes
2answers
122 views

NIntegrate over a list of functions

This question is the result of these other two questions. Question 1 and 2. I thought it would be better to ask a new question rather than deleting previous one. I think When ...
0
votes
0answers
16 views

What is the most efficient method to find a single eigenvector with eigenvalue that is numerically 0?

I need to find an eigenvector of a (numerically calculated) square matrix M corresponding to the eigenvalue 0. The methods I know are ...
23
votes
7answers
666 views

Faster position list construction from a “take instructions” list

Take some list, e.g., {1,2,3,4,5}, and a "take instructions" list, say {1,1,2,1,1}. The latter is read as "take the first ...
5
votes
1answer
150 views

Slow exponential evaluation over lists

This question,which is still unanswered, might be relevant because it involves NIntegrate over lists and it also has Exp. In ...
4
votes
1answer
53 views

How to get probabilities for multinomial & hypergeometric distribution ranges more quickly?

I often need to calculate the probability of a result from a multinomial distribution falling within some range, e.g., the maximum of the categories having an upper bound, or a lower bound for the ...
3
votes
0answers
60 views

Decrease computation time of module [closed]

I need to compute the exponential of a matrix (in this caseR) by the summation method where R is a square matrix with symbolic ...
1
vote
1answer
146 views

Slow evaluation of NIntegrate when used as a pure function

I asked a perhaps related question here. Here is my code in below. The goal is that to define a function which must be integrated numerically. The function itself first is calculated over different ...
0
votes
1answer
77 views

Animating a Large Graphic Aborts, but Dynamic Works Fine

I'm trying to animate a large Graphics output, and it is returning $Aborted. I'm confused because when I use Dynamic and Slider, the graphics output is exactly what I want, i.e., I can 'animate' the ...
13
votes
1answer
173 views

Major slowdown when using a ColorFunction with more than one named scheme

Earlier Kuba noted a major slowdown with code Simon Woods posted. Here is a simplified example. First a color function using two different named gradient schemes: ...
0
votes
0answers
58 views

Improve performance of processing transactions in a large file

I want to work with a table which has 296,705 transactionslines and 172,147 transactions. ...
2
votes
3answers
107 views

Finding minimum of a list of pairs

Sometimes I need to find the Min of a list, whose elements are lists (say of two elements, a pair). Given that Min does not have the option to indicate a sorting function like Sort, I elaborated the ...
1
vote
1answer
66 views

Performance problem with summation in Mathematica

I was trying to find out why the following function is rather slow. ...
0
votes
0answers
111 views

Run out of memory. How to optimize this code?

I have two sets of data: short with 22 entries, long with 46 entries. I want to take 20 entries from ...
1
vote
1answer
48 views

Let's do this list-rearrangement prettier [duplicate]

Suppose that i have a list in this format= $list=\{ \{a,obj1 \} ,\{c,obj2 \}, \{a,obj3 \}, \{b,obj4 \} \dots\}$ I want to construct a new list in this way: $ newlist= \{ \{ \{a,obj1\} , \{a,obj3\} ...
0
votes
0answers
108 views

Is there a way to give Mathematica extra memory?

I am running Mathematica 10 (Student Edition) on Mac OS X 10.7.5 on an old Mac Pro. I have lots of extra RAM that the system isn't touching, and I would like to give some of it to Mathematica in the ...
5
votes
1answer
104 views

Slow-down encountered when using NMinimize with a compiled function

I'm minimizing a large function. Evaluating the function is very slow symbolically but using Compile I can get ~100x speed-up on this step. However, despite this, the NMinimize process is only ...
12
votes
1answer
220 views

The efficiency compare between Flatten[#, 1] & and Join @@ # &

Err..Often I met the situation to join lists at the first level and I used to just Flatten[#, 1] &@ them. However, I found(when glance over the mathematica.stackexchange.com) someone else prefer ...
3
votes
2answers
66 views

Slow application of `Outer` on a matrix

I have some performance issue with the calculation of a gaussian kernel applied to a not-that-large dataset. I'm not very good with performance tuning in Mma and I have the feeling this one could be ...
8
votes
0answers
100 views

Efficient image stacking

I have a list of lists of images that I would like to stack and create mean images for each list of images. Images are .jpg files 2592x1944 pixels large. Each list ...
13
votes
3answers
1k views

Can Mathematica's random number generation be improved?

Performance on Random Number generation is intolerable. Whenever you need many deviates from the same distribution but with different parameters it takes forever. Here is a thread on Poisson ...
3
votes
2answers
58 views

How do I apply a ColorDataFunction[] to a grayscale image? [duplicate]

Suppose I have a 2D array im of real values between 0 and 1, representing a grayscale image. I can turn im into an image very ...
6
votes
2answers
981 views

Dramatic speed difference of code on Matlab and Mathematica

Background: I was trying to convert a Matlab code (fluid simulation, SPH method) into a Mathematica one, but the speed difference is huge. Matlab code: ...
1
vote
1answer
55 views

Performance boost for definite integration with symbolic limits

In Mathematica, the symbolic definite integration ...
1
vote
2answers
76 views

Performance in calculating maximum-likelihood- based estimates

My probability density function is a complicated one for which numerical estimation is necessary. Here my pdf: ...
2
votes
1answer
81 views

Simple-looking Minimize takes for ever

Consider the following: Minimize[{(ξ - x)^2 + (η - y)^2, a ξ + b η == c}, {ξ, η}, Reals] After asking Mathematica to do this yesterday afternoon and forgetting ...
3
votes
2answers
78 views

Most efficient way of splitting a file into length-prefixed blocks

Let's say I have a really big file (>100MB) that consists of length-prefixed blocks, i.e. the first byte of each byte block indicates the length of the rest of the block. This is a simplified example ...
2
votes
2answers
62 views

What is the fastest way to get notebook CellGroups / Cells structure?

So the question is how to get this kind of information: from This is one of my ideas: ...
2
votes
0answers
91 views

How to efficiently find parts of a string with beginning and ending specified?

I'm trying to find specific URL addresses from a html document. The addresses can be found between the strings "href=\"" and "\" class=\"listing". The problem is not including strings like this: ...
6
votes
2answers
168 views

Efficient Lloyd Sampling of Images

I want to sample a grayscale image so that every voronoi cell contains approximately the same total intensity using lloyd sampling. My current code is kind of slow and I was hoping for some advice to ...
5
votes
1answer
256 views

Function takes hugely different times to evaluate on sets of virtually (but also literally) identical data

I'm experiencing something which I believe to be weird with a function that I wrote. Briefly: what happens is that I generate random data to feed my function in two different but equivalent ways ...