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)

6
votes
0answers
95 views

Using a Mathematica index as a DiscreteVariable in NDSolve when solving a coupled set of ordinary differential equations

Context Since the explanation below of the problem to be solved is lengthy, let me preamble this by saying that I have code that works to solve the problem, but I don't know whether (1) it's ...
5
votes
4answers
615 views

Distance between two line segments in 3-space

I need to compute the distance between two line segments in a project. After googling, I found this algorithm and used it to implement a Mathematica version: ...
2
votes
3answers
145 views

Efficient matrix multiplication

I have a list of vectors vecs = {vec1, vec2, ..., vecN} where veci is a list with length $N$. Now I have a matrix $N\times N$ ...
4
votes
1answer
117 views

StreamPlot slow in version 10

In version, 10 StreamPlot seems to calculate many more points along each streamline than necessary, compared to version 7. For example, from Mathematica ...
13
votes
0answers
188 views

Why is ListPlot so slow here?

While examining How can I monitor the progress of a Plot? I was surprised to discover that in some cases ListPlot in version 10.0 and 10.1 is orders of magnitude ...
1
vote
1answer
79 views

Apply NonlinearModelFit to an image

I have several images (in FITS format), which I want to fit with a 2D-Gaussian distribution. The problem is that NonlinearModelFit expects data in the form of a ...
8
votes
2answers
219 views

How to read off coefficients of tensor-like expression in a speedy way?

I am considering identities involving t[a, b, c, d, ...], where number of indices is fixed. t has the cyclic property so that ...
5
votes
1answer
116 views

Reap, Sow with Parallelize: bad performance, why?

I have a question about the performance of Reap and Sow with Parallelize. I am aware of the following questions How to collect result continuously (interruptible calculation) when running parallel ...
4
votes
0answers
70 views

How to perform Bayesian optimization with Mathematica?

Does Mathematica have any Bayesian optimization methods for learning hyper-parameters? Practical Bayesian Optimization of Machine Learning Algorithms Jasper Snoek, Hugo Larochelle and Ryan P. ...
5
votes
1answer
106 views

Number of divisors visualized with the QPochhammer function, how improve performance of code?

I have this code that originally is Jeffrey Stopple's code for the Riemann zeta function in the complex plane. Because I discovered yesterday that the number of divisors can be generated with the ...
6
votes
3answers
169 views

Speed up derivative evaluation

I'm trying to calculate the normal vectors and the tangent vectors at the discrete points of a suface. For example: ...
16
votes
1answer
440 views

Speed up adding Poisson noise to an image

I'm simulating some images corrupted with Poisson noise, but I'm encountering a few problems with performance. According to the documentation on ImageEffect, one ...
3
votes
1answer
161 views

How to further accelerate arithmetic with Fermat Pseudoprime and Fibonacci number

I've been working on this all night, and I have made this go pretty fast, compared to my first iteration of the program, but now I'm out of ideas. I'm trying to write a program to test (by good ...
4
votes
2answers
245 views

Why does having many Print statements slow down my code?

I have a code that I need to debug. I have put a Print[] into it that should give about 10^4 prints for table entries. I know that 10^4 is large, and this exposes ...
1
vote
1answer
129 views

Making ReplaceAll faster

I have a list of functions ncalc that is calculated by ...
10
votes
1answer
288 views

Drop selection of columns from a ragged array

I have an application where I need to drop some arbitrary list of columns from a ragged array (where of course the shortest array rows have at least all the specified columns). E.g., given a ragged ...
5
votes
1answer
154 views

Force Dispatch to generate dispatch tables

Dispatch creates hash tables only for lists of length larger than 3. Is there any way to force MMA to create hash tables even for lists of length smaller than 4? ...
17
votes
0answers
226 views

Can code optimize code?

Are there functions, programs or packages that deal with optimizing mathematica code i.e. re-writing ineffecive or convoluted code. -This question has haunted me since I joined SE. The background for ...
1
vote
0answers
50 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 ...
7
votes
2answers
324 views

Using a given colour table with Image?

Context I would like to represent large images with a given colour table. Now, if I use Image ...
14
votes
1answer
192 views

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

Problem remains in 10.1 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: ...
8
votes
1answer
204 views

Why is `Block` significantly faster than `ReplaceAll` for inputting variable values

