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
import numpy as np
import perfplot
def dot(a, b):
a_shape = a.shape
b_shape = b.shape
b = b.reshape(b.shape[0], -1)
return np.dot(a, b).reshape(a_shape[:-1] + b_shape[1:])
@nschloe
nschloe / hyp.py
Last active February 16, 2021 12:01
stackoverflow hyperplane distance
import numpy as np
import scipy.optimize
np.random.seed(0)
V_orig = np.random.rand(50, 3)
def func(x):
Vx = V_orig @ x
return np.dot(Vx, Vx)
@nschloe
nschloe / dot-vs-einsum.py
Created April 30, 2021 17:43
dot vs einsum for 1D
import numpy as np
import perfplot
def dot(xy):
x, y = xy
return np.dot(x, y)
def einsum(xy):
@nschloe
nschloe / atc.py
Created May 3, 2021 12:21
A.T.conj(): caching vs inline
import perfplot
import numpy as np
from scipy.sparse import spdiags
def setup_dense(n):
A = np.random.rand(n, n) + 1j * np.random.rand(n, n)
AH = A.T.conj()
x = np.random.rand(n) + 1j * np.random.rand(n)
return A, AH, x
@nschloe
nschloe / monte-carlo.py
Created May 13, 2021 07:53
Monte Carlo test
import math
import numpy as np
import perfplot
import random
def plain_loop(n):
inside = 0
random.seed()
for _ in range(n):
@nschloe
nschloe / div-vs-mul.py
Created May 13, 2021 11:21
numpy division vs multiplication
import perfplot
import numpy as np
def div(ab):
a, b = ab
return a / b
def mul(ab):
@nschloe
nschloe / daxpy.py
Last active May 14, 2021 14:03
Python x+a*y vs daxpy
import numpy as np
import perfplot
from scipy.linalg.blas import daxpy
a = 1.3
def np_axpy(data):
x, y = data
return a * x + y
@nschloe
nschloe / ddot.py
Created May 14, 2021 14:05
np.dot vs blas.ddot
import numpy as np
import perfplot
from scipy.linalg.blas import ddot
def np_dot(data):
x, y = data
return np.dot(x, y)
@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
@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