Skip to content

Instantly share code, notes, and snippets.

View nschloe's full-sized avatar
👨‍💻
doing math

Nico Schlömer nschloe

👨‍💻
doing math
  • Monday Tech
  • Berlin, Germany
  • X @nschloe
View GitHub Profile
@nschloe
nschloe / sphere-vol.py
Created May 9, 2023 18:55
Volumes of spheres of radius 1 in higher dimensions
from math import tau
import matplotlib.pyplot as plt
n_max = 20
sphere_vols = [2, tau]
for n in range(3, n_max + 1):
sphere_vols.append(sphere_vols[n-2] * tau / (n-2))
plt.plot(range(1, n_max + 1), sphere_vols, "o")
@nschloe
nschloe / write-comparison.py
Created January 27, 2023 21:42
Python array I/O comparison
from sys import version_info
import matplotlib.pyplot as plt
import perfplot
import pickle
import netCDF4
import numpy as np
import h5py
import tables
@nschloe
nschloe / read-comparison.py
Last active January 28, 2023 20:08
Python array I/O comparison
from sys import version_info
import matplotlib.pyplot as plt
import perfplot
import pickle
import netCDF4
import numpy as np
import h5py
import tables
@nschloe
nschloe / means.py
Last active November 24, 2022 17:56
plot generalized means
import numpy as np
import matplotlib.pyplot as plt
def agm(an, bn):
while np.any(abs(an - bn) > 1.0e-15):
an, bn = (an + bn) / 2, np.sqrt(an * bn)
return an
x = np.linspace(0, 4, 401)
@nschloe
nschloe / integer-write-comparison.py
Created March 30, 2022 18:11
Python/NumPy integer ASCII writes
import perfplot
import numpy as np
def setup(n):
return np.random.randint(0, 100, (n, 4))
def loop(data):
with open("f.txt", "w") as f:
@nschloe
nschloe / fj-cplot-demo.py
Created February 16, 2022 21:48
cplot demo
# https://gist.github.com/fredrik-johansson/4098b7aea7e0321ac50bb533a03515d0
import cplot
import matplotlib.pyplot as plt
import mpmath
import numpy as np
from mpmath import fp
def _wrap(fun):
@nschloe
nschloe / cplot-lambert-series.py
Last active February 12, 2022 21:38
cplot Lambert series
import cplot
import numpy as np
def lambert_series(z, n=100):
zn = z.copy()
s = np.zeros_like(z)
for _ in range(n):
s += zn / (1 - zn)
zn *= z
@nschloe
nschloe / plot-optimization-tests.py
Last active February 3, 2022 16:51
Plot optimization test functions
import matplotx
import matplotlib.pyplot as plt
import numpy as np
class Rastrigin:
def __init__(self):
self.roots = np.array([[0, 0]])
def f(self, x):
@nschloe
nschloe / ackley-3d.py
Created February 3, 2022 09:28
Plot Ackley function layers
import numpy as np
import meshzoo
import meshio
def ackley(x):
xx = np.einsum("i...,i...->...", x, x)
sumcos = np.sum(np.cos(2 * np.pi * x), axis=0)
return (
-20.0 * np.exp(-0.2 * np.sqrt(0.5 * xx))
@nschloe
nschloe / multi-dotproduct.py
Created January 25, 2022 15:28
perfplot: multi dotproduct comparison
import numpy as np
import perfplot
def setup(n):
x = np.random.rand(n, k)
y = np.random.rand(n, k)
xt = np.ascontiguousarray(x.T)
yt = np.ascontiguousarray(y.T)
return x, y, xt, yt