def max_cut_loss(z, clause_list): ''' loss for a max cut problem. ''' loss = 0 for w, (start, end) in clause_list: loss -= w * (1 - 2 * (get_bit(z, start) ^ get_bit(z, end))) return loss
def is_vertex_cover(graph, z): """ checks if z (an integer) represents a valid vertex cover for graph adjacency matrix graph, with n vertices """ for e in graph.es: if get_bit(z, e.source) == 0 and get_bit(z, e.target) == 0: return False return True
def sra(machine, r, adr, x): v = get_effective_address(machine, adr, x) p = l2a(machine.GR[r]) prev_p = p sign = get_bit(machine.GR[r], 15) ans = (p >> v) & 0x7fff if sign == 0: ans = ans & 0x7fff else: ans = ans | 0x8000 machine.GR[r] = ans if 0 < v: return flags(machine.GR[r], OF=get_bit(prev_p, v - 1)) else: return flags(machine.GR[r])
def get_dat(self, branch): if not self.rsc: return [int((branch & (2**(self.K - 1))) > 0)] else: return list( np.mod( np.matmul(self.gen_feedback, (dec2bin(branch, self.K))) + get_bit(branch, self.K - 1), 2))
def srl(machine, r, adr, x): v = get_effective_address(machine, adr, x) p = machine.GR[r] prev_p = p ans = machine.GR[r] >> v ans = ans & 0xffff machine.GR[r] = ans if 0 < v: return flags(machine.GR[r], OF=get_bit(prev_p, (v - 1))) else: return flags(machine.GR[r])
def sll(machine, r, adr, x): v = get_effective_address(machine, adr, x) p = machine.GR[r] prev_p = p ans = p << v ans = ans & 0xffff machine.GR[r] = ans if 0 < v: return flags(machine.GR[r], logical=True, OF=get_bit(prev_p, 15 - (v - 1))) else: return flags(machine.GR[r], logical=True)
def vertex_cover_loss(z, graph, mask): """ the objective function to minimize: -(# of 0 in a bit string), corresponding to maximising the number of vertices NOT in the vertex cover """ if not mask[z]: return 0 n = graph.vcount() s = 0 for i in range(n): s += get_bit(z, i) return s - n
def flags(result, logical=False, ZF=None, SF=None, OF=None): ''' 計算結果に応じたフラグを返す 論理演算の場合は第二引数をTrueにする ''' if ZF is None: ZF = (result == 0) if SF is None: SF = (get_bit(result, 15) == 0) if OF is None: if logical is True: OF = (result < 0 or 0xffff < result) else: OF = (result < -32768 or 0x7fff < result) return map(int, (ZF, SF, OF))
def read_bit(self, slave_id, address, bit): message = tcp.read_holding_registers(slave_id, address, 1) return get_bit(tcp.send_message(message, self.sock)[0],bit)
def is_tcp(self): """Return whether or not the socket is using TCP""" return utils.get_bit(self.type, socket.SOCK_STREAM - 1)
def is_udp(self): """Return whether or not the socket is using UDP""" return utils.get_bit(self.type, socket.SOCK_DGRAM - 1)
def is_udp(self): """Return whether or not the socket is using UDP""" return utils.get_bit(self.type, socket.SOCK_DGRAM-1)
def is_tcp(self): """Return whether or not the socket is using TCP""" return utils.get_bit(self.type, socket.SOCK_STREAM-1)