def create_P(W: np.matrix): P = np.full_like(W, -1, dtype=np.int32) for index, x in np.ndenumerate(W): i, j = index if i == j or W.item(index) == np.inf or x == 0: continue P.itemset(index, i + 1) # NOT 0 INDEXED! return P
def getScale(cls, m: np.matrix) -> list: scale = [ math.sqrt(m.item(0, 0)**2 + m.item(0, 1)**2 + m.item(0, 2)**2), math.sqrt(m.item(1, 0)**2 + m.item(1, 1)**2 + m.item(1, 2)**2), math.sqrt(m.item(2, 0)**2 + m.item(2, 1)**2 + m.item(2, 2)**2) ] return scale
def get_chosen_cell(grid: numpy.matrix, points: list) -> list: print(points) #FIXME: Only support one cell now assert len(points) == 2 begin = points[0] end = points[1] ret = [] for row in range(begin[0], end[0]): ret += grid[row].flat #FIXME: 假定至少有一排 for col in range(0, end[1] + 1): ret.append(grid.item(end[0], col)) #过滤掉 None ret = [s for s in ret if s != None] print("After check") print(ret) return ret
def getTranslation(cls, m: np.matrix) -> [float, float, float]: ''' Gets the translation of the transform in the 4th row of the matrix. ''' return [m.item(3, 0), m.item(3, 1), m.item(3, 2)]
def function(x: np.matrix): x1 = x.item(0) x2 = x.item(1) return np.matrix([[100 * (x1**2 - x2)**2 + (x1 - 1)**2]])
def hessian(x: np.matrix): x1 = x.item(0) x2 = x.item(1) return np.matrix([[1200 * x1**2 - 400 * x2 + 2, -400 * x1], [-400 * x1, 200 * x2]])
def gradient(x: np.matrix): x1 = x.item(0) x2 = x.item(1) return np.matrix([[2 * (200 * x1 * (x1**2 - x2) + x1 - 1)], [-200 * (x1**2 - x2)]])
def way_dist(A: np.matrix, B: np.matrix) -> int: whole = 0 for i in range(A.shape[0]): whole += (A.item(i, 0)) - B.item((i, 0)) ** 2 return whole