def recalc(self): self.points.clear() for x in linspace(0, 1, 10): for y in linspace(0, 1, 10): self.points.append(self.surface.calculate(x, y)) self.m = matrix(self.points) self.m.shape = (10, 10)
def kraus_to_choi(kraus_list): """ Takes a list of Kraus operators and returns the Choi matrix for the channel represented by the Kraus operators in `kraus_list` """ kraus_mat_list = list(map(lambda x: matrix(x.data.todense()), kraus_list)) op_len = len(kraus_mat_list[0]) op_rng = range(op_len) choi_blocks = array([[sum([op[:, c_ix] * array([op.H[r_ix, :]]) for op in kraus_mat_list]) for r_ix in op_rng] for c_ix in op_rng]) return Qobj(inpt=hstack(hstack(choi_blocks)), dims=[kraus_list[0].dims, kraus_list[0].dims])
def __init__(self, parent): super().__init__(parent) self.setMouseTracking(True) self.setGeometry(parent.geometry()) self.mainPen = QtGui.QPen(QtGui.QColor(0x0d47a1), 2) self.highlightPen = QtGui.QPen(QtGui.QColor(0xFFA0A0), 3) self.surface_points = BilinearSurface.default_points self.current_rotation: Tuple[int] = (0, 0, 0) self.surface = BilinearSurface(self.surface_points) self.points: List[v3] = [] self.m: matrix = matrix([]) self.recalc() self.center = QtCore.QPoint(self.geometry().height() // 2, self.geometry().width() // 2)
def kron(a, b): """ Kronecker product of two arrays. Computes the Kronecker product, a composite array made of blocks of the second array scaled by the first. Parameters ---------- a, b : array_like Returns ------- out : ndarray See Also -------- outer : The outer product Notes ----- The function assumes that the number of dimensions of `a` and `b` are the same, if necessary prepending the smallest with ones. If ``a.shape = (r0,r1,..,rN)`` and ``b.shape = (s0,s1,...,sN)``, the Kronecker product has shape ``(r0*s0, r1*s1, ..., rN*SN)``. The elements are products of elements from `a` and `b`, organized explicitly by:: kron(a,b)[k0,k1,...,kN] = a[i0,i1,...,iN] * b[j0,j1,...,jN] where:: kt = it * st + jt, t = 0,...,N In the common 2-D case (N=1), the block structure can be visualized:: [[ a[0,0]*b, a[0,1]*b, ... , a[0,-1]*b ], [ ... ... ], [ a[-1,0]*b, a[-1,1]*b, ... , a[-1,-1]*b ]] Examples -------- >>> np.kron([1,10,100], [5,6,7]) array([ 5, 6, 7, ..., 500, 600, 700]) >>> np.kron([5,6,7], [1,10,100]) array([ 5, 50, 500, ..., 7, 70, 700]) >>> np.kron(np.eye(2), np.ones((2,2))) array([[1., 1., 0., 0.], [1., 1., 0., 0.], [0., 0., 1., 1.], [0., 0., 1., 1.]]) >>> a = np.arange(100).reshape((2,5,2,5)) >>> b = np.arange(24).reshape((2,3,4)) >>> c = np.kron(a,b) >>> c.shape (2, 10, 6, 20) >>> I = (1,3,0,2) >>> J = (0,2,1) >>> J1 = (0,) + J # extend to ndim=4 >>> S1 = (1,) + b.shape >>> K = tuple(np.array(I) * np.array(S1) + np.array(J1)) >>> c[K] == a[I]*b[J] True """ b = asanyarray(b) a = array(a, copy=False, subok=True, ndmin=b.ndim) ndb, nda = b.ndim, a.ndim nd = max(ndb, nda) if (nda == 0 or ndb == 0): return _nx.multiply(a, b) as_ = a.shape bs = b.shape if not a.flags.contiguous: a = reshape(a, as_) if not b.flags.contiguous: b = reshape(b, bs) # Equalise the shapes by prepending smaller one with 1s as_ = (1, ) * max(0, ndb - nda) + as_ bs = (1, ) * max(0, nda - ndb) + bs # Compute the product a_arr = a.reshape(a.size, 1) b_arr = b.reshape(1, b.size) is_any_mat = isinstance(a_arr, matrix) or isinstance(b_arr, matrix) # In case of `mat`, convert result to `array` result = _nx.multiply(a_arr, b_arr, subok=(not is_any_mat)) # Reshape back result = result.reshape(as_ + bs) transposer = _nx.arange(nd * 2).reshape([2, nd]).ravel(order='f') result = result.transpose(transposer) result = result.reshape(_nx.multiply(as_, bs)) return result if not is_any_mat else matrix(result, copy=False)
import numpy as np from numpy.matrixlib.defmatrix import matrix print('\n') print('What is the size of your Matrix') a=int(input('Enter Dimension 1: ')) b=int(input('Enter Dimension 2: ')) # Create empty matrix with user defined inputs m=np.empty([a,b],dtype=float) m=matrix(m) for i in range(a): for j in range(b): m[i,j]=float(input('Enter Matrix dim [{},{}]: '.format(i+1,j+1))) print('What would you like to do?') print('Enter the number corresponding to the choice you make.\n') userInput=int(input('1.) Solve a system of equations.')) if userInput==1: print("Enter the values for matrix 'b' of Ax=b. You already entered 'A'") bmatrix=np.empty([a,1]) bmatrix=matrix(bmatrix) for i in range(a): bmatrix[i,0]=float(input('Enter b Matrix dim [{},{}]: '.format(i+1,1))) solution = np.linalg.solve(m,bmatrix) print(solution)
def iteration_function( euclidean_matrix, centroid_value, duplicate_list, cluster_points, list_array): #---------------> iteration for finding the clusters. col = len(centroid_value) row = len(list_array) Distance_matrix = matrix([[None for x in range(col)] for x in range(row)]) final_distance_list = [] for k in range(len(list_array)): for i in range(len(centroid_value)): distance = [ pow((point1 - point2), 2) for (point1, point2) in zip(list_array[k], centroid_value[i]) ] # finding the distance between the center and other points. final_distance = math.sqrt(sum(distance)) Distance_matrix[k, i] = final_distance final_distance_list.append(final_distance) # min_val = Distance_matrix.min(1) cluster_number_list = [] for i in range(row): mini = float("inf") for j in range(col): if Distance_matrix[i, j] <= mini: mini = Distance_matrix[i, j] variable = j + 1 cluster_number_list.append( variable ) # finding the cluster number for each point where point should fall. dict = {} for length in range(len(list_array)): dict[length] = list_array[length] mylist = [] for length2 in range(len(centroid_value)): newllist = [] for length1 in range(len(cluster_number_list)): if cluster_number_list[length1] == length2 + 1: newllist.append(length1) mylist.append(newllist) finalmean = [] for item in (mylist): combine_list = [] for item1 in range(len(item)): for key, value in dict.iteritems(): if key == item[item1]: combine_list.append(value) mymean = [] for i in izip_longest(*combine_list): sum_element = 0.00 for item in i: sum_element = sum_element + item mean = float(sum_element) / len( i ) # finding the mean distance for all the points of one cluster. mymean.append(mean) finalmean.append(mymean) # forming the list of the new mean distance. if duplicate_list != cluster_number_list: #stoping condition of the iteration .i.e cluster number in new list should not match with the previous list. return iteration_function( euclidean_matrix, finalmean, cluster_number_list, cluster_points, list_array) #recursively calling the interation fuction. else: return cluster_number_list #when new list of the iteration is equale to the previous list. then that list is the final cluster number list.
import numpy as np from numpy.matrixlib.defmatrix import matrix print('\nWhat is the size of your Matrix') a = int(input('Enter Dimension 1: ')) b = int(input('Enter Dimension 2: ')) # Create empty matrix with user defined inputs m = np.empty([a, b], dtype=float) m = matrix(m) for i in range(a): for j in range(b): m[i, j] = float(input('Enter Matrix dim [{},{}]: '.format(i + 1, j + 1))) print('What would you like to do?') print('Enter the number corresponding to the choice you make.\n') userInput = int(input('1.) Solve a system of equations.')) if userInput == 1: print("Enter the values for matrix 'b' of Ax=b. You already entered 'A'") bmatrix = np.empty([a, 1]) bmatrix = matrix(bmatrix) for i in range(a): bmatrix[i, 0] = float( input('Enter b Matrix dim [{},{}]: '.format(i + 1, 1))) solution = np.linalg.solve(m, bmatrix) print(solution)
def calculate(self, u: float, w: float): a = matrix([1 - u, u]) b = matrix([[1 - w], [w]]) m: matrix = a * self.m * b return v3.from_matrix(m.item(0, 0))
def __init__(self, points: List[v3] = default_points): self.m = matrix(points)
def lsh_algo(json_data): #loading josn file list_array = json_data len_listarray = len(list_array) #making the characteristic matrix columns = len_listarray + 20 rows = 100 user_list_unicode = [None] * len_listarray # print user_list for list1 in list_array: for user_count in range(len_listarray): if user_list_unicode[user_count] == None: user_list_unicode[user_count] = list1[ 0] # saving the user's name break user_list = [] user_list = [x.encode('utf-8') for x in user_list_unicode] # print user_list A = matrix([[0 for x in range(columns)] for x in range(rows)]) count = 0 for i in list_array: for j in range(0, len(i[1])): A[i[1][j], count] = 1 #update the characteristic matrix count += 1 for x in range(rows): i = 1 j = len_listarray while (i < 21): hashfun = (3 * x + i) % 100 while (j < columns): A[x, j] = hashfun j = j + 1 break i = i + 1 # print "matrix:",x," ",A[x] #making of signature matrix col = len_listarray rowsize = 20 sign = matrix([[None for x in range(col)] for x in range(rowsize)]) # print sign p = 0 q = 0 # print A for m in range(rows): for n in range(len_listarray): # print A[m,n] if A[m, n] == 1: for p in range(rowsize): if (sign[p, n] == None): sign[p, n] = A[m, len_listarray + p] if (sign[p, n] > A[m, len_listarray + p]): sign[p, n] = A[m, len_listarray + p] # print sign #-------------------> print signature matrix. final_output_list = [] #Forming 5 bands. 4rows/band col_size = len_listarray rows_size = 4 bandmatrix1 = matrix([[None for x in range(col_size)] for x in range(rows_size)]) bandmatrix2 = matrix([[None for x in range(col_size)] for x in range(rows_size)]) bandmatrix3 = matrix([[None for x in range(col_size)] for x in range(rows_size)]) bandmatrix4 = matrix([[None for x in range(col_size)] for x in range(rows_size)]) bandmatrix5 = matrix([[None for x in range(col_size)] for x in range(rows_size)]) band_no = 1 k = 0 a = 0 b = 0 c = 0 d = 0 for j in range(0, len(sign)): if j >= 0 and j < 4: bandmatrix1[k] = sign[j] k = k + 1 # print bandmatrix1 if j >= 4 and j < 8: bandmatrix2[a] = sign[j] a = a + 1 if j >= 8 and j < 12: bandmatrix3[b] = sign[j] b = b + 1 if j >= 12 and j < 16: bandmatrix4[c] = sign[j] c = c + 1 if j >= 16 and j < 20: bandmatrix5[d] = sign[j] d = d + 1 # print bandmatrix1 # print bandmatrix2 # print bandmatrix3 # print bandmatrix4 # print bandmatrix5 output_list = [] r = 0 # for r in range (len(bandmatrix1)): for c in range(0, len_listarray): for z in range(c + 1, len_listarray): if bandmatrix1[r, c] == bandmatrix1[r, z] and bandmatrix1[ r + 1, c] == bandmatrix1[r + 1, z] and bandmatrix1[ r + 2, c] == bandmatrix1[r + 2, z] and bandmatrix1[ r + 3, c] == bandmatrix1[r + 3, z]: output_list = [] # print "bandmatrix1[r,c]: ",bandmatrix1[r,c] # print "bandmatrix1[r,z]: ",bandmatrix1[r,z] output_list.append(user_list[c]) output_list.append(user_list[z]) output_list.sort() final_output_list.append(output_list) # print output_list temp_name = set() final_output_list_set = set(map(tuple, final_output_list)) final_output_list_set = map(list, final_output_list_set) for element in range(len(final_output_list_set)): # print"*******************", final_output_list_set[element] temp_name.add(final_output_list_set[element][0]) temp_name.add(final_output_list_set[element][1]) # print temp_name s = temp_name # print len(final_output_list_set) w = 0 for c1 in range(0, len_listarray): for z1 in range(c1 + 1, len_listarray): if user_list[c1] != s or user_list[z1] != s: # print user_list[c1] # print user_list[z1] if bandmatrix2[w, c1] == bandmatrix2[w, z1] and bandmatrix2[ w + 1, c1] == bandmatrix2[w + 1, z1] and bandmatrix2[ w + 2, c1] == bandmatrix2[w + 2, z1] and bandmatrix2[ w + 3, c1] == bandmatrix2[w + 3, z1]: output_list = [] output_list.append(user_list[c1]) output_list.append(user_list[z1]) output_list.sort() final_output_list.append(output_list) temp_name = set() final_output_list_set = set(map(tuple, final_output_list)) final_output_list_set = map(list, final_output_list_set) for element in range(len(final_output_list_set)): # print final_output_list_set[element] # print len(final_output_list_set) temp_name.add(final_output_list_set[element][0]) temp_name.add(final_output_list_set[element][1]) # print temp_name s = temp_name # print s # print len(final_output_list_set) h = 0 for c2 in range(0, len_listarray): for z2 in range(c2 + 1, len_listarray): if user_list[c2] != s or user_list[z2] != s: # print user_list[c2] # print user_list[z2] if bandmatrix3[h, c2] == bandmatrix3[h, z2] and bandmatrix3[ h + 1, c2] == bandmatrix3[h + 1, z2] and bandmatrix3[ h + 2, c2] == bandmatrix3[h + 2, z2] and bandmatrix3[ h + 3, c2] == bandmatrix3[h + 3, z2]: output_list = [] output_list.append(user_list[c2]) output_list.append(user_list[z2]) output_list.sort() final_output_list.append(output_list) temp_name = set() final_output_list_set = set(map(tuple, final_output_list)) final_output_list_set = map(list, final_output_list_set) for element in range(len(final_output_list_set)): # print final_output_list_set[element] temp_name.add(final_output_list_set[element][0]) temp_name.add(final_output_list_set[element][1]) # print temp_name s = temp_name # print s # print len(final_output_list_set) p = 0 for c3 in range(0, len_listarray): for z3 in range(c3 + 1, len_listarray): if user_list[c3] != s or user_list[z3] != s: # print user_list[c2] # print user_list[z2] if bandmatrix4[p, c3] == bandmatrix4[p, z3] and bandmatrix4[ p + 1, c3] == bandmatrix4[p + 1, z3] and bandmatrix4[ p + 2, c3] == bandmatrix4[p + 2, z3] and bandmatrix4[ p + 3, c3] == bandmatrix4[p + 3, z3]: output_list = [] output_list.append(user_list[c3]) output_list.append(user_list[z3]) output_list.sort() final_output_list.append(output_list) temp_name = set() final_output_list_set = set(map(tuple, final_output_list)) final_output_list_set = map(list, final_output_list_set) for element in range(len(final_output_list_set)): # print final_output_list_set[element] temp_name.add(final_output_list_set[element][0]) temp_name.add(final_output_list_set[element][1]) # print temp_name s = temp_name # print len(final_output_list_set) t = 0 for c4 in range(0, len_listarray): for z4 in range(c4 + 1, len_listarray): if user_list[c4] != s or user_list[z4] != s: # print user_list[c2] # print user_list[z2] if bandmatrix5[t, c4] == bandmatrix5[t, z4] and bandmatrix5[ t + 1, c4] == bandmatrix5[t + 1, z4] and bandmatrix5[ t + 2, c4] == bandmatrix5[t + 2, z4] and bandmatrix5[ t + 3, c4] == bandmatrix5[t + 3, z4]: output_list = [] output_list.append(user_list[c4]) output_list.append(user_list[z4]) output_list.sort() final_output_list.append(output_list) temp_name = set() final_output_list_set = set(map(tuple, final_output_list)) final_output_list_set = map(list, final_output_list_set) # if checker == True: for element in range(len(final_output_list_set)): # if checker == True: print final_output_list_set[element] # print len(final_output_list_set) listing = [] listing = final_output_list_set