# Tagged Questions

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.

79 views

### Problems with NMaximize [duplicate]

The Objective function I would like to Maximize takes less than a second to evaluate. There are several packages needed to evaluate it, so I'm not showing it here. ...
135 views

### Speed Up Sampling of Perimeter of Polygon

I have a polygon whose vertices (points) are defined by some image coordinates that I have manually selected. The polygon is used to create a mask for a region of interest in a microscopy image. ...
189 views

### Replace For with If in Mathematica way

I have this code: ...
157 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 ...
292 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: ...
121 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 ...
73 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 ...
72 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: ...
565 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 "...
143 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 ...
508 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 ...
136 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-...
79 views

### Why are CoefficientRules and MonomialList so slow?

Why is CoefficientRules so slow in this example (v10.2 on OS X 10.11.4)? ...
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 ...
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 : ...
93 views

### More efficient way to generate lattice graph with edges between visible vertices?

Given a nXn lattice, I need to generate the graph such that there is an edge between two vertices iff they are directly "visible" to each other, i.e., there is no vertex between them. I'm using <...
174 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. ...
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): ...
156 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 ...
75 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 ...
205 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 ...
70 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 ...
366 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 ...
3k views

### Profiling from Mathematica

I've always wished I could do some profiling like you get in Wolfram Workbench, but directly from Mathematica, without using or having Workbench. If it is possible, how can I do it?
125 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 ...
94 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 ...
103 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 ...
350 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 ...
80 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. ...
90 views

### Optimization of custom probability distibutions?

Consider the two tests: Timing[Do[RandomVariate[NormalDistribution[0.5, 0.3]], {200}]] {0.001188, Null} ...
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 ...
49 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 ...
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 ...
74 views

### FourierCoefficient performance

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

### Performance of Apart with complex numbers

Consider the following code ...
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. ...
79 views

### How to compile code that involve sparse array?

I have the following code ...
172 views

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

I use of this code Fractional Integral and wrote the following code: ...
206 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 ...
117 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 ...
91 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 ...
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 ...
84 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 ...
96 views

### Need help solving a polynomial system [closed]

I need help solving the following system of equations: ...
471 views

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/...
95 views

### Asymptotic form of the “strange” function

I want to find the asymptotic form of this function ...
109 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? ...
107 views

### Want Manipulate to be low resolution enough to render fast but no so low resolution that it is useless

When using Manipulate to graph a simple surface (sum of 2 sine waves) with Plot3D, setting PerformanceGoal->"Quality" makes it take about 1/3 second to render, while "Speed" sets it at such a low ...
64 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$. ...
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: ...