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
8
votes
2answers
146 views

Making a faster alternative for {PatternSequence[1, PatternSequence[2, 3 ..] ..] ..}

I need to improve a pattern or switch an approach. It is best described by an example For a hierarchy/order given by a list e.g.: order = {1, 2, 3} and a list: <...
0
votes
0answers
13 views

How to memoize with patterns? [duplicate]

Here is an artificial example to explain what I am up to. Define ClearAll[f] f[x_, y_] := f[x, y] = If[x == 0, g[y], g[f[x - 1, y]]] Then ...
1
vote
1answer
31 views

Optimizing a series expansion for high order in $x$

I would like to expand the following function at $x \sim 0$ up to some high $x_\text{max} = \Delta_\text{max}$: $$16 \sum_{\Delta=1}^{\Delta_\text{max}} \sum_{s=0}^{\Delta-2} f_{\Delta,s} \frac{(s+\...
3
votes
3answers
74 views

Mean values of skew diagonals of a $(n+1,n)$ matrix

How mean values of skew diagonals of a $(n+1,n)$ matrix can be computed efficiently? Here is my naive implementation: ...
2
votes
0answers
30 views

Ideas to speed up an order statistic calculation?

A toy example: {n, top} = {25, 100}; mxs = RandomReal[{0, top}, n]; dist = UniformDistribution[Thread[{0, mxs}]]; result = Mean[OrderDistribution[dist, n]] With ...
0
votes
1answer
46 views

Solution for system of linear equations for unknown variables in Mathematica and Python

I have a system of linear equations (here 9) as below: $ 2 a_{\{1,2,1,2,2,1,2,1,2,2\}}+2 a_{\{1,1\}}=1, \\ \left(a_{\{1,2,1,2,2,1,2,1,2,2,2\}}+a_{\{1,2,1,2,2,1,2,2,1,2,2\}}\right)+2 a_{\{1,1,2\}}=1, \\...
2
votes
0answers
55 views

Optimizing a graph-theoretic piece of code

I have written the following piece of code: ...
3
votes
0answers
42 views

Why is doing a computation inside Select slower than a precomputation?

I am trying to generate primitive Pythogorean triples using Euclid's parametrization. I basically want a list of pairs with ordered pairs $(m,n)$ such that $m$ and $n$ are coprime. My first approach ...
2
votes
1answer
79 views

Solving 4 coupled linear 1st order ODE with constant coefficients

I'm getting trouble solving these 4 coupled differential equations, just can't get a result while it keep running. ...
5
votes
1answer
86 views

Why is NextPermutation slow?

<< Combinatorica` Permutations[Range[0, 9]][[1000000]] // AbsoluteTiming Nest[NextPermutation, Range[0, 9], 1*^6 - 1] // AbsoluteTiming I want to save memory,...
0
votes
1answer
80 views

Colors in Manipulate[] plot change during execution

UPDATE WITH WORKING EXAMPLE Following what @Kuba said, I've attempted to find a MWE that shows the problem without too much of the other stuff, and here's what I've done. Here's a couple of new GIFs ...
1
vote
1answer
65 views

Speeding up Gaussian expectations

I found that Mathematica's Expectation operator is slow for Gaussian random variables, I'd like to scale to 100 dimensions, but it currently takes 10 seconds for 10 dimensions, looking forward to tips ...
3
votes
1answer
170 views

Parallelizing large sum to use all cpu cores

I'm looking to compute the the following expectation, with $x$ a column vector sampled from $d$-dimensional distribution. $$E[(I-\alpha xx')\text{cov}(I-\alpha xx')]$$ Simple example is below. ...
1
vote
0answers
39 views

Precompute Functions for use in NDSolve

I have a very complicated pair of functions $F(x,y), G(x,y)$ that are used in a differential equation $0 = x'' + F(x,y)$, $0 =y '' + G(x,y)\,. $ $F$ and $G$ are roughly sums of products of the Bessel ...
5
votes
4answers
293 views

Speed up the intersection of the diagonals of a regular polygon

I need to calculate all the intersection points of the diagonals of a regular polygon, following code is really slow, when n=15, it take about 30sec. I also tried using ...
4
votes
1answer
59 views

Multi-dimensional array lookup vs flat array lookup with compute

During Monte-Carlo (MC) simulations on a latice of dimensions $(L_1,L_2,\ldots,L_D)$, all the site varibales are often stored as a $D$ dimensional array array. To ...
0
votes
0answers
28 views

How apply a function to multiple nested elements without a loop

I'm trying to speed up solving the intersection of groups of lines with a surface. At the moment I have a function that loops through each line in a group (i), then goes to the next group (j). The ...
1
vote
1answer
63 views

Help Calculating My “Deviation”

I created a "distribution deviation" where for $\left\{a_1,...,a_k\right\}$ we take all take the mean of all combinations of $\frac{\min\left\{a_{i},a_{j}\right\}}{\max\left\{a_{i},a_j\right\...
5
votes
1answer
151 views

Can this probability function speed be improved?

I have a process that as part of it requires a certain probability query, specifically, given a multinomial distribution with f equiprobable categories and r trials, some specific category has a given ...
0
votes
0answers
51 views

Generating and operating with random number

I'm working basically with the creation of random numbers that represent the positions of $N$ electrons within a box. Later, I sum all the individual electric fields generated by the electrons, thus ...
1
vote
2answers
132 views

Fast way to check if $f(x)>x$ for what value of $x$

In order to find the value $x$ for which $f(x)>x$, I tried it using the following code: ParallelTable[If[TrueQ[f[x]>x],x,Nothing],{x,0,10^9}] For some ...
4
votes
0answers
41 views

What exactly does MemoryInUse measure?

I have a long-running process, which takes about half a day to complete. The main "loop" is a Fold of (roughly) the following structure: ...
0
votes
1answer
98 views

Symbolic Matrix Exponential of Large (500x500) Matrix

I am trying to build a sort of time propagator matrix. I have a matrix $H$ that is 500x500 and i am trying to evaluate $\exp(-H t)$. I need to find this in a symbolic way because i will need to do $\...
0
votes
1answer
100 views

Optimization of code

I have written a following mathematica code which involve solution to nonlinear equations. It produce results according to my expectation but it take more than a day in execution which is very ...
0
votes
1answer
58 views

Speeding up this NIntegrate

I'm trying out the code below and it takes quite a long time to compute and throws eincr and slwcon warnings. Is there a way to speed this up? ...
0
votes
0answers
47 views

Alternative to procedural loop with evaluation of notebooks

very new Mathematica user here. I am struggeling with programming Mathematical-like and would appreciate some guidance. I set up a main notebook which calls various sub-notebooks: ...
1
vote
0answers
47 views

LTemplate and external library Armadillo

As a great fan of the simplicity of the LTemplate library for the end user I am now faced with a problem when apparently everything compiles correctly but the functions are not loaded from the library....
1
vote
1answer
45 views

Round-off error

Assuming $0<q<1$. I built these two functions A[k_,q_]:=Sum[PDF[BinomialDistribution[i, q], k]*PDF[ZipfDistribution[n], i], {i, k, Infinity}] and ...
8
votes
1answer
182 views

Why does it take so long to Quit?

Running Quit is a core task repeated many times in any debugging/development cycle. Its efficiency is therefore important and yet in my set-up it is taking ...
2
votes
1answer
64 views

Efficiently check if a File object is a valid Image

I have a directory of images that might not have downloaded, and need to delete the duds. This is too slow: Import /* ImageQ /@ fileNames In bash, I use the ...
2
votes
2answers
55 views

Dynamically set several values in a list at once

I am attempting to create a threshold which can be manually adjusted when necessary, but otherwise is assumed to be constant. This corresponds to a list of values, wherein all later values are set ...
3
votes
0answers
61 views

Why is ContainsOnly so slow for checking a list is full of zeros?

I'm on Windows 10 Mathematica 12.1.1. I would expect allocating a big ConstantArray and comparing would be slower but apparently it is not: ...
2
votes
2answers
75 views

Computational efficient way to find a SL(2,Z) matrix between two set of vectors [closed]

Suppose I have two sets of vectors (let's say it 2d, but it would be nice to generalize it to arbitrary dimensions): $(v_1,v_2,\ldots,v_n)$ and $(v'_1,v'_2,\ldots,v'_n)$ and they have integer entries, ...
0
votes
3answers
61 views

Slow Integrate of a product of Boolean functions

I have ...
11
votes
1answer
237 views

How can I avoid performance losses from ResourceFunction?

It seems like there is some sort of evaluation overhead related to the ResourceFunction wrapper that I'd like to avoid. This is especially pronounced for functions ...
1
vote
0answers
40 views

Improve NIntegrate + fit

I need to improve the performance of my fit. Below you can find the data I am using ...
3
votes
1answer
83 views

Compiled Function is slower than non-compiled version?

I need to maximize a function numerically, thus the speed of this function is very important. Here is the most time consuming part of this function. ...
2
votes
1answer
55 views

FileNames becomes extremely slow when there are large number of files under a directory

I have a directory on my portable hard drive, with around 350000 files under it (there is no subdirectory, only files). I want to get all the file dirs so use ...
0
votes
1answer
101 views

How do we use mathematica to apply my average to this example? [closed]

I have defined a set function and an average. If you need help understanding my definitions, let me know. I will do my best to explain. Question How do I use Mathematica to apply my definition of ...
4
votes
1answer
132 views

Why Does Parallelization Not Speed Up these Seemingly Well-Parallelizable, Simple Functions?

Original Example Consider function f, a parallelized version fPar, and a coarsest-grained parallelized version ...
4
votes
1answer
95 views

Efficiently removing elements from list of permutations

I'm looking for an efficient way to remove entries from a list of permutations. I have a list of variables from which I calculate all possible permutations. I then want to remove those permutations ...
1
vote
0answers
40 views

CUDA and sparse arrays

Is there a way to use the CUDA functions (like CUDADot and so on) when we are dealing with SparseArrays? I found some answers to similar questions but they are extremely old (around five years ago) ...
5
votes
1answer
133 views

How can I reduce timing to solve this equation?

I am tring to solve equation $$\frac{2 \log (x y+2021)}{\log (2)}+4^{x+1}=2^{x y+2022}+\frac{2 \log (2 x+1)}{\log (2)},$$ where $x$, $y$ are integer numbers. I tried ...
1
vote
2answers
59 views

Optimize certian list over permutations, perhaps using recursions?

I am trying to improve my code for computing products of monomial symmetric functions. It boils down to the following. Let lam and ...
0
votes
1answer
33 views

Generalizing previous question: Partition $[a,b]$ into $m$ sub-intervals and count number of sub-interval intersecting $A_1\subseteq[a,b]$?

In my previous question, How to partition $[a,b]$ into $m$ equal sub-intervals and count the number of sub-intervals that intersect with a subset of $[a,b]$?, I was given an answer to my specific ...
-1
votes
1answer
35 views

Using coding to calculate my average for functions defined on countable sets?

Suppose we have a function $f:A\to\mathbb{R}$ where $A\subseteq[a,b]$, $a,b\in\mathbb{R}$ and $S\subseteq A$. Now suppose $f$ is $$f(x)=\begin{cases} x^2+1 & x\in S_1\\ x & x\in S_2\\ \end{...
8
votes
1answer
365 views

Why are more highly nested lists often not more memory and time-intensive?

I would expect more highly nested lists, being deeper and more complex than flatter lists, to take up more space (bytecount) and take more time and memory to operate on in general. Consider the ...
1
vote
1answer
77 views

How to partition $[a,b]$ into $m$ equal sub-intervals and count the number of sub-intervals that intersect with a subset of $[a,b]$?

Suppose we have a subset of $[a,b]$, where $a=0$ and $b=1$, such as $$A_1=\left\{\frac{1}{2^x}+\frac{1}{2^y}+\frac{1}{2^z}:x,y,z\in\mathbb{Z}\right\}\cap[0,1]$$ Which is a subset of $[0,1]$ If we ...
5
votes
1answer
131 views

Avoiding CopyTensor of large array in Compiled functions

After reading about how to avoid the CopyTensor in various cases I still do not understand the optimal answer for the Fast-Hadamard transform below (CopyTensor appears in line 4 of the code): ...

1
2 3 4 5
46