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)

15
votes
1answer
268 views

Efficient lazy weak compositions

In Mathematica all weak k-compositions of integer n can be generated using permutations of integer partitions: ...
8
votes
4answers
167 views

How to delete subsets quickly?

I have some data like this: ...
2
votes
3answers
103 views

My evaluation is taking a lot of time

I was evaluating the roots of a transcendental equation, when I noticed that Mathematica never finishes but stays in the running state. The following is the code that I was using: ...
10
votes
1answer
55 views

How to speed up the evaluation of a function with a large LeafCount but many repeated sub-functions for use in NIntegrate

General Context Apparently, the memory leak in NIntegrate is unavoidable pre-MMA-10.2. I have some (two-dimensional) numerical integration to do on a complicated ...
6
votes
2answers
240 views

Modifying the elements of a list according to a given probability

Let list be a list of lists, the elements of which are random integers in the interval $[1,c]$. Each element $i$ has a probability ...
-1
votes
0answers
71 views

Trouble summing over many indices [on hold]

I'm trying to solve a problem by using `Sum, but as I have many indices (14) in the summation, the running time is awful. My program looks like this: ...
1
vote
2answers
94 views

How can I speed up this code with 4 variables sum

I have to make a sum over 4 variables. My code is very very slow. I want to know how to speed up this code. This problem is related to but different from one previous problem. Any help or suggestion ...
0
votes
0answers
57 views

How to accelerate Simplify and Series? [closed]

A recent project of mine requires a lot of symbolic calculations in Mathematica and is very time consuming. By experiments I have found that the most time consuming parts are: ...
6
votes
1answer
132 views

Is it possible to speed up the user-defined function CAGDBezierSurface?

For given control net of points $P_{i,j}, \quad i=0,2, \cdots m, \quad j=0,2,\cdots, n$,the Bézier surface is defined by the following equation: ...
6
votes
0answers
49 views

MathLink (WSTP) vs LibraryLink performance

Has anyone benchmarked LibraryLink vs MathLink when passing large arrays? Specifically I would like to know how performance compares when using the following to pass and return a large array: ...
3
votes
0answers
79 views

Importing large number of frames form Video - improved method?

I am trying to work with large numbers of frames from a video (30000+). I see there are a number of answers out there, but I find none of them satisfying - see below for details. I understand from ...
15
votes
1answer
536 views

Use Compile to improve performance

I've recently discovered Compile as a way to optimize my code. I wrote a program to learn something new about Compile. I have a ...
17
votes
3answers
442 views

Improving speed of code computing number of nonrepeating partitions

I need to answer the following for a number of parameters: How many ways can the integer $k$ be written as a sum of $n$ different integers ranging from $1$ to $m$? My initial attempt was the ...
11
votes
3answers
201 views

How to implement a more efficient inverse triangular recursion?

