Questions tagged [performance-tuning]

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.

Filter by
Sorted by
Tagged with
0
votes
0answers
39 views

Force WhenEvent and NDSolve to numerical evaluation and Best Practice

I'm trying to perform an hybrid kalman filter for a nonlinear system. The following code works but it is very slow due to the fact that it does symbolic evaluation. What I wish is that for the filter ...
1
vote
1answer
95 views

NSolve V12.1.1 much slower than V6.0 for the same polynomial system

We are solving polynomial systems with real coefficients using NSolve. A typical system is attached below. The problem we have is that Mathematica V12.1.1 solves it 17 times slower than V6.0, see the ...
0
votes
0answers
46 views

Area of ImplicitRegion is slow or doesn't compute under certain circumstances

I'm trying to find the area of a region defined by ImplicitRegion using the Area function. I have the following: ...
1
vote
0answers
72 views

Symbolic add of 32Bit Registers

I would like to perform multiple 32bit word register additions (mod 32 add. with variables of Boolean type shuffled with symbolic type). For this task I implemented a ripple adder function: ...
16
votes
2answers
1k views

How do the functions Reap and Sow actually work behind the scenes?

I have read that using Reap and Sow to generate a list of unknown length is much more efficient than using AppendTo over and over. I believe AppendTo works by creating a new list that is one element ...
1
vote
0answers
22 views

Fastest way to do repeated subsample/Intersection calls over large set of vectors

In this answer I found I needed to calculate a bunch of intersections of subsets of vectors, like ...
0
votes
0answers
41 views

sparse matrix multiplication

Say I have a sparse matrix $M$ with $\sim 50.000 .000$ non zero elements. What is the fastest way to make operations such as : $$M^k X$$ where $X$ is a given vector. Is the operation $MX$ ...
3
votes
2answers
80 views

Avoiding duplicates after integer insertions

This is related to Splitting balls over sized bins but a simplified subproblem I've got a list of lists of numbers, e.g. ...
2
votes
1answer
85 views

Splitting balls over sized bins

This is strongly related to Splitting a set of integers over a set of bins, but a much simpler case. If we have $N$ indistinguishable balls and $k$ arbitrarily large distinguishable bins, we know the ...
1
vote
2answers
54 views

WhenEvent inside Ndsolve how to evaluate actions numerically every time the event happens

I need to force a whenevent action to be evaluated numerically all the times the event happens. The fact is that I use very complicated matrices and I need to invert them so the system requires a lot ...
1
vote
1answer
48 views

How to flatten the given table?

Consider a table Table1 = Flatten[Table[{x, y, {f1[x,y],f2[x,y],f3[x,y]}}, {x, 1, 2, 0.001}, {y, 2, 3, 0.001}],{1,2}]; Could you please tell me whether it is ...
1
vote
0answers
37 views

How to speedup RegionIntersection?

