def test_univariate(self): np.random.seed(12345) for x in np.linspace(-5, 5, num=11): A = np.array([[x]]) assert_allclose(expm_cond(A), abs(x)) for x in np.logspace(-2, 2, num=11): A = np.array([[x]]) assert_allclose(expm_cond(A), abs(x)) for i in range(10): A = np.random.randn(1, 1) assert_allclose(expm_cond(A), np.absolute(A)[0, 0])
def test_expm_cond_fuzz(self): np.random.seed(12345) eps = 1e-5 nsamples = 10 for i in range(nsamples): n = np.random.randint(2, 5) A = np.random.randn(n, n) A_norm = scipy.linalg.norm(A) X = expm(A) X_norm = scipy.linalg.norm(X) kappa = expm_cond(A) # Look for the small perturbation that gives the greatest # relative error. f = functools.partial(_help_expm_cond_search, A, A_norm, X, X_norm, eps) guess = np.ones(n * n) out = minimize(f, guess, method="L-BFGS-B") xopt = out.x yopt = f(xopt) p_best = eps * _normalized_like(np.reshape(xopt, A.shape), A) p_best_relerr = _relative_error(expm, A, p_best) assert_allclose(p_best_relerr, -yopt * eps) # Check that the identified perturbation indeed gives greater # relative error than random perturbations with similar norms. for j in range(5): p_rand = eps * _normalized_like(np.random.randn(*A.shape), A) assert_allclose(norm(p_best), norm(p_rand)) p_rand_relerr = _relative_error(expm, A, p_rand) assert_array_less(p_rand_relerr, p_best_relerr) # The greatest relative error should not be much greater than # eps times the condition number kappa. # In the limit as eps approaches zero it should never be greater. assert_array_less(p_best_relerr, (1 + 2 * eps) * eps * kappa)
def test_expm_bad_condition_number(self): A = np.array([ [-1.128679820, 9.614183771e4, -4.524855739e9, 2.924969411e14], [0, -1.201010529, 9.634696872e4, -4.681048289e9], [0, 0, -1.132893222, 9.532491830e4], [0, 0, 0, -1.179475332], ]) kappa = expm_cond(A) assert_array_less(1e36, kappa)
def test_expm_cond_fuzz(self): np.random.seed(12345) eps = 1e-5 nsamples = 10 for i in range(nsamples): n = np.random.randint(2, 5) A = np.random.randn(n, n) A_norm = scipy.linalg.norm(A) X = expm(A) X_norm = scipy.linalg.norm(X) kappa = expm_cond(A) # Look for the small perturbation that gives the greatest # relative error. f = functools.partial(_help_expm_cond_search, A, A_norm, X, X_norm, eps) guess = np.ones(n * n) out = minimize(f, guess, method='L-BFGS-B') xopt = out.x yopt = f(xopt) p_best = eps * _normalized_like(np.reshape(xopt, A.shape), A) p_best_relerr = _relative_error(expm, A, p_best) assert_allclose(p_best_relerr, -yopt * eps) # Check that the identified perturbation indeed gives greater # relative error than random perturbations with similar norms. for j in range(5): p_rand = eps * _normalized_like(np.random.randn(*A.shape), A) assert_allclose(norm(p_best), norm(p_rand)) p_rand_relerr = _relative_error(expm, A, p_rand) assert_array_less(p_rand_relerr, p_best_relerr) # The greatest relative error should not be much greater than # eps times the condition number kappa. # In the limit as eps approaches zero it should never be greater. assert_array_less(p_best_relerr, (1 + 2 * eps) * eps * kappa)
def test_expm_cond_smoke(self): np.random.seed(1234) for n in range(1, 4): A = np.random.randn(n, n) kappa = expm_cond(A) assert_array_less(0, kappa)