In [3]:
import numpy as np
import mpl_toolkits.mplot3d as mpl3
import matplotlib.pyplot as pl
%matplotlib notebook
In [13]:
x = np.array([1, 2, 3], dtype=float)
x /= np.sum(x)
In [15]:
np.sort?
In [16]:
a = np.random.randn(10)
In [17]:
o = np.argsort(a)
In [21]:
def invert_sort(order):
result = np.empty(len(order), dtype=order.dtype)
for i, j in enumerate(order):
result[j] = i
return result
In [32]:
import pdb
In [35]:
def proj(x):
order = np.argsort(x)[::-1]
x_sorted = x[order]
a = 0
i = len(x)
while x_sorted[i - 1] + a / i < 0:
a += x_sorted[i - 1]
i -= 1
result = np.zeros(len(x), dtype=float)
result[:i] = x_sorted[:i] + a / i
return result[invert_sort(order)]
In [42]:
psimplex = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
fig = pl.figure(0, figsize=(10, 10))
ax = mpl3.Axes3D(fig)
smplx = mpl3.art3d.Poly3DCollection([psimplex])
ax.add_collection3d(smplx)
for x in np.random.randn(10, 3):
x /= sum(x)
y = proj(x)
print(x, y)
pl.plot((x[0], y[0]), (x[1], y[1]), (x[2], y[2]), color='r')
In [ ]: