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
4answers
219 views

Getting AbsoluteTiming to display in hours, minutes, and seconds

I'm doing some extended length calculations and was wondering how I could get AbsoluteTiming to display in hours, minutes, and seconds. I googled and found nothing, ...
3
votes
3answers
187 views

Replace For with If in Mathematica way

I have this code: ...
2
votes
1answer
81 views

Sampling a Dirichlet Distribution efficiently

I'm sampling a DirichletDistribution like so: ...
4
votes
1answer
288 views

Writing Faster Mathematica Code - Sow and Reap?

I am applying Select function to a large list called xyCoordinateCentreCircle. The dimensions of the list are: ...
3
votes
0answers
116 views

Mathematica Newmark Optimization

Here is my Mathematica code which implements the Newmark method to solve a equation of motion. The variable "ag" contains the accelerations from an earthquake record. Is it possible to optimize this ...
1
vote
0answers
63 views

How to speed up NDSolve?

I have a program where I need to iterate NDSolve thousands of times for different configurations of a diffusion problem. The thing is, I only need about 3 digits of accuracy. I looked thru previous ...
0
votes
1answer
70 views

Faster code for calculat in Integral include derivative

I saw this question How I can get faster I tried help that. But I can not understand. I want to write a faster code for: ...
10
votes
1answer
140 views

RandomInteger: speed with long lists

I noticed an interesting effect with RandomInteger when looking at the time to create 10^8 random integers. The following code lines all take about 0.5 seconds on a ...
15
votes
1answer
135 views

Is it better to completely forget about the existence of PowersRepresentations?

