Skip to content

Instantly share code, notes, and snippets.

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

Nico Schlömer nschloe

👨‍💻
doing math
View GitHub Profile
@nschloe
nschloe / fresnel-aux.py
Last active October 27, 2021 16:51
verifying the asymptotic expansion of Fresnel auxiliary functions
from numpy import pi, cos, sin
def f(z, s_z, c_z):
return (0.5 - s_z) * cos(pi / 2 * z ** 2) - (0.5 - c_z) * sin(pi / 2 * z ** 2)
def f_asymp(z):
zp2 = (pi * z ** 2) ** 2
r = 1.0
@nschloe
nschloe / divide.py
Last active November 5, 2021 08:56
/-where() vs. divide()
import perfplot
import numpy as np
threshold = 0.5
# threshold = 1.0e-3
def div_where(data):
a, b = data
return a / np.where(b > threshold, b, 1.0)
@nschloe
nschloe / string-vs-list-lookup.py
Created November 13, 2021 12:05
Python string lookup vs list lookup speed
import random
import perfplot
lst = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"]
string = "abcdefghijkl"
def list_lookup(data):
return [lst[n] for n in data]
@nschloe
nschloe / golden-ratio-vs-fibonacci.py
Created November 14, 2021 08:32
Golden ratio vs. Fibonacci
from math import sqrt
fibonacci = [1, 1]
phi = (sqrt(5) - 1) / 2
print(phi)
print()
errors = []
@nschloe
nschloe / inv-vs-double-solve.py
Created December 22, 2021 17:49
np.linalg.solve vs np.linalg.solve (twice)
import perfplot
import numpy as np
def inv(AX):
A, X = AX
Ainv = np.linalg.inv(A)
return Ainv @ X @ Ainv.T
@nschloe
nschloe / gram-schmidt.py
Last active January 13, 2022 20:43
Gram-Schmidt row-wise vs column-wise
import numpy as np
import perfplot
rng = np.random.default_rng(1)
def setup(n):
U = rng.random((n, 10))
return U, np.ascontiguousarray(U.T)
@nschloe
nschloe / householder.py
Created January 16, 2022 22:32
Householder comparison
import perfplot
import numpy as np
def householder(A):
m, n = A.shape
R = A.copy()
Q = np.identity(m)
for j in range(n):
@nschloe
nschloe / array-vs-columnstack.py
Created January 17, 2022 21:02
np.array() vs np.column_stack
import perfplot
import numpy as np
rng = np.random.default_rng(0)
def setup(n):
return [rng.random(n) for _ in range(10)]
@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
@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))