Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

2
votes
1answer
41 views

Unable to use `Solve` to solve simultaneous equations, or use `Maximize` for exact maximum solution

Mathematica has found that the maximum of the following function is negative using NMaximize, but I am trying to prove that the maximum is negative (i.e. that the ...
1
vote
1answer
58 views

Is there a way to automate this procedure?

I have some code which numerically solves a differential equation for a given value of the parameter $M0$ and for some initial conditions. The initial conditions will remain fixed throughout the ...
0
votes
0answers
56 views

Integral evaluating very slowly

I am trying to evaluate the following simple integral: ...
3
votes
3answers
300 views

Speeding up the calculation of a binomial sum

Is it possible to speed up this calculation? A[j_, p_, n_] := Sum[Binomial[n, k] p^k (1 - p)^(n - k), {k, 0, j}] Plot[{A[j, 0.5, 12000]}, {j, 0, 12000}] Thanks!
1
vote
2answers
31 views

Reduce Plot3D evaluation time?

I have a large function func[a,b] and I need its Plot3D. Since the command ...
4
votes
2answers
57 views

Code to generate series expansion ran fine in M10, not in M11

I have a simple code to get a planet's true anomaly as a function of time, using Newton's method and a series expansion. (Note that I purposefully iterate Newton's method two more times than the order ...
0
votes
2answers
72 views

How to speed up this calculation regarding the normal distribution?

This calculation takes a very long time! Why? How can I improve it? Thanks! ...
2
votes
1answer
42 views

Crashes of Classify function

I'm working on a classifying two classes. I am training my Classify function with the RandomForest method, but it crashes. The ...
3
votes
2answers
74 views

Label Product of a Graph with itself

Defn Let $\mathcal{G}=(\mathcal{V},\mathcal{E})$ and $\mathcal{G}' = (\mathcal{V}', \mathcal{E}')$ be two labeled graphs with alphabet $\mathcal{A}$. The labeled graph product $\mathcal{G} * \mathcal{...
9
votes
3answers
454 views

Create lists of equivalences from pairs

I have a list of pairs, e.g., ex1={{1,2},{1,3},{2,3}, {4,5}, {6,7},{6,8}, {7,8}} that I would like to merge into the list ...
10
votes
0answers
84 views

What changed in String handling between 11.0 and 11.3?

I was trying to optimize some String-handling code here using my standard trick of converting it to a list of bytes first and using basic list operations instead of ...
3
votes
0answers
66 views

Getting rid of discontinuities in plots caused by square roots, logarithms, `Arg`, etc

I encounter this very often, here is just one recent example: I want StreamPlot[ReIm[Sqrt[x+I y]],{x,-3,3},{y,-3,3},StreamStyle->"Line"] and because of square ...
1
vote
0answers
34 views
0
votes
0answers
51 views

compiling matrix row assignment

I have this relatively simple code, generated with another Mathematica code (this is just an example): (edit: see below for simpler version) ...
1
vote
1answer
39 views

use GPU/CUDA to speed up rendering of surface

I am attempting to create several surface renderings of expressions that come out of a solution for the eigenvalues of a matrix and the resulting expressions are considerably messy The eigenvalue ...
1
vote
1answer
55 views

Distance Correlation

I am interested in calculating the distance correlation between a pair of vectors. To do it I use CorrelationDistance function, but I get a distance greater than 1. ...
3
votes
1answer
137 views

Can this power tower function be optimized to perform faster?

Take the following function defined in Mathematica: Itr[x_, p_, n_] := x^Nest[Power[p, #] &, 1, n]; Evaluating this function even for small values of ...
0
votes
2answers
39 views

Enhance performace for custom code

I need to make this code faster and consume less memory. ...
3
votes
1answer
46 views

SystemException[“MemoryAllocationFailure”] with compile and speed issues

I am trying to run a code that gives out a matrix but, I seem to encounter some memory issues while trying to run it. The code is: ...
0
votes
1answer
70 views

Increasing the speed of sparse array

I am trying to find a sparse matrix of the size Col x Rows = 16777216 x 1024 where all the elements are 0 except the ones given ...
6
votes
1answer
81 views

Clever caching of a recursively defined function

I've got a function of the form f[n,s] where n is a natural number and s is a finite ...
5
votes
1answer
90 views

How do computational resources required for NDSolve scale with integration time?

In a nutshell I have a large system of ODE. When I integrate the system from x = 0 to x = dx, ...
0
votes
1answer
33 views

Optimization with constraint or with the constraint embedded into the problem?

I need to find the minimum of a function. However, the condition is that the final state should be normalized i.e. sum of the squares must be one. One way to deal with it is just add the constraint ...
0
votes
1answer
95 views

What is the fastest way to build a simple table?

Question: What is the fastest way to obtain a table of values from a parametrized function? For a function of x, which is a sum of ...
3
votes
1answer
57 views

Whats the difference between Cases and Select? [duplicate]

Im trying to further my understanding of mathematica coding and came up across this question. What is the main difference between Cases and ...
0
votes
1answer
53 views

Why does Table[] slow down exponentially with increasing length? [closed]

I haven't been able to find anything to specifically answer my question: When using Table[], I find that, for a program I am working on (I would post the code but ...
16
votes
3answers
1k views

Efficient way to simulate thousands of Markov chains

I am currently trying to simulate relaxation of a protein population while maintaining the stochastic properties of the system. For this, I used a Markov chain to describe the temporal evolution of ...
4
votes
1answer
78 views

Tips for Complicated Indefinite Integrals

I would like to have an analytical expression for some complicated indefinite integrals (example below). I am able to solve these integrals numerically, and I am able to plot them in Mathematica, ...
6
votes
1answer
271 views

What is wrong with this code? (Usage of FunctionInterpolation and how to make code efficient)

I am trying to create an interpolation out of a function that comes from an improper integral. The end goal is to make a 3d plot over a specific region. Specifically, I have the following code: ...
5
votes
1answer
142 views

Perform BFS for the 8-puzzle

I've tried to implement Breadth First Search algorithm in MMA, to attempt to solve the 8-puzzle game. But in some cases, I ran out of memory, but on other cases it solves without problem. Here is the ...
2
votes
1answer
36 views

Rank 3 Tensor Multiplication Memory issue

I'm doing a complicated tensor multiplication experiment. Imaging there are two Rank 3 tensors generated as below: ...
3
votes
2answers
88 views

Implementing Gauss–Laguerre quadrature for double integral of oscillatory function

I need an efficient way to evaluate a certain double integral for a wide range of parameters: $$I=(\gamma_l \gamma_L )^{3/2} \int_0^\infty \int_0^{1} \frac{r ~d r ~dx}{\sqrt{r^2+b^2 x^2}} \times \\...
7
votes
2answers
193 views

Can Mathematica Take Advantage Of a AMD Ryzen Threadripper 2990wx 32 Core Processor?

I have a student version of Mathematica. I'm currently running it on my 8 Core Desktop. On my current Desktop I use it for Numerically integrating expressions 1000s of times, the ParallelTable ...
1
vote
0answers
111 views

Finding an optimal location of 2D points?

I have a function f that returns the number of triangles formed by given line segments. For example, in the following case f ...
5
votes
3answers
142 views

Improving the run time of random walk simulation

One of our professors told us that during his thesis he used Mathematica in simulating it since it is about networks, I don't know how he did it but all I know is he said it took about two weeks to ...
1
vote
1answer
70 views

Sparse matrices dot multiplication

I have two huge sparse matrices, and I am interested in dot multiplication them. But I have a memory issue so a notebook is a shutdown. I am using the 11.3 version. Any suggestion on how to avoid ...
12
votes
1answer
286 views

Speed up NDSolve compared to Python (calls to LSODA)

I migrated a numerical model code from Python to Mathematica and am surprised how much faster the Python version runs. Profiling of the Python version tells me that it is about 100 times faster (120 ...
1
vote
0answers
20 views

keep variables in the correct position when TensorExpand

I would like to expand a tensor expression of 3-d vectors. However, the code runs really slow, the problem is asked here: Why is TensorExpand so slow for vector operations? However, I found that by ...
3
votes
2answers
112 views

Why is TensorExpand so slow for vector operations?

I would like to expand the following tensor expression: ...
2
votes
0answers
69 views

How to speed up multiple calls of NonlinearModelFit?

I'm trying to do an insane amount of non-linear fits to large amounts of data sets. However, I found that the NonlinearModelFit is horribly slow. Here's the setup: ...
1
vote
2answers
45 views

Filtering sparse matrix

I am interested in filtering a sparse matrix, where the values are between zero and one. I do it in the following manner. ...
0
votes
0answers
24 views

Help with Plot excluding parts of a curve even with “Exclusions” option set to “None” and speed up of code

I modeled a system mathematically. When the model is ideal, I can plot the results and export the plotted points. Everything works fine. Now, I started to model some imperfections of the system, but ...
6
votes
1answer
145 views

How to speed up evaluation of expression (multiple variables) to gain speed? Compile?

I obtained some long expression and need to use them with a lot of different parameters inside a simulation. However, evaluation of the expressions is pretty slow, so that I would like to speed it up. ...
1
vote
1answer
35 views

How to avoid initial large number evaluation in LogLogPlot

If you run this: LogLogPlot[Echo[x], {x, 10000, 100000}, WorkingPrecision -> 10] You will see the first evaluation point is at a very very large number ...
3
votes
1answer
65 views

Efficient compilation of a list of functions

Let's say that we have at our disposal nbfunc = 4; functions, whose exact expressions are gathered in the list ...
0
votes
0answers
59 views

When I use Plot to plot an interpolating function which is the solution of a big ODEs solved by NDSolve,I found it takes a long time

I'm a Chinese student.I can't speak English very well.If there are any mistakes,please forgive me. I use NDSolve to solve a big ODEs. ...
1
vote
0answers
54 views

NDSolve for a system of PDEs with piecewise coefficients

I want to solve a system of 3 second order linear PDEs with homogeneous Dirichlet boundary conditions. The code I use is as follows: ...
2
votes
1answer
47 views

Optimisation of a loop

I've opened a question here, which was answered. The code works totally fine for a small set of data. However, for a large set and over many iterations, it will take a long time (in order of 24 hours)....
1
vote
1answer
75 views

Toroidal metric in a random geometric graph

I have some code I've been using to generate some random geometric graphs as fast as possible. The boundary conditions on the rectangular domain I am using sometimes interferes with a clean ...
3
votes
2answers
247 views

Efficient implementation of Non-Maximum Suppression

I would like to know is there is a way to do this: ...