def check_random_complex_exact(self): n = 20 a = random([n, n]) + 1j * random([n, n]) for i in range(n): a[i, i] = 20 * (0.1 + a[i, i]) for i in range(2): b = random([n, 3]) x = lstsq(a, b)[0] assert_array_almost_equal(numpy.dot(a, x), b)
def check_random_overdet_large(self): # bug report: Nils Wagner n = 200 a = random([n, 2]) for i in range(2): a[i, i] = 20 * (0.1 + a[i, i]) b = random([n, 3]) x = lstsq(a, b)[0] assert_array_almost_equal(x, direct_lstsq(a, b))
def check_random_complex_overdet(self): n = 20 m = 15 a = random([n, m]) + 1j * random([n, m]) for i in range(m): a[i, i] = 20 * (0.1 + a[i, i]) for i in range(2): b = random([n, 3]) x, res, r, s = lstsq(a, b) assert r == m, "unexpected efficient rank" # XXX: check definition of res assert_array_almost_equal(x, direct_lstsq(a, b, 1))
def test_lstsq(self): from linalg import lstsq shapes = ([10, 3], [3, 10]) for shape in shapes: for b2d in True, False: A = (np.random.rand(np.prod(shape))-.5).reshape(shape) if b2d: b = np.random.randn(shape[0],2) else: b = np.random.randn(shape[0]) x1, residuals1, rank1, s1 = lstsq(A, b) x2, residuals2, rank2, s2 = np.linalg.lstsq(A, b) #print(x1.r) #print(x2) #print(residuals1.r) #print(residuals2) self.assertTrue(np.max(np.abs(x1.r-x2)) < 1e-14) if len(residuals2) > 0: self.assertTrue(np.max(np.abs(residuals1.r-residuals2)) < 1e-14)
def test_lstsq(self): from linalg import lstsq shapes = ([10, 3], [3, 10]) for shape in shapes: for b2d in True, False: A = (np.random.rand(np.prod(shape))-.5).reshape(shape) if b2d: b = np.random.randn(shape[0],2) else: b = np.random.randn(shape[0]) x1, residuals1, rank1, s1 = lstsq(A, b) x2, residuals2, rank2, s2 = np.linalg.lstsq(A, b) #print x1.r #print x2 #print residuals1.r #print residuals2 self.assertTrue(np.max(np.abs(x1.r-x2)) < 1e-14) if len(residuals2) > 0: self.assertTrue(np.max(np.abs(residuals1.r-residuals2)) < 1e-14)
def check_simple_underdet(self): a = [[1, 2, 3], [4, 5, 6]] b = [1, 2] x, res, r, s = lstsq(a, b) # XXX: need independent check assert_array_almost_equal(x, [[-0.05555556], [0.11111111], [0.27777778]])
def check_simple_overdet(self): a = [[1, 2], [4, 5], [3, 4]] b = [1, 2, 3] x, res, r, s = lstsq(a, b) # XXX: check defintion of res assert_array_almost_equal(x, direct_lstsq(a, b))
def check_simple_exact(self): a = [[1, 20], [-30, 4]] for b in ([[1, 0], [0, 1]], [1, 0], [[2, 1], [-30, 4]]): x = lstsq(a, b)[0] assert_array_almost_equal(numpy.dot(a, x), b)