def test_solve_safe(): a = np.diag([1.0, 2.0, 1.0]) b = np.array([2.0, 4.0, 1.0]) assert (solve_safe(a, b) == [2.0, 2.0, 1.0]).all() a = np.diag([1.0, 2.0, 0.0]) b = np.array([2.0, 4.0, 1.0]) assert (solve_safe(a, b) == [2.0, 2.0, 0.0]).all()
def solve_cdiis(a): r'''Solve the linear equations found in the cdiis method The following is minimized: .. math: \frac{1}{2} x^T a x under the constraint :math:`\sum_i x_i = 1`. **Arguments:** a The matrix a, an array of size (N,N). ''' n = len(a) assert a.shape == (n, n) assert (a == a.T).all() a2 = np.zeros((n+1, n+1)) a2[:n,:n] = a a2[n,:n] = 1 a2[:n,n] = 1 b2 = np.zeros(n+1) b2[n] = 1 x2 = solve_safe(a2, b2) return x2[:n]
def test_solve_radius_posdef_nl8(): for i in xrange(100): a, b, r, s = get_random_problem(nl=8, posdef=True) radius = np.random.uniform(1e-5, 1e2) center = np.random.normal(0, 0.5*radius, len(b)) try: x1 = solve_radius(a, b, center, radius, r, s) except FeasibilityError: continue assert np.linalg.norm(x1 - center) < radius*(1 + 1e-3) x2 = solve_safe(r, s) assert abs(x1 - x2).max() < 1e-6*abs(x1).max()