示例#1
0
class LowerSystemResolutionTest(unittest.TestCase):
    low_matrix = Matrix(4, 4).set_data([
        2.0, 0.0, 0.0, 0.0, -1.0, 3.0, 0.0, 0.0, 2.0, 0.0, 2.0, 0.0, 1.0, -2.0,
        1.0, 1.0
    ])
    sys_vec = Vector(4).set_data([20, -16, 40, 28])
    expected_solution = Vector(4).set_data([10, -2, 10, 4])

    def test_lower_system_resolution(self):
        actual = solve_lower_sys(self.low_matrix, self.sys_vec)
        self.assertEqual(self.expected_solution, actual)
示例#2
0
def solve_upper_sys(up_matrix: Matrix, vector: Vector) -> Vector:
    """
    Given an upper triangular matrix `up_matrix` [U] and a vector
    `vector` [b], computes the [U][x] = [b] system solution,
    [x], by backward substitution.

    :param up_matrix: upper triangular `Matrix` [U]
    :param vector: system's `Vector` [b]
    :return: solution vector [x]
    """
    size = vector.length
    last_index = size - 1
    solution = Vector(size)

    for i in range(last_index, -1, -1):
        _sum = 0.0

        for j in range(i + 1, size):
            u_ij = up_matrix.value_at(i, j)
            x_j = solution.value_at(j)
            _sum += u_ij * x_j

        y_i = vector.value_at(i)
        u_ii = up_matrix.value_at(i, i)
        solution_val = (y_i - _sum) / u_ii
        solution.set_value(solution_val, i)

    return solution
示例#3
0
class CholeskyTest(unittest.TestCase):
    sys_matrix = Matrix(4, 4).set_data([
        4.0, -2.0, 4.0, 2.0,
        -2.0, 10.0, -2.0, -7.0,
        4.0, -2.0, 8.0, 4.0,
        2.0, -7.0, 4.0, 7.0
    ])
    low_matrix = Matrix(4, 4).set_data([
        2.0, 0.0, 0.0, 0.0,
        -1.0, 3.0, 0.0, 0.0,
        2.0, 0.0, 2.0, 0.0,
        1.0, -2.0, 1.0, 1.0
    ])
    sys_vec = Vector(4).set_data([20, -16, 40, 28])
    low_solution = Vector(4).set_data([10, -2, 10, 4])
    solution = Vector(4).set_data([1.0, 2.0, 3.0, 4.0])

    def test_lower_matrix_decomposition(self):
        actual = lower_matrix_decomposition(self.sys_matrix)
        self.assertEqual(self.low_matrix, actual)

    def test_solve_system(self):
        actual = cholesky_solve(self.sys_matrix, self.sys_vec)
        self.assertEqual(self.solution, actual)
示例#4
0
 def test_length(self):
     self.assertEqual(5, Vector(5).length)
示例#5
0
    def test_multiply_vectors(self):
        v1 = Vector(2).set_data([4, 5])
        v2 = Vector(2).set_data([2, 3])
        expected = Vector(2).set_data([8, 15])

        self.assertEqual(expected, v1 * v2)
示例#6
0
    def test_add_vectors(self):
        v1 = Vector(2).set_data([4, 5])
        v2 = Vector(2).set_data([2, 3])
        expected = Vector(2).set_data([6, 8])

        self.assertEqual(expected, v1 + v2)
示例#7
0
    def test_subtract_vectors(self):
        v1 = Vector(2).set_data([4, 5])
        v2 = Vector(2).set_data([2, 1])
        expected = Vector(2).set_data([2, 4])

        self.assertEqual(expected, v1 - v2)
示例#8
0
    def test_scaled(self):
        vector = Vector(3).set_data([1, 2, 3])
        expected = Vector(3).set_data([2, 4, 6])

        self.assertEqual(expected, vector.scaled(2))
示例#9
0
 def test_add_to_value(self):
     vector = Vector(2).set_data([1, 2]).add_to_value(10, 0)
     self.assertEqual(11, vector.value_at(0))
     self.assertEqual(2, vector.value_at(1))
示例#10
0
 def test_set_get_value(self):
     value = 10.0
     vector = Vector(2).set_value(value, 1)
     self.assertEqual(0.0, vector.value_at(0))
     self.assertEqual(value, vector.value_at(1))
示例#11
0
 def test_unset_value_is_zero(self):
     vector = Vector(2)
     self.assertEqual(0.0, vector.value_at(0))
     self.assertEqual(0.0, vector.value_at(1))
示例#12
0
 def test_sum(self):
     vector = Vector(3).set_data([1, 2, 3])
     self.assertEqual(6, vector.sum)