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 / 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 / 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 / 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 / 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 / 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 / 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 / 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)
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:])
import numpy
import perfplot
def setup(n):
pts = numpy.random.rand(3, n, 2)
e = numpy.array([
pts[2] - pts[1],
pts[0] - pts[2],
pts[1] - pts[0],
@nschloe
nschloe / compare.py
Last active October 26, 2020 12:57
einsum is faster if the tail survives
import perfplot
import numpy
def setup(n):
a = numpy.random.rand(n, 3)
b = numpy.ascontiguousarray(a.T)
return a, b