Consider the following inverse triangular formula $$\left( \begin{array}{ccccc} & & & & N_{i-p,p}\left(u_0\right) \\ & & N_{i-2,2}\left(u_0\right) & & ...
0
votes
0answers
38 views

Simplifying sums of rational trigonometric functions

After simplifying the trigonometric functions as advised before (cf. Simplifying Expressions for FindMinimum), I obtained a sum of 62 similar expressions, the first three as in the below: ...
1
vote
1answer
110 views
9
votes
4answers
158 views

Faster position-based duplicate removal in a ragged array?

Take a ragged 2D array: rArray={{4, 2, 1, 0}, {2, 2}, {0, 0, 3, 3, 3}, {4, 0}, {3, 4}} I need to "deduplicate" this, where a duplicate is any element of a ...
11
votes
2answers
190 views

Fastest way to sum the upper triangle

I feel like this is an recurring question: if there's a symmetric matrix whose diagonal is not all 0, how could I get the sum of the part of it that's above the diagonal as fast as possible? Small ...
6
votes
0answers
64 views

Fast Partition of list at depth 2

I am looking to partition a list up at a certain depth level. Following code should demonstrate what I mean: ...
2
votes
1answer
75 views

Clean & efficient bounding box non-intersection test

I would like a quick test to tell me that segments $ab$ and $cd$ cannot intersect in the plane. If the maximum $x$-coordinate of $a$ and $b$ is less than the minimum $x$-coordinate of $c$ and $d$, ...
0
votes
0answers
43 views

Hook Length Formula and Ferrer's Diagram

So, I am new to using Mathematica, and trying to write some code that needs to use hook length of each cell in Ferrer's diagram of a partition. I first thought of using for loops to manually count ...
0
votes
0answers
42 views

Is there a way to pre-calculate a slow animation and then run easily with no lag? [duplicate]

I have an animation of a triple pendulum on a moving base, however even after trying to change the whole coding to make it faster by using module and all it is still pretty bad as I need it to be ...
25
votes
1answer
285 views

Solve slower in versions 9 and 10

With some systems of equations, Solve is much slower in versions 9 and 10 than in earlier versions, apparently because it is doing more simplification of the ...
0
votes
0answers
57 views

Why is kernel quitting during Manipulate and CurrentImage

I have a Manipulate that captures a camera image using CurrentImage, does some (fairly complicated) processing on the image, and ...
2
votes
1answer
79 views

Simplifying Expressions for FindMinimum

I am currently trying to apply the FindMinimum function to find the lowest energy of an expression. My current expression as of now is relatively complicated. It is the summation of 48 similar terms, ...
2
votes
2answers
147 views

Efficient List of Lists

I'm running some analysis that requires the creation, row by row, of a large list of lists. The big list is called "Nodes" and it will ultimately contain hundreds of thousands of rows. The first few ...
1
vote
1answer
83 views

Image manipulation is too slow

When I hit shift+enter at a document, I must wait for a long time. But AbsoluteTiming function indicates it just spent under 1 second. as follows. I think it shows that only 0.24sec took to ...
3
votes
1answer
52 views

Fishing for monomials in a nested or partially factored polynomial stream

I have a problem where I'd like to be able to take a multivariate polynomial whose variables are nonscalar and is not written explicitly as a sum of it's nonzero monomial terms and determine which ...
19
votes
2answers
363 views

Speeding up the built-in Rudin-Shapiro and Thue-Morse sequence functions

Version 10.2 introduced two well-studied sequences as functions: the (Golay-)Rudin-Shapiro sequence (RudinShapiro[]) and the (Prouhet-)Thue-Morse sequence (...
2
votes
3answers
122 views

Integrate over Piecewise function efficiently

I have this Piecewise function: ...
4
votes
2answers
88 views

The fastest way of raising indices of high rank tensor

I have some high dimensional high rank tensors, let's say $$F_{ijkl}$$ and I need to find $$F^{abcd}=g^{ai}g^{bj}g^{ck}g^{dl}F_{ijkl}.$$ Here $g^{ij}$ is the contravariant metric. Simple summation ...
4
votes
0answers
120 views

CompilationTarget to C takes long compilation time

Consider I have a function, with one version that compiled to the virtual machine and one that compiled to C: ...
7
votes
1answer
99 views

How to compile procedural program effectively

Consider these four function in different form: ...
1
vote
1answer
52 views

Adding another looping element with a list

I have code that is looping on to elements and is working shown below ...
1
vote
2answers
78 views

Replace For-Loop to speed up code

I need to speed up this code ...
5
votes
1answer
110 views

Composition vs serial assignment, which performs better?

I have a giant block of code that manipulations an expression in many different ways. Currently it looks like ...
6
votes
0answers
248 views

Visualization of traces of moving objects

An example movie which is made with the listed mathematica code below shows traces of moving objects: https://youtu.be/8jN2uuSHCEg (Melting ...) http://youtu.be/5ET62846C-I (Crystallisation ...) ...
3
votes
2answers
112 views

Image smoothing with own smoothing function

I have thousands images similar to the following one: This image was produced with the following color table: ...
0
votes
0answers
48 views

Compilable function with the functionality of SingularValueList

I'm doing something that involves finding the singular values of a lot of matrices. Basically I need to find singular values of $n$ matrices of dimension $n\times n$, where $n$ is as large as ...
19
votes
1answer
337 views

Faster binary Hamming weight for big integers?

While working on an answer to Count the sequences in an array I found that DigitCount was the bottleneck in my code when used as ...
2
votes
1answer
59 views

Increasing Plot3D speed when plotted function is Eigenvalue

This is probably a really simple fix, but I'm fairly new to Mathematica and am not sure how to do this properly. I know that trying to plot eigenvalues directly can be computationally expensive, so ...
1
vote
0answers
59 views

Improve speed of evaluating a sum over four indices

I am trying to implement the Fox-H function with several variables as sum of residues. I have arrived at the following function; ...
3
votes
0answers
47 views

Why is the Max function so slow when operating on lists of dates?

Example: dates = DateRange[DateObject[], DatePlus[DateObject[], 15]]; First[AbsoluteTiming[Max[dates]]] (* 21.364247 *) As a workaround, ...
2
votes
2answers
102 views

How to Optimize Solution? (Project Euler #14) [duplicate]

Link to Question: https://projecteuler.net/problem=14 I began by defining the following functions below. ...
6
votes
4answers
740 views

Finding number of pixels with certain RGB color in an image

I have the following image: To determine the number of black pixels (nBlackPixels) I use: ...
5
votes
3answers
84 views

Conditional subset generation

I am trying to accomplish the following task: Given a list of coordinates of n points in the plane, I need to find subsets of all points that share the same ...
0
votes
0answers
34 views

Numerical minimization hangs at some steps

Let me briefly explain what I observe and cannot really explain by myself. I occasionally use NMinimize to find optimal parameter sets and sometimes it happens that ...
2
votes
2answers
161 views

Optimize evaluation time for a Quantum Walk simulation

Here's the problem. I'm trying to simulate a noisy Quantum Walk to obtain results similar to these, experimentally obtained. I wrote my code, and I get results which are in accordance to what I ...
14
votes
3answers
402 views

Solve a trig equation system

Background of the problem In the same plane, P is a fixed point, A,B,C are moving point, PA=a, PB=b, PC=c, find the maximize perimeter of △ABC. let ∠BPC=A, ∠CPA=B, ∠APB=2*Pi-A-B, then the ...
9
votes
0answers
97 views

Mathematica is getting slow when repeatedly calling an external program

I found a strange phenomenon that Mathematica is getting slow when I repeatedly called an external program by using ReadList. For example, the following code will display a significant increase of CPU ...