def test_gauss_seidel_step(self): # the test written below tests if Gauss-seidel step produces the # correct output for a sompletridiagonal SDD matrix A A = array([ [11.07336608, -0.85580894, 0., 0., 0., 0., 0., 0., 0., 0.], [0.34408418, -11.5525474, 1.32434231, 0., 0., 0., 0., 0., 0., 0.], [0., -1.25446038, 11.39901, 0.47072897, 0., 0., 0., 0., 0., 0.], [0., 0., 1.23226501, -10.25414388, -0.3773415, 0., 0., 0., 0., 0.], [0., 0., 0., 1.22311693, 10.2952351, 0.78994453, 0., 0., 0., 0.], [ 0., 0., 0., 0., -0.42566436, -10.52022012, -1.58403561, 0., 0., 0. ], [ 0., 0., 0., 0., 0., 0.65026657, -10.45407605, -1.44470734, 0., 0. ], [0., 0., 0., 0., 0., 0., 0.90469079, 10.33843249, -0.49375486, 0.], [ 0., 0., 0., 0., 0., 0., 0., -0.76157863, -10.82581569, -0.97784685 ], [0., 0., 0., 0., 0., 0., 0., 0., 0.80920961, 11.84434943] ]) b = array([ -0.82211229, -1.40264905, -0.10539367, 0.22763046, 0.15404998, 1.13328694, -2.68971004, 0.18112895, -0.56502348, -0.6756333 ]) x0 = ones(len(b)) D, L, U = decompose(A) x1 = gauss_seidel_step(D, L, U, b, x0) x1_actual = array([ -0.06170079, 0.16227553, 0.09662346, 0.10123638, -0.08866736, -0.19775514, 0.32920744, -0.0703139, -0.00683268, -0.12536298 ]) self.assertAlmostEqual(norm(x1 - x1_actual), 0)
def test_jacobi_step(self): # the test written below tests if jacobi step produces the # correct output for a sompletridiagonal SDD matrix A A = array([ [11.07336608, -0.85580894, 0., 0., 0., 0., 0., 0., 0., 0.], [0.34408418, -11.5525474, 1.32434231, 0., 0., 0., 0., 0., 0., 0.], [0., -1.25446038, 11.39901, 0.47072897, 0., 0., 0., 0., 0., 0.], [0., 0., 1.23226501, -10.25414388, -0.3773415, 0., 0., 0., 0., 0.], [0., 0., 0., 1.22311693, 10.2952351, 0.78994453, 0., 0., 0., 0.], [ 0., 0., 0., 0., -0.42566436, -10.52022012, -1.58403561, 0., 0., 0. ], [ 0., 0., 0., 0., 0., 0.65026657, -10.45407605, -1.44470734, 0., 0. ], [0., 0., 0., 0., 0., 0., 0.90469079, 10.33843249, -0.49375486, 0.], [ 0., 0., 0., 0., 0., 0., 0., -0.76157863, -10.82581569, -0.97784685 ], [0., 0., 0., 0., 0., 0., 0., 0., 0.80920961, 11.84434943] ]) b = array([ -0.82211229, -1.40264905, -0.10539367, 0.22763046, 0.15404998, 1.13328694, -2.68971004, 0.18112895, -0.56502348, -0.6756333 ]) x0 = ones(len(b)) D, L, U = decompose(A) x1 = jacobi_step(D, L, U, b, x0) x1_actual = array([ 0.00304304, 0.26583536, 0.05950848, 0.06117459, -0.18057009, -0.29875676, 0.18129477, -0.02222842, -0.10848162, -0.12536298 ]) self.assertAlmostEqual(norm(x1 - x1_actual), 0)
def test_decompose(self): # the test written below only tests if the identity matrix is properly # decomposed. this is not sufficient for testing if decompose() works # properly but is a good start. A = eye(3) D, L, U = decompose(A) D_actual = eye(3) L_actual = zeros((3,3)) U_actual = zeros((3,3)) self.assertAlmostEqual(norm(D_actual - D), 0) self.assertAlmostEqual(norm(L_actual - L), 0) self.assertAlmostEqual(norm(U_actual - U), 0) #test a 3 x 3 matrix A = array([[1,2,3], [4,5,6], [7,8,9]]) D, L, U = decompose(A) D2 = numpy.diag([1,5,9]) L2 = array([[0,0,0], [4,0,0], [7,8,0]]) U2 = array([[0,2,3], [0,0,6], [0,0,0]]) self.assertAlmostEqual(norm(D2 - D), 0) self.assertAlmostEqual(norm(L2 - L), 0) self.assertAlmostEqual(norm(U2 - U), 0) #test a 4 x 4 matrix and type float A = array([[1,2.0,3,4], [5,6,7.0,8], [9,10,11,12], [13,14,15,16]]) D, L, U = decompose(A) D4 = numpy.diag([1,6,11,16]) L4 = array([[0,0,0,0], [5,0,0,0], [9,10,0,0], [13,14,15,0]]) U4 = array([[0,2.0,3,4], [0,0,7.0,8], [0,0,0,12], [0,0,0,0]]) self.assertAlmostEqual(norm(D4 - D), 0) self.assertAlmostEqual(norm(L4 - L), 0) self.assertAlmostEqual(norm(U4 - U), 0) #test a 1 x 1 matrix A = array([[10]]) D, L, U = decompose(A) D5 = numpy.diag([10]) L5 = None U5 = None self.assertAlmostEquals(norm(D5 - D), 0) self.assertEquals(L5, None) self.assertEquals(U5, None)
def test_decompose(self): # the test written below only tests if the identity matrix is properly # decomposed. this is not sufficient for testing if decompose() works # properly but is a good start. A = eye(3) D, L, U = decompose(A) D_actual = eye(3) L_actual = zeros((3,3)) U_actual = zeros((3,3)) self.assertAlmostEqual(norm(D_actual - D), 0) self.assertAlmostEqual(norm(L_actual - L), 0) self.assertAlmostEqual(norm(U_actual - U), 0)
def test_gauss_seidel_step(self): # the test written below tests if Gauss-seidel step produces the # correct output for a sompletridiagonal SDD matrix A A = array([[ 11.07336608, -0.85580894, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [ 0.34408418, -11.5525474 , 1.32434231, 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [ 0. , -1.25446038, 11.39901 , 0.47072897, 0. , 0. , 0. , 0. , 0. , 0. ], [ 0. , 0. , 1.23226501, -10.25414388, -0.3773415 , 0. , 0. , 0. , 0. , 0. ], [ 0. , 0. , 0. , 1.22311693, 10.2952351 , 0.78994453, 0. , 0. , 0. , 0. ], [ 0. , 0. , 0. , 0. , -0.42566436, -10.52022012, -1.58403561, 0. , 0. , 0. ], [ 0. , 0. , 0. , 0. , 0. , 0.65026657, -10.45407605, -1.44470734, 0. , 0. ], [ 0. , 0. , 0. , 0. , 0. , 0. , 0.90469079, 10.33843249, -0.49375486, 0. ], [ 0. , 0. , 0. , 0. , 0. , 0. , 0. , -0.76157863, -10.82581569, -0.97784685], [ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.80920961, 11.84434943]]) b = array([-0.82211229, -1.40264905, -0.10539367, 0.22763046, 0.15404998, 1.13328694, -2.68971004, 0.18112895, -0.56502348, -0.6756333 ]) x0 = ones(len(b)) D, L, U = decompose(A) x1 = gauss_seidel_step(D, L, U, b, x0) x1_actual = array([-0.06170079, 0.16227553, 0.09662346, 0.10123638, -0.08866736, -0.19775514, 0.32920744, -0.0703139 , -0.00683268, -0.12536298]) x1_actual_correct = array([0.00304304, 0.23614172, -0.02455408, -0.06194853, -0.05440617, -0.25609386, 0.10316296, 0.05625159, -0.04209044, -0.05416704]) self.assertAlmostEqual(min(norm(x1-x1_actual), norm(x1-x1_actual_correct)), 0)
def test_jacobi_step(self): # the test written below tests if jacobi step produces the # correct output for a sompletridiagonal SDD matrix A A = array([[ 11.07336608, -0.85580894, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [ 0.34408418, -11.5525474 , 1.32434231, 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [ 0. , -1.25446038, 11.39901 , 0.47072897, 0. , 0. , 0. , 0. , 0. , 0. ], [ 0. , 0. , 1.23226501, -10.25414388, -0.3773415 , 0. , 0. , 0. , 0. , 0. ], [ 0. , 0. , 0. , 1.22311693, 10.2952351 , 0.78994453, 0. , 0. , 0. , 0. ], [ 0. , 0. , 0. , 0. , -0.42566436, -10.52022012, -1.58403561, 0. , 0. , 0. ], [ 0. , 0. , 0. , 0. , 0. , 0.65026657, -10.45407605, -1.44470734, 0. , 0. ], [ 0. , 0. , 0. , 0. , 0. , 0. , 0.90469079, 10.33843249, -0.49375486, 0. ], [ 0. , 0. , 0. , 0. , 0. , 0. , 0. , -0.76157863, -10.82581569, -0.97784685], [ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.80920961, 11.84434943]]) b = array([-0.82211229, -1.40264905, -0.10539367, 0.22763046, 0.15404998, 1.13328694, -2.68971004, 0.18112895, -0.56502348, -0.6756333 ]) x0 = ones(len(b)) D, L, U = decompose(A) x1 = jacobi_step(D, L, U, b, x0) x1_actual = array([ 0.00304304, 0.26583536, 0.05950848, 0.06117459, -0.18057009, -0.29875676, 0.18129477, -0.02222842, -0.10848162, -0.12536298]) self.assertAlmostEqual(norm(x1-x1_actual), 0)
def test_decompose(self): # the test written below tests if a random dense 10x10 SDD matrix A is properly # decomposed. B=array([[ 0.28384623, -0.00671327, -0.5909395 , 0.22319892, 0.47311985, 0.68994718, 1.68902479, 1.31311603, 1.34724346, 0.87469211], [ 1.4600519 , 1.32770264, -0.07754125, -0.40217056, -0.58388847, -1.51263798, -0.47040971, 2.30832758, 0.92357739, 0.54564659], [ 0.57554678, 0.15559427, 1.90983902, 0.16232167, -0.07962706, -0.54497108, -1.51698454, 0.86898915, 0.96449889, 0.33047333], [-2.29421824, -0.12860935, -2.4615153 , -0.49196098, -0.39143065, 0.7018368 , 0.61227205, -0.0688328 , -0.14342273, 0.49445229], [ 0.20822844, -1.53027653, 1.60181548, -1.78165038, -0.5640091 , 1.18444976, -0.03295762, 1.53161805, 0.86493622, 1.18826723], [ 1.19243729, 0.97582135, -0.39137045, 2.20075989, -0.3963792 , -0.70617887, 0.56454132, -0.16555516, 1.059915 , -0.56712244], [ 1.69045549, 0.67501015, -1.4364708 , 0.8182705 , 1.09775466, -0.61637005, -0.23972642, 0.96483237, 1.80930647, -0.31649356], [ 0.12250029, 0.53998451, 0.71529106, -0.55052708, 0.37901874, -0.45185482, 1.32690993, -0.35890031, 0.86842993, -0.96008517], [-0.79938861, 0.53407704, 1.14140023, 0.39350382, 0.01804328, 0.16847941, -0.22454093, 1.13058928, 1.6950224 , 0.59640698], [-0.16192425, -0.07277925, -0.03477962, 0.36739539, 0.15564998, 1.92979674, 0.84068755, -1.41742836, -0.059325 , 0.26732909]]) D, L, U = decompose(B) D_actual = array(array([[ 0.28384623, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [ 0. , 1.32770264, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [ 0. , 0. , 1.90983902, 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [ 0. , 0. , 0. , -0.49196098, 0. , 0. , 0. , 0. , 0. , 0. ], [ 0. , 0. , 0. , 0. , -0.5640091 , 0. , 0. , 0. , 0. , 0. ], [ 0. , 0. , 0. , 0. , 0. , -0.70617887, 0. , 0. , 0. , 0. ], [ 0. , 0. , 0. , 0. , 0. , 0. , -0.23972642, 0. , 0. , 0. ], [ 0. , 0. , 0. , 0. , 0. , 0. , 0. , -0.35890031, 0. , 0. ], [ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 1.6950224 , 0. ], [ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.26732909]])) L_actual = array([[ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [ 1.4600519 , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [ 0.57554678, 0.15559427, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [-2.29421824, -0.12860935, -2.4615153 , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [ 0.20822844, -1.53027653, 1.60181548, -1.78165038, 0. , 0. , 0. , 0. , 0. , 0. ], [ 1.19243729, 0.97582135, -0.39137045, 2.20075989, -0.3963792 , 0. , 0. , 0. , 0. , 0. ], [ 1.69045549, 0.67501015, -1.4364708 , 0.8182705 , 1.09775466, -0.61637005, 0. , 0. , 0. , 0. ], [ 0.12250029, 0.53998451, 0.71529106, -0.55052708, 0.37901874, -0.45185482, 1.32690993, 0. , 0. , 0. ], [-0.79938861, 0.53407704, 1.14140023, 0.39350382, 0.01804328, 0.16847941, -0.22454093, 1.13058928, 0. , 0. ], [-0.16192425, -0.07277925, -0.03477962, 0.36739539, 0.15564998, 1.92979674, 0.84068755, -1.41742836, -0.059325 , 0. ]]) U_actual = array([[ 0. , -0.00671327, -0.5909395 , 0.22319892, 0.47311985, 0.68994718, 1.68902479, 1.31311603, 1.34724346, 0.87469211], [ 0. , 0. , -0.07754125, -0.40217056, -0.58388847, -1.51263798, -0.47040971, 2.30832758, 0.92357739, 0.54564659], [ 0. , 0. , 0. , 0.16232167, -0.07962706, -0.54497108, -1.51698454, 0.86898915, 0.96449889, 0.33047333], [ 0. , 0. , 0. , 0. , -0.39143065, 0.7018368 , 0.61227205, -0.0688328 , -0.14342273, 0.49445229], [ 0. , 0. , 0. , 0. , 0. , 1.18444976, -0.03295762, 1.53161805, 0.86493622, 1.18826723], [ 0. , 0. , 0. , 0. , 0. , 0. , 0.56454132, -0.16555516, 1.059915 , -0.56712244], [ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.96483237, 1.80930647, -0.31649356], [ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.86842993, -0.96008517], [ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.59640698], [ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ]]) self.assertAlmostEqual(norm(D_actual - D), 0) self.assertAlmostEqual(norm(L_actual - L), 0) self.assertAlmostEqual(norm(U_actual - U), 0)
def test_decompose(self): # the test written below tests if a random dense 10x10 SDD matrix A is properly # decomposed. B = array( [[ 0.28384623, -0.00671327, -0.5909395, 0.22319892, 0.47311985, 0.68994718, 1.68902479, 1.31311603, 1.34724346, 0.87469211 ], [ 1.4600519, 1.32770264, -0.07754125, -0.40217056, -0.58388847, -1.51263798, -0.47040971, 2.30832758, 0.92357739, 0.54564659 ], [ 0.57554678, 0.15559427, 1.90983902, 0.16232167, -0.07962706, -0.54497108, -1.51698454, 0.86898915, 0.96449889, 0.33047333 ], [ -2.29421824, -0.12860935, -2.4615153, -0.49196098, -0.39143065, 0.7018368, 0.61227205, -0.0688328, -0.14342273, 0.49445229 ], [ 0.20822844, -1.53027653, 1.60181548, -1.78165038, -0.5640091, 1.18444976, -0.03295762, 1.53161805, 0.86493622, 1.18826723 ], [ 1.19243729, 0.97582135, -0.39137045, 2.20075989, -0.3963792, -0.70617887, 0.56454132, -0.16555516, 1.059915, -0.56712244 ], [ 1.69045549, 0.67501015, -1.4364708, 0.8182705, 1.09775466, -0.61637005, -0.23972642, 0.96483237, 1.80930647, -0.31649356 ], [ 0.12250029, 0.53998451, 0.71529106, -0.55052708, 0.37901874, -0.45185482, 1.32690993, -0.35890031, 0.86842993, -0.96008517 ], [ -0.79938861, 0.53407704, 1.14140023, 0.39350382, 0.01804328, 0.16847941, -0.22454093, 1.13058928, 1.6950224, 0.59640698 ], [ -0.16192425, -0.07277925, -0.03477962, 0.36739539, 0.15564998, 1.92979674, 0.84068755, -1.41742836, -0.059325, 0.26732909 ]]) D, L, U = decompose(B) D_actual = array( array([[0.28384623, 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 1.32770264, 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 1.90983902, 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., -0.49196098, 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., -0.5640091, 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., -0.70617887, 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., -0.23972642, 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., -0.35890031, 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 1.6950224, 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.26732909]])) L_actual = array([ [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [1.4600519, 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0.57554678, 0.15559427, 0., 0., 0., 0., 0., 0., 0., 0.], [-2.29421824, -0.12860935, -2.4615153, 0., 0., 0., 0., 0., 0., 0.], [ 0.20822844, -1.53027653, 1.60181548, -1.78165038, 0., 0., 0., 0., 0., 0. ], [ 1.19243729, 0.97582135, -0.39137045, 2.20075989, -0.3963792, 0., 0., 0., 0., 0. ], [ 1.69045549, 0.67501015, -1.4364708, 0.8182705, 1.09775466, -0.61637005, 0., 0., 0., 0. ], [ 0.12250029, 0.53998451, 0.71529106, -0.55052708, 0.37901874, -0.45185482, 1.32690993, 0., 0., 0. ], [ -0.79938861, 0.53407704, 1.14140023, 0.39350382, 0.01804328, 0.16847941, -0.22454093, 1.13058928, 0., 0. ], [ -0.16192425, -0.07277925, -0.03477962, 0.36739539, 0.15564998, 1.92979674, 0.84068755, -1.41742836, -0.059325, 0. ] ]) U_actual = array( [[ 0., -0.00671327, -0.5909395, 0.22319892, 0.47311985, 0.68994718, 1.68902479, 1.31311603, 1.34724346, 0.87469211 ], [ 0., 0., -0.07754125, -0.40217056, -0.58388847, -1.51263798, -0.47040971, 2.30832758, 0.92357739, 0.54564659 ], [ 0., 0., 0., 0.16232167, -0.07962706, -0.54497108, -1.51698454, 0.86898915, 0.96449889, 0.33047333 ], [ 0., 0., 0., 0., -0.39143065, 0.7018368, 0.61227205, -0.0688328, -0.14342273, 0.49445229 ], [ 0., 0., 0., 0., 0., 1.18444976, -0.03295762, 1.53161805, 0.86493622, 1.18826723 ], [ 0., 0., 0., 0., 0., 0., 0.56454132, -0.16555516, 1.059915, -0.56712244 ], [0., 0., 0., 0., 0., 0., 0., 0.96483237, 1.80930647, -0.31649356], [0., 0., 0., 0., 0., 0., 0., 0., 0.86842993, -0.96008517], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.59640698], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]]) self.assertAlmostEqual(norm(D_actual - D), 0) self.assertAlmostEqual(norm(L_actual - L), 0) self.assertAlmostEqual(norm(U_actual - U), 0)