def test_qr_complex(self): shapes = [(20, 15), (15, 20), (20, 20)] for m, n in shapes: np.random.seed(1) # always get the same random a = np.random.rand(m, n).astype(np.complex_) + 1j * np.random.rand(m, n) q, r = qr(a, reduced=False) self.assertAlmostEqual(0, np.absolute(a - q @ r).sum()) # val.1 if m == n: self.assertAlmostEqual(q.T.conj().all(), np.linalg.inv(q).all()) # val.6 self.assertAlmostEqual(1, norm(np.linalg.det(q))) # val. 3 self.assertTrue(np.allclose(q.T.conj() @ q, q @ q.T.conj())) # val.4 self.assertTrue(np.allclose(np.identity(q.shape[0]), q @ q.T.conj())) # val.5 self.assertTrue(np.allclose(np.triu(r), r)) # val.7 q, r = qr(a) mgs_q, mgs_r = mgs(a) self.assertTrue(np.allclose(np.absolute(mgs_q), np.absolute(q))) # val.2 self.assertTrue(np.allclose(np.absolute(mgs_r), np.absolute(r))) # val.2 a = np.array([[5. + 5.j, -5.j, 2.], [-4.j, 8. + 8.j, -6.], [0., -6., 10.]]) b = np.array([3., 5. + 4.j, -2.]).reshape(-1, 1) x_expected = np.array([[0.6149065 + 0.1621229j], [0.7862721 + 0.0385111j], [0.2717633 + 0.0231067j]]) x_actual = solve(a, b) self.assertAlmostEqual(x_expected.all(), x_actual.all()) # val.8
def fit_to_sphere(point, radius=1): # fit one 3-dim vector to a sphere # applies to numpy array with 3d rows # freaks out when point is 0 try: fitted = radius * point / norm(point) except: fitted = 0 return fitted
def extract_lengths(data): lengths = dict() for seg in data_model.KIN_TREE: # for now, stupidly average over whole sample try: lengths[seg] = np.mean(norm(np.array(data[seg[1]]) - np.array(data[seg[0]]))) except KeyError: pass return lengths
def test_norm(self): v_r = np.array([[5.], [4.], [1.]]) self.assertEqual(np.sqrt(5.**2 + 4.**2 + 1.), norm(v_r)) v_c = v_r + 1j * np.array([[1.], [0.], [3.]]) self.assertEqual(np.sqrt(5.**2 + 1. + 4.**2 + 1. + 3**2), norm(v_c))
def run(self): while True: # Get the work from the queue and expand the tuple current_env_variables, current_iteration, buffer_env_predicate_pairs = self.queue.get( ) self.logger.info( "Env variables for binary search: {}, Iteration: {}".format( current_env_variables.get_params_string(), current_iteration)) t_env_variables = copy.deepcopy(self.init_env_variables) f_env_variables = copy.deepcopy(current_env_variables) binary_search_counter = 0 max_binary_search_iterations = 20 search_suffix = "binary_search_" if self.param_names: search_suffix += self.param_names_string + "_" search_suffix += str(current_iteration) + "_" + str( binary_search_counter) dist = norm( env_vars_1=f_env_variables, env_vars_2=t_env_variables) / norm(env_vars_1=t_env_variables) execution_times = [] regression_times = [] env_predicate_pairs = [] while dist > self.binary_search_epsilon: new_env_variables = get_binary_search_candidate( t_env_variables=t_env_variables, f_env_variables=f_env_variables, algo_name=self.algo_name, env_name=self.env_name, param_names=self.param_names, discrete_action_space=self.discrete_action_space, buffer_env_predicate_pairs=buffer_env_predicate_pairs, ) self.logger.debug("New env after binary search: {}".format( new_env_variables.get_params_string())) env_predicate_pair, execution_time, regression_time = self.runner.execute_train( current_iteration=current_iteration, search_suffix=search_suffix, current_env_variables=new_env_variables, _start_time=self.start_time, ) execution_times.append(execution_time) regression_times.append(regression_time) env_predicate_pairs.append(env_predicate_pair) if env_predicate_pair.is_predicate(): self.logger.debug("New t_env found: {}".format( env_predicate_pair.get_env_variables( ).get_params_string())) t_env_variables = copy.deepcopy( env_predicate_pair.get_env_variables()) else: self.logger.debug("New f_env found: {}".format( env_predicate_pair.get_env_variables( ).get_params_string())) f_env_variables = copy.deepcopy( env_predicate_pair.get_env_variables()) dist = norm(env_vars_1=f_env_variables, env_vars_2=t_env_variables) / norm( env_vars_1=t_env_variables) if binary_search_counter == max_binary_search_iterations: break binary_search_counter += 1 search_suffix = "binary_search_" if self.param_names: search_suffix += self.param_names_string + "_" search_suffix += str(current_iteration) + "_" + str( binary_search_counter) self.logger.info("dist {} <= binary_search_epsilon {}".format( dist, self.binary_search_epsilon)) self.queue_result.put_nowait(( t_env_variables, f_env_variables, env_predicate_pairs, max_binary_search_iterations, binary_search_counter, current_iteration, execution_times, regression_times, )) self.queue.task_done()