I noticed that in several cases the performance of PowersRepresentations is hugely worse than that of IntegerPartitions. (Mma 10....
0
votes
1answer
77 views

Why are CoefficientRules and MonomialList so slow?

Why is CoefficientRules so slow in this example (v10.2 on OS X 10.11.4)? ...
8
votes
0answers
132 views

Exporting a high-resolution GraphPlot of a very large graph (65,536 nodes)

Problem I have a very large graph of 65,536 vertices. GraphPlot yields: But the vertices are too dense to see in any detail, and attempting to use any of the on-...
1
vote
0answers
58 views

How to speedup Plot3D with precompiled argument

I have a huge problem with my code: I estimate it to take hours to run. The scheme of the code is the following (I left the steps the same, but took much simpler functions): ...
5
votes
1answer
155 views

Efficiently importing data into Mathematica

I have a graph data file infile.txt. 3 2 0 1 1 2 3 2 0 1 2 1 3 2 1 0 1 2 3 3 0 1 1 0 1 2 3 3 0 1 1 0 2 1 3 4 0 1 1 0 1 2 2 1 ...
18
votes
1answer
502 views

Numerical inverse Laplace-Hankel transform

When trying to reproduce the result of this paper about numerical solution of Lamb's problem, I encountered the following double integral (to be more precise, the 0-order inverse Hankel-Laplace ...
1
vote
1answer
170 views

Why timing of FindRoot is almost the same even if the function is speedup significantly

This question is almost the same issue as this one. However, it seems that it didn't draw enough attention. Now I encountered the same problem. I previously make this post, I will copy the code here. ...
3
votes
0answers
74 views

What is the good source to study advanced custom coding in Mathematica [duplicate]

My question is, probably, too general ... I deal a lot with complicated symbolic evaluations, integrations, plotting. I have troubles with optimizing my codes. They come out way too slow. Google and ...
2
votes
3answers
114 views

Strong glitch with Evaluate in Manipulate

I'm experiencing a strong evaluation problem for a Manipulate box and I can't find the best solution yet. Here's a MWE to show the glitch : ...
6
votes
1answer
137 views

Calculate the ESP Grid based on Density Grid

I want to build a fast way to convert a density grid file into a grid file of the respective electrostatic potential (ESP; also known as MEP, the molecular electrostatic potential). This ...
9
votes
0answers
121 views

Is there a linter to help write good Mathematica code?

I don't do much real programming, but I've recently been turned on to Atom, which has linter plugins for over 50 languages to make coding easier (most drawing on pre-existing lint-like programs). In ...
2
votes
1answer
68 views

Can this simple loop with several counters be improved or parallelized

I am running the following calculations. The timing for H = 2000 is about 8 days on my machine. Can this be parallelized or improved upon in some other way. Also I am trying to avoid storing the ...
9
votes
1answer
199 views

Fast implementation of Metropolis-Hastings update with multiple sets

This is an optimisation question rather than something I am unable to do. I have a naive, primitive working version of the code (I submit it below) but I am trying to make it work faster. I am asking ...
3
votes
1answer
99 views

Speed up computation of sum from large matrix

I have a 3x3 matrix (252^3) of data (densities) and I want to compute a correlation xi from nearest neighbours as well as a sum involving a check whether the density is in a certain bin. The ...
5
votes
0answers
79 views

Is it possible to speedup these simple linear algebra operations

I'm trying to numerically solve some equations using splitting operator method. The solver I construct iteratively constructs a matrix and feeds it to LinearSolve. ...
9
votes
3answers
348 views

Give a element rank list of a list

SeedRandom[417] list1 = RandomChoice[Range[22, 28], 4] {22, 24, 26, 23} We can get the rank list ...
0
votes
0answers
48 views

How to calculate a continuous function once and access values later?

I have a function that involves a numerical calculation: h[v_]:=NIntegrate[f[x]*Log[p[x, v]], {x, -Infinity, Infinity}] I need to visualize how well a bunch of ...
2
votes
1answer
65 views

Twin Prime Max Gaps (Performance Tuning)

Ok, let's build a foundation here: A common way of testing primality, is dividing by all primes smaller than the number's square root. For instance, $97$ is prime because dividing by none of the ...
4
votes
0answers
67 views

Sudden changes in performance

Last week Mathematica was working fine. At the beginning of this week I went on a trip and it slowed down. I ran Needs["Benchmarking`"] BenchmarkReport[] The ...
17
votes
3answers
517 views

How to compute the boundary of a group of ellipse efficiently?

Update1 I discovered that the bountary of a series of ellipses consists of the following three parts Part (I): the first ellipse's effective black-segment; Part (II): the effective ...
2
votes
0answers
74 views

FourierCoefficient performance

I encountered weird performance issue while using FourierCoefficient[]. I narrowed it down to calculating $n$-th coefficient for $\cos(nx)$. ...
11
votes
1answer
155 views

Why FoldList 5 times faster than Apply for matrix multiplication? [duplicate]

Consider this list of $2\times 2$ matrices: list=RandomReal[{-2,2},{500,2,2}]; Now compare timings of Apply and ...
6
votes
2answers
357 views

Performance of calculation of means squared displacement

How can I improve the performance of the following code which calculates the mean squared displacement for coordinates stored in data? Here for each time difference ...
3
votes
0answers
34 views
0
votes
0answers
70 views

How to compile code that involve sparse array?

I have the following code ...
3
votes
2answers
145 views

Fast way to evaluate the PDF of a multivariable distribution

I'm trying to evaluate the PDF of a truncated multi-normal distribution. My distribution has 8 dimensions and each value must be ≥ 0. ...
13
votes
3answers
544 views

Particles bouncing in a 3D box

I made this Manipulate code which shows a pack of particles randomly moving inside a box. It has a huge performance issue. What should be the best way in doing "...
2
votes
0answers
65 views

Estimating processing time for commands

I am trying to evaluate some equations by using Taylor series of expansion (Series[], ExpandAll[], and Normal[] command), and using n equation and n unknown (Solve). Following is not full command (if ...
4
votes
2answers
115 views

Efficiently strip off coefficients in front of variables?

I am working with multivariate polynomials and need a very efficient way to decompose monomials into coefficients and pure monomials. for instance consider variables ...
1
vote
1answer
167 views

How I can get faster code for computing a fractional integral?

I use of this code Fractional Integral and wrote the following code: ...
4
votes
1answer
204 views

Mathematica 10.X benchmark report for high-end surface pro 4/surface book?

Recently, I am seriously considering purchasing the high-end surface device (with intel i7 and maximal RAM). Mathematica will be one of the main applications. Since it is not a full or traditional ...
1
vote
0answers
82 views

How to speed up the construction of a large matrix

As a part of a physics problem that I'm working on, I need to construct a rather large matrix. The matrix is a real symmetric $2^{m}\times 2^{m}$ matrix for some integer $m$, which we would like to ...
3
votes
2answers
89 views

Perform matrix/tensor contractions more efficiently

Problem: explicit index contractions for matrices using the Sum command take too long, and I want to improve the performance for complicated computations. Let me ...
10
votes
2answers
203 views

ParallelTable behaves differently when it is run inside a package

I've been butting my head against some very weird behaviour by ParallelTable, and its interaction with packages, over the past few weeks, and making relatively ...
1
vote
0answers
95 views

Need help solving a polynomial system [closed]

I need help solving the following system of equations: ...
1
vote
2answers
94 views

Asymptotic form of the “strange” function

I want to find the asymptotic form of this function ...
1
vote
1answer
106 views

How to make AppendTo faster

Function AppendTo seems to be very slow for generating a lot of scenarios, is there any chance to change it and make it faster? ...
8
votes
5answers
295 views

What is the most efficient way to replace items in a list at periodic intervals?

For example, if I want a list of $\phi(n)$ for $1 \leq n \leq 100$ but if $n$ is a multiple of 3 I want to show a 3 instead of $\phi(n)$. I tried ...
0
votes
0answers
43 views

Finding minima of a function: Ways to speed up the code [duplicate]

I am quite new to Mathematica. I tried to calculate all minima of the function of interest as follows: ...
1
vote
1answer
61 views

Fast way to compute intersection of equivalence classes

There is a set $S=\{1, 2, \dots, m\}$ and $n$ equivalence relations on the set: $a \sim_i b $, where $a,b \in S$, $i=1, \dots, n$. Now we define $a \sim b$ iff $ a \sim_i b$ $\forall i=1, \dots, n$. ...
0
votes
1answer
75 views

A custom function is too slow when I first time to run it

I make a function like this ...
10
votes
3answers
459 views

Radial distribution function

I have tried to rewrite my old IDL code to calculate the radial correlation function for a regular 2D crystal structure. The theory behind this function is given here: https://en.wikipedia.org/wiki/...