In [21]:
import numpy as np
from numpy import random as rnd, linalg as la
import scipy as sp
from scipy import linalg
from matplotlib import pyplot as plt
import seaborn as sns
%matplotlib inline
In [7]:
N = 1000
gauss_coeffs = np.random.randn(1000) * 1000
coeffs = np.random.randn(1000) * 1000
In [8]:
exps = [lambda: rnd.normal(loc=mu, scale=1.0) for mu in gauss_coeffs]
In [52]:
def sum_of_exps(gauss_coeffs, coeffs, exps, vector=False, batch_size=1000):
N = len(exps)
if not vector:
evaluate = np.zeros(N)
for i, exp in enumerate(exps):
evaluate[i] = exp()
first_part = coeffs @ evaluate
second_part = coeffs @ evaluate
return first_part + second_part
else:
evaluate = np.zeros_like(N, batch_size)
for i, exp in enumerate(exps):
evaluate[i, :] = exp()
first_part = coeffs @ evaluate
second_part = np.sum(coeffs[:, None] * evaluate, ax)
def n_sum_of_exps(gauss_coeffs, coeffs, n_call, batch_size=10000):
batch_size=10000
exp_funcs =
evaluate = np.zeros_like(N, batch_size)
for i, exp in enumerate(exps):
evaluate[i, :] = exp()
first_part = coeffs @ evaluate
In [53]:
%timeit sum_of_exps(gauss_coeffs, coeffs, exps)
In [54]:
values = np.array([sum_of_exps(gauss_coeffs, coeffs, exps) for _ in range(10000)])
In [55]:
values = np.array(values)
In [56]:
values.shape
Out[56]:
In [57]:
plt.figure(figsize=(15, 10))
sns.distplot(values, bins=len(values)//1)
Out[57]:
In [48]:
a = rnd.randn(50)
In [49]:
np.sum(np.outer(a, a))
Out[49]:
In [50]:
np.sum(a)**2
Out[50]:
In [51]:
np.sum(np.kron(a, a))
Out[51]:
In [10]:
n_mat = 50
res = [np.zeros((n_mat, n_mat)) for n in range(10, 110, 10)]
for n, r in zip(range(10, 110, 10), res):
idd = np.eye(n)
mats = [la.qr(np.random.randn(n, n) + 1j * np.random.randn(n, n))[0] for _ in range(n_mat)]
for i in range(n_mat):
for j in range(n_mat):
temp = mats[i].dot(mats[j])
r[i, j] = la.norm(np.conj(temp).T.dot(temp) - idd)
In [11]:
print([np.mean(r) for r in res])
In [30]:
n = 100
x = 1000 *np.random.randn(n, n) + 1000j * np.random.randn(n, n)
xhskew = 0.5 * (x - np.conj(x).T)
In [31]:
u = sp.linalg.expm(xhskew)
In [32]:
np.conj(u).T @ u
Out[32]:
In [37]:
v = la.qr(rnd.randn(n, n) + 1j * rnd.randn(n, n))[0]
In [38]:
w = u @ v
In [39]:
w.shape
Out[39]:
In [40]:
np.conj(w).T @ w
Out[40]:
In [ ]: