def simplex_projection(v, b=1):
 v = np.asarray(v)
 p = len(v)
 v = ((v > 0) * v)
 u = np.sort(v)[::(-1)]
 sv = np.cumsum(u)
 rho = np.where((u > ((sv - b) / np.arange(1, (p + 1)))))[0][(-1)]
 theta = np.max([0, ((sv[rho] - b) / (rho + 1))])
 w = (v - theta)
 w[(w < 0)] = 0
 return w

def simplex_projection ( v , b = 1 ) : v = np . asarray ( v ) p = len ( v ) v = ( ( v > 0 ) * v ) u = np . sort ( v ) [ : : ( - 1 ) ] sv = np . cumsum ( u ) rho = np . where ( ( u > ( ( sv - b ) / np . arange ( 1 , ( p + 1 ) ) ) ) ) [ 0 ] [ ( - 1 ) ] theta = np . max ( [ 0 , ( ( sv [ rho ] - b ) / ( rho + 1 ) ) ] ) w = ( v - theta ) w [ ( w < 0 ) ] = 0 return w
projection vectors to the simplex domain implemented according to the paper: efficient projections onto the l1-ball for learning in high dimensions .