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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / matvec-vs-manual.py
Last active August 17, 2021 16:51
3x3-matvec vs manual matvec
import perfplot
import numpy as np
import npx
A = np.array([[0.0, 1.0, 0.0], [125 / 29, -125 / 29, 0.0], [0.0, 50 / 29, -50 / 29]])
def manual(data):
fx, fy, fz = data
@nschloe
nschloe / x-div-alpha.py
Created August 9, 2021 08:23
numpy array scalar division
import numpy as np
import perfplot
def mult(x):
return x * (1.0 / np.pi)
def div(x):
return x / np.pi