Given that the documentation for FindMinimum uses ReplaceAll for its various examples of what to do with the found solution to ...
25
votes
0answers
386 views

What Mathematica features and idioms have $n^2$ complexity or worse

Here are a few: AppendTo[data, elem], Sort[data, test], in some cases use of ___ (as ...
4
votes
1answer
86 views

How to create fast functions based on units?

Using units is a good means for rapidly developing code, since it provides some error control. Hovewer, using Mathamatica functions containing units appears to be copmutationally inefficient. ...
11
votes
6answers
562 views

How to find all the positions of max value of a list efficiently?

For Example, list=RandomInteger[{1,100},2000]. Yes,I know Position[list,Max[list]] can do. But it's based on pattern ...
0
votes
0answers
74 views

NIntegrate gives message NIntegrate::vars:

As seen in the code below, I initially constructed a list of functions of two variables ζ and t0. These functions are pure ...
1
vote
0answers
59 views

Orthogonality relations of Hermite polynomials

The Hermite polynomials are orthogonal. $$ \int_{-\infty}^\infty H_m(x) H_n(x) e^{-x^2}\, \mathrm{d}x = \sqrt{ \pi} 2^n n! \delta_{nm} $$ Does Mathematica not use this relationship? Because running ...
14
votes
3answers
597 views

Why is calling Dot with many arguments so inefficient?

Say I want to multiply a reasonably large list of symbolic matrices. ...
11
votes
2answers
187 views

Faster trimming of leading/trailing zeroes in rectangular array

Given a (large) rectangular array, e.g. (small for example case): ...
8
votes
3answers
420 views

Benchmarking expressions

It often happens that I'd like to know which of two or more alternative expressions (that should evaluate to the same value) is fastest. For example, below are three ways to define a function: ...
17
votes
0answers
236 views

Performance problems in new Sequence functions

The new-in-10.1 Sequence* family of functions should provide newly optimized methods for handling sequence problems. Happily in some cases they do! For example ...
4
votes
2answers
47 views

NaNs in tabular data and import performance

I'm working with some data files that are just too large for Import. I tried using ReadList, specifying either ...
3
votes
1answer
117 views

Constructing lists in an efficient way

I have a list of functions and I want to separate them into two lists: a list that contains the functions that have at least one zero and a list that contains the functions that do not have ...
0
votes
1answer
54 views

Returning functions that are partly evaluated

I have some performance issues when calling Table[{h, Slow1[h, n]}, {n, nlist}, {h, 0.1, 10, 0.5}]; for a given list ...
12
votes
1answer
250 views

Why does partitioning increase Dot execution speed?

I have noticed that multiplying a list of matrices can be significantly sped up by partitioning the list, calculating the product of the partitions matrices, and then multiplying the results. ...
0
votes
0answers
39 views

What is the right way to remove head of a number? [duplicate]

I often have a number for which I want to remove its head, e.g. {1}. I know of two ways to remove the head: {1}[[1]] # & @@ {1} Neither of these methods seem ...
5
votes
2answers
146 views

Combinatorics: Placed errors

Consider a vector of length n where each element can take one of the values U, X, ...
7
votes
1answer
143 views

Compiled function crashes kernel when trying to list over different length arguments

I'm trying to speed up a tally/binning operation that I have to run many times. I have many lists of between 1 and 10 numbers from the range [1,5] and I want to ...
4
votes
1answer
71 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
0answers
23 views

Timing of an Evaluation is not correctly displayed

From: http://reference.wolfram.com/language/howto/DisplayTheTimingOfAnEvaluationInANotebookWindow.html I ran a process that took about two minutes to finish. But the time recorded is only 0.18 ...
0
votes
0answers
37 views

Speeding up a “filling in” process

I have a sparse EventSeries that looks something like ...
0
votes
0answers
57 views

Computing the coefficients for Gaussian integral

I wrote a small script to find the Gaussian integral points and weights: ...
2
votes
2answers
151 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 ...
5
votes
3answers
257 views

Split list of states by run lengths and discontinuous changes in state

I was asked to look into writing some simulation code for a user, where the simulation output (x) would oscillate between three possible states: ...
0
votes
2answers
87 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
53 views

How to speed up solution of system of recurrence equations

I was wondering how I can improve the speed on this method for solving a particular system of coupled recurrence equations. The system is $$ \begin{align*} &V(m)=\alpha + \beta V(m+1)+\beta ...