def test_matrix_log(self): M = np.array([[-1, 0], [0, -1]], 'complex') # degenerate negative evals mt.real_matrix_log(M, actionIfImaginary="raise", TOL=1e-6) M = np.array( [[-1, 1e-10], [1e-10, -1]], 'complex' ) # degenerate negative evals, but will generate complex evecs mt.real_matrix_log(M, actionIfImaginary="raise", TOL=1e-6) M = np.array( [[1, 0], [0, -1]], 'd') # a negative *unparied* eigenvalue => log may be imaginary mt.real_matrix_log(M, actionIfImaginary="ignore", TOL=1e-6) self.assertWarns(mt.real_matrix_log, M, actionIfImaginary="warn", TOL=1e-6) with self.assertRaises(ValueError): mt.real_matrix_log(M, actionIfImaginary="raise", TOL=1e-6) with self.assertRaises(AssertionError): mt.real_matrix_log(M, actionIfImaginary="foobar", TOL=1e-6)
def test_matrix_log(self): M = np.array([[-1, 0], [0, -1]], 'complex') # degenerate negative evals logM = mt.real_matrix_log(M, action_if_imaginary="raise", tol=1e-6) self.assertArraysAlmostEqual(spl.expm(logM), M) M = np.array( [[-1, 1e-10], [1e-10, -1]], 'complex' ) # degenerate negative evals, but will generate complex evecs logM = mt.real_matrix_log(M, action_if_imaginary="raise", tol=1e-6) self.assertArraysAlmostEqual(spl.expm(logM), M) with self.assertRaises(ValueError): M = np.array([ [1, 0], [0, -1] ], 'd') # a negative *unparied* eigenvalue => log may be imaginary mt.real_matrix_log(M, action_if_imaginary="raise", tol=1e-6) M = np.array( [[1, 0], [0, -1]], 'd') # a negative *unparied* eigenvalue => log may be imaginary logM = mt.real_matrix_log(M, action_if_imaginary="ignore", tol=1e-6) self.assertArraysAlmostEqual(spl.expm(logM), M)
def test_nullspace(self): a = np.array([[1, 1], [1, 1]]) print("Nullspace = ", mt.nullspace(a)) expected = np.array([[0.70710678], [-0.70710678]]) diff1 = np.linalg.norm(mt.nullspace(a) - expected) diff2 = np.linalg.norm( mt.nullspace(a) + expected) # -1*expected is OK too (just an eigenvector) self.assertTrue(np.isclose(diff1, 0) or np.isclose(diff2, 0)) diff1 = np.linalg.norm(mt.nullspace_qr(a) - expected) diff2 = np.linalg.norm( mt.nullspace_qr(a) + expected) # -1*expected is OK too (just an eigenvector) self.assertTrue(np.isclose(diff1, 0) or np.isclose(diff2, 0)) mt.print_mx(a) b = np.array([[1, 2], [3, 4]], dtype='complex') with self.assertRaises(ValueError): mt.real_matrix_log(b) with self.assertRaises(AssertionError): mt.real_matrix_log(a)
def test_matrix_log(self): M = np.array([[-1, 0], [0, -1]], 'complex') # degenerate negative evals mt.real_matrix_log(M, actionIfImaginary="raise", TOL=1e-6) # TODO assert correctness M = np.array( [[-1, 1e-10], [1e-10, -1]], 'complex' ) # degenerate negative evals, but will generate complex evecs mt.real_matrix_log(M, actionIfImaginary="raise", TOL=1e-6) # TODO assert correctness M = np.array( [[1, 0], [0, -1]], 'd') # a negative *unparied* eigenvalue => log may be imaginary mt.real_matrix_log(M, actionIfImaginary="ignore", TOL=1e-6)
def test_matrix_log_raise_on_no_real_log(self): a = np.array([[1, 1], [1, 1]]) with self.assertRaises(AssertionError): mt.real_matrix_log(a)
def test_matrix_log_raises_on_invalid_action(self): M = np.array([[1, 0], [0, -1]], 'd') with self.assertRaises(AssertionError): mt.real_matrix_log(M, actionIfImaginary="foobar", TOL=1e-6)
def test_matrix_log_raises_on_imaginary(self): M = np.array([[1, 0], [0, -1]], 'd') with self.assertRaises(ValueError): mt.real_matrix_log(M, actionIfImaginary="raise", TOL=1e-6)