def find_3_missing(n, number_array): expected_sum = n * (n + 1) / 2 expected_squared_sum = n * (n + 1) * (2 * n + 1) / 6 expected_cubed_sum = (n * (n + 1) / 2)**2 actual_sum = 0 actual_squared_sum = 0 actual_cubed_sum = 0 for num in number_array: actual_sum += num actual_squared_sum += num**2 actual_cubed_sum += num**3 x = expected_sum - actual_sum y = expected_squared_sum - actual_squared_sum z = expected_cubed_sum - actual_cubed_sum k1, k2, k3 = symbols('k1 k2 k3') eqs = [ k1 + k2 + k3 - x, k1**2 + k2**2 + k3**2 - y, k1**3 + k2**3 + k3**3 - z ] solutions = solve_poly_system(eqs, [k1, k2, k3]) return solutions[0]
def _do_ellipse_intersection(self, o): """The intersection of two ellipses. Private helper method for `intersection`. """ seq = self.equation() variables = self.equation().atoms(C.Symbol) if len(variables) > 2: return None x, y = variables oeq = o.equation(x=x, y=y) # until the following line works... # result = solve([seq, oeq], [x, y]) # return [Point(*r) for r in result if im(r[0]).is_zero and im(r[1]).is_zero] # we do this: if self.center[0] == o.center[0] or self.center[1] == o.center[1]: result = solve_poly_system([seq, oeq], x, y) return [ Point(*r) for r in result if im(r[0]).is_zero and im(r[1]).is_zero ] raise NotImplementedError( "Off-axis Ellipse intersection not supported.")
def construct_sense_matrix(self, base_approximation=None, d_approximation=None): if base_approximation is None: base_approximation = deepcopy(self.last_approximation) assert base_approximation is not None self.p_var = self.dP[self.known_q_nodes_indexes, :] self.p_fix = self.dP[self.known_p_nodes_indexes, :] self.q_var = self.dQ[self.known_p_nodes_indexes, :] self.q_fix = self.dQ[self.known_q_nodes_indexes, :] a_q = self.A[self.known_q_nodes_indexes, :] a_p = self.A[self.known_p_nodes_indexes, :] a_f_q = self.AF[self.known_q_nodes_indexes, :] a_f_p = self.AF[self.known_p_nodes_indexes, :] a_l_q = self.AL[self.known_q_nodes_indexes, :] a_l_p = self.AL[self.known_p_nodes_indexes, :] d_f = self.DF.subs(base_approximation.items()) d_l = self.DL.subs(base_approximation.items()) self.M = a_q * (d_f * a_f_q.transpose() + d_l * a_l_q.transpose()) self.inv_M = deepcopy(self.M).inv() self.M_PQ = a_p * (d_f * a_f_q.transpose() + d_l * a_l_q.transpose()) self.M_QP = a_q * (d_f * a_f_p.transpose() + d_l * a_l_p.transpose()) self.M_PP = a_p * (d_f * a_f_p.transpose() + d_l * a_l_p.transpose()) self.p_var = self.inv_M * self.q_fix - self.inv_M * self.M_QP * self.p_fix self.q_var = self.M_PQ * self.inv_M * self.q_fix + (self.M_PP - self.M_PQ * self.inv_M * self.M_QP) * self.p_fix if d_approximation is not None: self.p_var = self.p_var.subs(d_approximation.items()) self.q_var = self.q_var.subs(d_approximation.items()) self.nQ = (self.A * self.X).subs(base_approximation.items()) + sm.Matrix([self.q_fix, self.q_var]).subs( d_approximation.items()) self.nP = self.P[self.known_p_nodes_indexes + self.known_q_nodes_indexes, :].subs( base_approximation.items()) + sm.Matrix([self.p_fix, self.p_var]).subs(d_approximation.items()) self.current_sort_P = self.P[self.known_p_nodes_indexes + self.known_q_nodes_indexes, :] self.current_sort_Q = self.Q_symbols[self.known_q_nodes_indexes + self.known_p_nodes_indexes, :] self.d_approximation = dict() for variable, value in zip(self.current_sort_P, self.nP): self.d_approximation[str(variable)] = value x = solve_poly_system(self.A * self.X - self.nQ, self.X.atoms(sm.Symbol))[0] x = {str(sym): value for value, sym in zip(x, self.X.atoms(sm.Symbol))} self.d_approximation.update(x) self.approximations.append(self.d_approximation) return self
def _do_ellipse_intersection(self, o): """ Find the intersection of two ellipses. """ seq = self.equation() variables = self.equation().atoms(C.Symbol) if len(variables) > 2: return None x, y = variables oeq = o.equation(x=x, y=y) # until the following line works... # result = solve([seq, oeq], [x, y]) # return [Point(*r) for r in result if im(r[0]).is_zero and im(r[1]).is_zero] # we do this: if self.center[0] == o.center[0] or self.center[1] == o.center[1]: result = solve_poly_system([seq, oeq], x, y) return [Point(*r) for r in result if im(r[0]).is_zero and im(r[1]).is_zero] raise NotImplementedError("Off-axis Ellipse intersection not supported.")
def find_k_missing(n, number_array, k): expected_kth_sums_list = [0] * k expected_kth_sums = numpy.array(expected_kth_sums_list, dtype='int64') full_number_array = list(xrange(1, n + 1)) numpy_number_array = numpy.array(full_number_array, dtype='int64') actual_kth_sums = [0] * k for m in range(1, k + 1): kth_array = numpy.power(numpy_number_array, m) expected_kth_sums[m - 1] = sum(kth_array) for num in number_array: for index in range(1, k + 1): actual_kth_sums[index - 1] += num**index # for k missing numbers, i'll need k equations equation_variables = symbols('k0:%d' % (k)) equations = {} for j in range(1, k + 1): equation_name = "" for h in range(1, k + 1): if (h != k): equation_name += str( equation_variables[h - 1]) + "**" + str(j) + " + " else: equation_name += str(equation_variables[h - 1]) + "**" + str(j) equations[j - 1] = equation_name equation_list = list() listIndex = 0 for key, value in equations.iteritems(): listIndex += 1 equation_list.append(value + " - " + str((expected_kth_sums[listIndex - 1] - actual_kth_sums[listIndex - 1]))) solutions = solve_poly_system(equation_list, equation_variables) return solutions[0]
def find_k_missing(n, number_array, k): expected_kth_sums_list = [0] * k expected_kth_sums = numpy.array(expected_kth_sums_list, dtype='int64') full_number_array = list(xrange(1,n+1)) numpy_number_array = numpy.array(full_number_array, dtype='int64') actual_kth_sums = [0] * k for m in range(1, k+1): kth_array = numpy.power(numpy_number_array, m) expected_kth_sums[m-1] = sum(kth_array) for num in number_array: for index in range(1, k+1): actual_kth_sums[index-1] += num**index # for k missing numbers, i'll need k equations equation_variables = symbols('k0:%d'%(k)) equations = {} for j in range(1, k+1): equation_name = "" for h in range(1, k+1): if (h != k): equation_name += str(equation_variables[h-1]) + "**" + str(j) + " + " else: equation_name += str(equation_variables[h-1]) + "**" + str(j) equations[j-1] = equation_name equation_list = list() listIndex = 0 for key, value in equations.iteritems(): listIndex += 1 equation_list.append(value + " - " + str((expected_kth_sums[listIndex-1] - actual_kth_sums[listIndex-1]))) solutions = solve_poly_system(equation_list, equation_variables) return solutions[0]
def find_3_missing(n, number_array): expected_sum = n*(n+1)/2 expected_squared_sum = n*(n+1)*(2*n+1)/6 expected_cubed_sum = (n*(n+1)/2)**2 actual_sum = 0 actual_squared_sum = 0 actual_cubed_sum = 0 for num in number_array: actual_sum += num actual_squared_sum += num**2 actual_cubed_sum += num**3 x = expected_sum - actual_sum y = expected_squared_sum - actual_squared_sum z = expected_cubed_sum - actual_cubed_sum k1, k2, k3 = symbols('k1 k2 k3') eqs = [k1 + k2 + k3 - x, k1**2 + k2**2 + k3**2 - y, k1**3 + k2**3 + k3**3 - z] solutions = solve_poly_system(eqs, [k1,k2,k3]) return solutions[0]