Skip to content

Instantly share code, notes, and snippets.

View tuelwer's full-sized avatar
🚀

Tobias Uelwer tuelwer

🚀
View GitHub Profile
@tuelwer
tuelwer / solve-circulant.txt
Last active November 13, 2023 11:23
Solving a linear system with circulant matrix using FFT
import numpy as np
from scipy.linalg import circulant
def solve_circulant(A,b):
return np.fft.ifft(np.fft.fft(b)/np.fft.fft(A[:,0]))
n = 10000
a = np.random.randn(1,n)
A = circulant(a)
b = np.random.rand(n)
@tuelwer
tuelwer / minted.tex
Last active August 14, 2023 17:33
LaTeX minted Python example
\documentclass{article}
\usepackage{algorithm}
\usepackage{minted}
\begin{document}
\begin{algorithm}
\vspace{0.2em}
\begin{minted}[xleftmargin=1em, numbersep=6pt, linenos, breaklines]{python}
def fib(n):
"""This function calculates the n-th Fibonacci number."""
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('arg1')
parser.add_argument('--arg2', default=0.1, type=float)
args = parser.parse_args()
print(args.arg1)
print(args.arg2)
@tuelwer
tuelwer / check-triangle-inequality.py
Last active March 10, 2021 14:32
Short Python script that checks whether a matrix fulfills the triangle inequality
def check_triangle_inequality(D):
""" Returns true iff the matrix D fulfills
the triangle inequaltiy.
"""
n = len(D)
valid = True
for i in range(n):
for j in range(i, n):
for k in range(n):
if k == j or k == i:
@tuelwer
tuelwer / plot_bars.py
Created December 18, 2020 07:30 — forked from 99991/plot_bars.py
Plot bar chart with multiple groups of bars with matplotlib pyplot
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
# ugly hack to embed fonts
matplotlib.rc("pdf", fonttype=42)
edgecolor = "black"
bar_scale = 0.8
@tuelwer
tuelwer / pickle-example.py
Created July 28, 2020 15:19
Simple pickle example
import pickle
r = [1, 2, 3]
pickle.dump(r, open('r.file', mode='wb'))
r = pickle.load(open('r.file', mode='rb'))
@tuelwer
tuelwer / pytorch-lbfgs-example.py
Last active March 25, 2024 14:12
pytorch-L-BFGS-example
import torch
import torch.optim as optim
import matplotlib.pyplot as plt
# 2d Rosenbrock function
def f(x):
return (1 - x[0])**2 + 100 * (x[1] - x[0]**2)**2
@tuelwer
tuelwer / pytorch-gmm.py
Last active March 10, 2021 14:31
Gaussian mixture model in PyTorch
class GMM(torch.nn.Module):
def __init__(self, n, d=2, k=2):
super(GMM, self).__init__()
self.d = d
self.k = k
self.n = n
self.covs = torch.eye(self.d).view(-1, self.d, self.d).repeat(self.k,1,1)
self.mus = torch.zeros(n, k)
self.member = torch.zeros(n, k)