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)

10
votes
1answer
679 views

Modular arithmetic - efficiently calculating the remainders of factorials

When working on this question regarding the divisibility of the sum of factorials, I decided to write some code to test "small values" of the problem using the following code. ...
2
votes
1answer
154 views

How to determine whether an expression can act as a “good” function?

It's not easy to see whether an expression can act as a "good" function. A good function refers to an operater that transforms symbolic entities like 1+x^2 or ...
11
votes
5answers
417 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 ...
16
votes
2answers
295 views

How to improve performance of BesselJ to the level of GSL?

Consider the following code: zs = N /@ Range[0, 12, 10^-5]; AbsoluteTiming[bessels = BesselJ[1, #] & /@ zs;] Length@zs I've tried to measure only computation ...
1
vote
1answer
60 views

How to perform multi-columns selection and/or !MemberQ on large lists with speed?

This question (2 subquestions) is connected to How to speed up Select with MemberQ for large lists? Q1: I would like to have an alternate version of the form below to do a multi-columns selection on ...
20
votes
1answer
364 views

Why some built-in functions are slow

I just wonder why some built-in functions are slower compared to some other ways using more than one built-in functions for doing same job. consider this example: ...
9
votes
2answers
236 views

Relaxation of Mass-Spring System with Pinned Masses

Problem Assume that four masses are connected to each other by springs while two of them are pinned down at their initial positions. Find the position of masses when system is at its minimum ...
1
vote
0answers
61 views

Efficient way for solving a matrix equation

I am looking for a more effcient method to perform the following analysis. I did my best, but running the following code on my machine takes 9.766s. I need to perform this analysis several times. ...
4
votes
3answers
2k views

Definite integral takes a very long time

I'm trying to integrate the following function: $$r_e=\int_0^z\frac{dz}{\sqrt{\Omega_m(1+z)^3+\Omega_\Lambda}}$$ where $\Omega_\Lambda=1-\Omega_M$ and both $\Omega_\Lambda$ and $\Omega_M$ are real ...
1
vote
2answers
123 views

Slow program- Mathematica shuts down

I've been trying to draw dynamic pictures of level sets of certain polynomials. The code right now looks like that : ...
5
votes
2answers
129 views

How to speed up Select with MemberQ for large lists?

I have two large lists: list1 and list2. I would like to select elements of list1 only if they appear in list2. Since the list1 has dimensions of (335000, 2) and list2 : (122000, 4), it's taking a ...
6
votes
1answer
91 views

SameQ for unsorted lists

I have to compare two or more lists and check if they are the same, but their elements can be in different position. For instance ...
2
votes
1answer
98 views

Compiling a summation

I have a sum over Legendre polynomials which I'm trying to speed up. I've come across the suggestion of using Compile to speed up code in previous answers around ...
5
votes
4answers
669 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
157 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
121 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 ...
14
votes
0answers
205 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
82 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
231 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
133 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
79 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. ...
6
votes
3answers
182 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
489 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 ...
4
votes
1answer
170 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
254 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
133 views

Making ReplaceAll faster

I have a list of functions ncalc that is calculated by ...
10
votes
1answer
294 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
158 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
240 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
57 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
339 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
194 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: ...
27
votes
0answers
431 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
92 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
574 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
75 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
65 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
620 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
189 views

Faster trimming of leading/trailing zeroes in rectangular array

Given a (large) rectangular array, e.g. (small for example case): ...
9
votes
3answers
445 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: ...
18
votes
0answers
317 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
48 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
122 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
273 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. ...