23 votes
Accepted

Fast Hankel Function in Mathematica

This is LibraryLink code with compiler and linker options specifically for macos on Apple Silicon. These are the prerequisites to compile and run this successfully: XCode installed along with the ...
Henrik Schumacher's user avatar
21 votes

Gravitation simulation and interaction of N (1000) massive objects

And I was somewhat lying in wait for this kind of question... In I have been working on Barnes-Hut-like code for the so-called tangent-point energies in the last couple of years. This is a family of ...
Henrik Schumacher's user avatar
17 votes

Speeding up Gaussian sampling

Watching Activity Monitor, I don't think RandomVariate is multithreaded. Luckily random samples from a normal distribution can be compiled. We can exploit ...
Greg Hurst's user avatar
  • 35.8k
16 votes
Accepted

How to speed up Outer[Subtract, vect1,vect2]?

x = RandomReal[1., 10000]; y = RandomReal[1., 10000]; a = Outer[Subtract, x, y]; // RepeatedTiming b = Outer[Plus, x, -y]; // RepeatedTiming a == b {13.667, Null}...
Karl's user avatar
  • 901
14 votes
Accepted

Speeding up Gaussian sampling

We can achieve noticeable speedup with Intel's MKL random number generator, which leverages hardware accelerated code. m = 1000000; d = 100; Default behavior: <...
Greg Hurst's user avatar
  • 35.8k
14 votes

How can I get samples of f(x) that are roughly evenly spaced?

f[x_] := 1 - 8 x^2 + 8 x^4; You can use the option MeshFunctions -> {ArcLength} and ... 1. Specify the number of equal-arc-...
kglr's user avatar
  • 392k
13 votes

How can I get samples of f(x) that are roughly evenly spaced?

If we want to the ArcLength approximately equal to the dist = 0.15;, we can define an arclength function ...
cvgmt's user avatar
  • 69.4k
12 votes

Speeding up Gaussian sampling

This is my LibraryLink take on this. I use one of the random number generators of the standard library in conjunction with OpenMP. This will only work on Apple Silicon with OpenMP installed via ...
Henrik Schumacher's user avatar
12 votes
Accepted

How can I make the following table quickly?

Tabb has the structure: {{a[1, 1], a[1, 2]}, {a[2, 1], a[2, 2]}, {a[3, 1], a[3, 2]}} Now you want to introduce 1 in sublist 1. This can be done with "Riffle&...
Daniel Huber's user avatar
11 votes

How to quickly make a rectangular array of continous 1 and 0 given the start position of 1

Obligatory bare-metal C answer for ultimate speed: ...
Roman's user avatar
  • 46.9k
10 votes
Accepted

Why does Mathematica take so long to produce this sound?

Creating the audio signal as a vector first allows us to use more efficient ways to sample the function: ...
Henrik Schumacher's user avatar
10 votes

How to quickly make a rectangular array of continous 1 and 0 given the start position of 1

This is pretty fast: ...
flinty's user avatar
  • 24.7k
10 votes

How to quickly make a rectangular array of continous 1 and 0 given the start position of 1

Instead of constructing every column separately, note that each column is contained in a vector of n 0's and n 1's, we only need to pick the corresponding part. For ease, we create the matrix row wise ...
Daniel Huber's user avatar
9 votes

Efficient eigendecomposition of DPR1 matrices

The most important facts about DPR1 matrices of size $n \times n$ are: Their matrix-vector multiplication can be computed in $O(n)$ time instead of $O(n^2)$ as for ordinary dense matrices. By virtue ...
Henrik Schumacher's user avatar
9 votes

Why does Mathematica take so long to produce this sound?

The way you have created the function you are forcing MMA to keep evaluating your expression, which although lightweight on its own, is rather heavy when it needs to be repeated for different values ...
alex's user avatar
  • 1,396
9 votes

How can I make the following table quickly?

Tabb = Array[a, {3, 2}]; John Taylor (original) ...
Bob Hanlon's user avatar
  • 155k
9 votes

How to quickly make a rectangular array of continous 1 and 0 given the start position of 1

...
Roman's user avatar
  • 46.9k
9 votes

How can I make Manipulate faster?

You can use a single Plot function and add the required annotations using the options MeshFunctions, ...
kglr's user avatar
  • 392k
9 votes
Accepted

Better code for Ramsey partitions

Update 3: We get further speed-up by (1) working with the left parts of partitions with elements $\geq 2$ and appending the results with appropriate number of 1s in ...
kglr's user avatar
  • 392k
9 votes
Accepted

Why Does Part Work Much Slower on Modified List?

what is the reason for slowdown for the second example? It got unpacked, that is why tabb=RandomReal[{0.0000001,10^5},{10^6,80}]; Developer`PackedArrayQ[tabb] <...
Nasser's user avatar
  • 142k
8 votes
Accepted

Efficient eigendecomposition of DPR1 matrices

Okay, apparently, the root finding for the characteristic polynomial is quite easy. Since the derivative of the characteristic polynomial is easy to compute, too, I try to use Newton's method where ...
Henrik Schumacher's user avatar
8 votes

Values of counting functions

You can use SparseArray and Accumulate to assemble a vector cvec so that ...
Henrik Schumacher's user avatar
8 votes

How to produce the following table faster?

tl;dr: As always with my post, find the most recent implementation at the bottom of the post. InterpolationFunctions are indeed much less efficient than they could/...
Henrik Schumacher's user avatar
8 votes

How to quickly make a rectangular array of continous 1 and 0 given the start position of 1

...
Syed's user avatar
  • 48.6k
8 votes
Accepted

How to quickly make a rectangular array of continous 1 and 0 given the start position of 1

Plain C-style loop with Compile: ...
Henrik Schumacher's user avatar
8 votes

How to use Compile to optimize the performance of a function calculating the distance between two points?

...
Karl's user avatar
  • 901
7 votes

Fast pairwise difference

As Henrik has demonstrated in his excellent answer, the good old C compiler interface is still super fast -- the fastest if considering the compiling time. But the new compiler (...
Silvia's user avatar
  • 27.5k
7 votes
Accepted

How to keep lines on boundary or inside a region?

...
kirma's user avatar
  • 19k
7 votes
Accepted

Why the ugly code is faster than the pretty code?

The issue seems to be that you use coordx1 = coordx[Tab1[[i]][[indexE]], M, Tab2[[j]][[indexE1]]]; instead of ...
Henrik Schumacher's user avatar
7 votes

How can I make the following table quickly?

Tabb1 == Join @@ Outer[{#1[[1]], #2, #1[[2]]} &, Tabb, Range[3], 1] (* True *)
Roman's user avatar
  • 46.9k

Only top scored, non community-wiki answers of a minimum length are eligible