def dithering(x, k, state, partition='linear', norm="max"): y = x.flatten() if norm == "max": scale = np.max(np.abs(y)) elif norm == "l2": scale = np.linalg.norm(y.astype(np.float64), ord=2) else: raise ValueError("Unsupported normalization") y /= scale sign = np.sign(y) y = np.abs(y) # stocastic rounding if partition == 'linear': y *= k low = np.floor(y) p = y - low # whether to ceil y = low + bernoulli(p, state) y /= k elif partition == "natural": y *= 2**(k - 1) low = round_next_pow2((np.ceil(y).astype(np.uint32))) >> 1 length = copy.deepcopy(low) length[length == 0] = 1 p = (y - low) / length y = low + length * bernoulli(p, state) y = y.astype(np.float32) y /= 2**(k - 1) else: raise ValueError("Unsupported partition") y *= sign y *= scale return y.reshape(x.shape)
def bipartite_with_probability(V1, V2, p): ''' Returns a random simple bipartite graph on V1 and V2 vertices, containing each possible edge with probability p. @param V1 the number of vertices in one partition @param V2 the number of vertices in the other partition @param p the probability that the graph contains an edge with one endpoint in either side @return a random simple bipartite graph on V1 and V2 vertices, containing each possible edge with probability p @raises ValueError if proba bility is not between 0 and 1 ''' if p < 0.0 or p > 1.0: raise ValueError('Probability must be between 0 and 1') # Modification question #5 if V1 < 0 or V2 < 0: raise ValueError("Vertex value cannot be less then 0") vertices = [i for i in range(V1 + V2)] rand.shuffle(vertices) G = Graph(V1 + V2) for i in range(V1): for j in range(V2): if utils.bernoulli(p): G.add_edge((vertices[i], vertices[V1 + j])) return G
def funct_dOmeg(H0, T, Omeg, order): """ Return ODEs USAGE: funct_dOmeg(H0, T, Omeg, order) INPUT: H0 - matrix representing initial value of the Hamiltonian (T + V) T - matrix representing kinetic values Omeg - matrix representing Omega values order - integer representing to what order to take the summation OUTPUT: dOmeg solutions from given Omeg """ Hs = funct_Hs(H0, Omeg, order) eta = myComm(T, Hs) dOmeg = np.copy(eta) for i in xrange(1, order): # length of order dOmeg += bernoulli(i) / factorial(i) * adjointOp(eta, Omeg, i) return dOmeg
def simple_with_probability(V, p): ''' Returns a random simple graph on V vertices, with an edge between any two vertices with probability p. This is sometimes referred to as the Erdos-Renyi random graph model. @param V the number of vertices @param p the probability of choosing an edge @return a random simple graph on V vertices, with an edge between any two vertices with probability p @raises ValueError if probability is not between 0 and 1 ''' if p < 0.0 or p > 1.0: raise ValueError('Probability must be between 0 and 1') G = Graph(V) for v in range(V): for w in range(v+1,V,1): if utils.bernoulli(p): G.add_edge((v , w)) return G
def forward(self, input): mask = bernoulli(self.size, 1 - self.p) / (1 - self.p) self._mask = mask return mask * input
def sum_bernoulli(V, p): sum = 0 for x in range(V): if (utils.bernoulli(p)): sum = sum + 1 return sum