Consider a circle circle located at the origin of a coordinate frame and with a radius ROffAxisArb (it is parametrized by two ...
3
votes
1answer
51 views

Significant slowdown in a generalised implementation

Consider the following code: ...
0
votes
0answers
43 views

How to resolve these errors that pop up when using 'Compile' for a large function? [duplicate]

The problem I have been wanting to make my code which solves an optimisation problem faster. This led to this question on here and I realised from there that the root of the problem is the very slow <...
1
vote
1answer
49 views

Using With[] vs. Using a Pure Function for Constants

There are many cases where I am conflicted over using With[] versus using pure functions when dealing with constants, knowing the output is the same either way. For ...
3
votes
4answers
168 views

Code to solve quadratically constrained quadratic optimisation problem using FindMinimum is very slow. How do I make it faster?

The problem I have a quadratically constrained quadratic optimisation problem (QCQP) which takes about 13 seconds to find the optimal solution. I want to iteratively optimise a large number of times (...
0
votes
0answers
66 views

How to make the following table compiled?

This is the continuation of the two previous questions 1, 2. The task In short, I computed the Lorentz boost of some 3-vector $\mathbf p = \mathbf{p}(E,\theta,\phi) \to \mathbf{p}'$ having the 3-...
5
votes
2answers
156 views

BreadthFirstScan for implicit Graph

Many algorithms on graphs require only local knowledge at each step, e.g. for BFS at any point we only need to know the AdjacencyList of the vertex we are currently ...
5
votes
3answers
237 views

How can I reorder levels of a list in a performant way?

I have the following problem and I am not sure how to speed it up: I construct a list element by element like so: a[[i]]=Table[some_function[j,k],{j,1,3},{k,1,6}] ...
1
vote
1answer
61 views

Why the computation time is shorter in this case?

The task I would like to compute the Lorentz transformation $\mathbf{p}'$ of an arbitrary 3-momentum vector $\mathbf{p}$ given a boost characterized by some other momentum vector $\mathbf{p}_{N}$. The ...
1
vote
2answers
114 views

How to make the following code work faster?

The problem I would like to compute the Lorentz transformation $\mathbf{p}'$ of an arbitrary 3-momentum vector $\mathbf{p}$ given a boost characterized by some other momentum vector $\mathbf{p}_{N}$, ...
0
votes
0answers
39 views

Import and processing of large formulas

I have a text file which is several GB in size and contains one large formula. I have created this file in another program which I use for analytical transformations. Now I need to insert numbers into ...
0
votes
1answer
58 views

Series expansion with criteria on the coefficients

I will first do an illustrative example. Suppose I have the following function: $ f(\vec{x},\vec{t})=\frac{x_1x_2}{(1-x_1 x_2^{-1} t_1)(1-x_2x_1^{-1} t_2)}$ I want to expand it with respect to $(t_1,...
0
votes
1answer
122 views
4
votes
2answers
134 views

Large matrix inversion

I'm still learning Mathematica, and I'm trying to write a program that needs to invert a large (nxn, n=160 or possibly larger than this) matrix with numerical entries, multiply with to another matrix, ...
3
votes
2answers
70 views

Generating representatives of rotation classes of tuples of ones and zeros with a fixed number of ones

In a related question I asked how to generate all the tuples of ones and zeroes with a fixed number of ones (generating tuples of ones and zeroes with a fixed number of ones). I wish to consider a ...
5
votes
1answer
114 views

Why does NIntegrate become much faster if I evaluate a function, then compile it, then make it numerical?

I am using Mathematica for numerical integrals in optics. By accident, I found out that NIntegrate becomes faster by an order of magnitude for a function I'm integrating, if I take the following steps:...
5
votes
3answers
364 views

Fastest way : two by two list inversion

Consider a list of even length, for example list={1,2,3,4,5,6,7,8} what is the fastest way to accomplish both these operations ? Operation 1: two by two element ...
9
votes
6answers
370 views

generating tuples of ones and zeroes with a fixed number of ones

I would like to generate all the tuples of ones and zeros of a given length and with a given number of ones without generating all the possible tuples, which is impossible for tuples of large enough ...
1
vote
1answer
33 views

Fastest way to convert two associations into a sparse matrix

I have two association $N$ and $C$ $N$ has this form : $N=<|x_1\rightarrow \{x_{1,1},...x_{1,p_1}\},x_2\rightarrow \{x_{2,1},...x_{2,p_2}\}...,x_D\rightarrow \{x_{D1},...x_{Dp_D}\} |>$ $C$ has ...
2
votes
2answers
152 views

How to find the intervals in which a function is positive?

I have to find the positive range for the derivatives of a lot of functions. I have a very inefficient solution. At the moment I am using a For loop to find the ...
0
votes
1answer
44 views

Speeding up Do loop with lists inside

The question is simple. I'm trying to improve the (time) performance of a "Do" loop that looks like the following MWE ...
0
votes
0answers
31 views

While a program is running, can you free some RAM by transferring to disk w/o using Export?

While my program is running, 100's of images are being calculated. I noticed that physical memory (PM) fills up at about 500 images, and then I see the resource monitor drop the PM back to about 50% ...
1
vote
1answer
87 views

Best structure to store a graph?

I have a weighted, undirected graph with weighted edges, that could be represented in mathematica by the following code for $N=3$ ...
0
votes
0answers
17 views

Optimize the code to run faster (with MatrixPower and Partition) [duplicate]

I'm looking for a way to optimize this code to get the exact same result, but at least 5 times faster. What could I try? ...
9
votes
2answers
232 views

Efficient method to find a pair that sum to a given value in a list in matematica style

Given an array nums and an number target, find all pairs in array whose sum is equal to ...
2
votes
0answers
33 views

Why is RotationTransform 100x slower than manually specifying the transformation?

I'm plotting an implicit surface using RegionPlot3D. I have two descriptions of the surface which I believe are (or should be) conceptually and functionally ...
14
votes
1answer
891 views

Metropolis-Hastings Algorithm - Significantly slower than Python

I would like to code a Metropolis Hasting algorithm and thought I would get the general scheme of things going with a toy example. I think I got it running but my Mathematica code runs significantly ...
7
votes
2answers
104 views

Compiling Eratosthenes Sieve

My best implementation of Eratosthenes sieve is here. ...
2
votes
0answers
48 views

Handling lists indexed with integer partitions

I am trying to do some exploration around the multi-dimensional version of [truncated moment problem], where I work with various distributions (uni- or multi-variate), and use some truncated moment ...
7
votes
1answer
199 views

Is there a way to speed up Integrate when the integrand contains a product of polynomials each of which having a large degree?

I have integrals of the form $$\int_0^\infty\mathrm d x\; e^{-x^2}\;\;(\textrm{Polynomial of degree n})\times(\textrm{Polynomial of degree m}),$$ where $n,m$ can be as large as 300. When I use ...
5
votes
3answers
224 views

Speeding up FactorInteger for product of two primes

I have a large integer $N$ of size about 10^150. I know that $N$ is a product of two primes $p$ and $q$. I also know that both $p$ and $q$ are of roughly equal size, so one of them is not, for example,...
5
votes
2answers
62 views

Pair-wise equality over large sets of large vectors

I've got an interesting performance tuning/algorithmic problem that I'm running into in an optimization context. I've got a set of ~16-50 lists of integers (usually in ...
3
votes
3answers
68 views

all rooted subgraphs of size $k$ in the grid graph

I would like to compute all rooted subgraphs of size $k$ in the grid graph. I use the following approach, I start from the root, then traverse the graph along edges in all possible ways until I have ...
3
votes
0answers
77 views

MMA 12.2 freezes while closing notebooks with many plots

I recently upgraded to 12.2 and revisited some notebooks made last year (presumably in 12.0 or 12.1). It quite often happens for big notebooks (typically larger than or around 200MB) that MMA 12.2 ...
7
votes
1answer
144 views

Fast Evaluation of a series of dot product

Context I have a function that depends on 3 real variables x,y and z and that is defined by a series of matrix products. The evaluation of f for a specific (x,y,z) is fast ~0.02 sec but I want to ...
3
votes
1answer
83 views

Find all non-isomorphic trees with $n$ nodes

I would like to compute all non-isomorphic trees with n nodes efficiently. I use the following approach: I create all possible trees ( Visualizing Cayley's Formula in Mathematica) and filter the ...
3
votes
1answer
51 views

Copy DateListPlot ticks in ListLinePlot (for large data)

It is not very surprising that plotting a PackedArray is faster to plot than a list of pairs (date, value), nonetheless the plotting time may be prohibitive with <...
1
vote
1answer
26 views

Is there a better way : solutions to integer equation

I have the following code which solves for Integers the equation $$a_1+a_2+a_3+a_4+b_1+b_2+b_3+b_4+c_1+c_2+c_3+c_4=tot$$ with $$ -4<a_i\in \mathbb{Z}<4$$ $$ -7<b_i\in \mathbb{Z}<7$$ $$ -4&...
1
vote
0answers
31 views

Problem with the return type of Compile [closed]

Can I return anything other than a real number with a compiled function ? In the example below, I use Compile on a toy function to illustrate my problem, the real ...

1
2 3 4 5
49