Recursive Gauss Elimination algorithm

时间:2015-09-28 21:25:50

标签: python algorithm python-3.x recursion linear-algebra

I'm studying functional programming concepts in Python 3, so I wrote this Gauss Elimination algorithm with tail-recursion. The algorithm passes all tests I found in a text-book except for one. I had to write simple Matrix and Vector classes along with some accessory functions, because the web-platform I use doesn't have NumPy (the classes are provided at the bottom).

NB! Based on the first answer I received I should stress that there is a cutoff for arithmetics precision (you can find DIGITS in the block at the bottom)

The algorithm

def istrue(iterable, key=bool):
    """
    :type iterable: collections.Iterable
    :type key: (object) -> bool
    :rtype: tuple[bool]
    """
    try:
        return tuple(map(int, map(key, iterable)))
    except TypeError:
        return bool(iterable),


def where(iterable, key=bool):
    """
    :type iterable: collections.Iterable
    :type key: (object) -> bool
    :rtype: tuple[int]
    """
    return tuple(i for i, elem in enumerate(iterable) if key(elem))


def next_true(iterable, i, key=bool):
    """
    Returns position of a True element next to the i'th element
    (iterable[j]: j>i, key(iterable[j]) -> True) if it exists.
    :type iterable: collections.Iterable
    :type i: int
    :rtype: int | None
    """
    true_mask = istrue(iterable, key)
    try:
        return where(enumerate(true_mask),
                     key=lambda ind_val: ind_val[0] > i and ind_val[1])[0]
    except IndexError:
        # No elements satisfy the condition
        return None

def row_echelon(matrix):
    """
    Transforms matrix into the row echelon form
    :type matrix: Matrix
    :return: upper-triangular matrix
    :rtype: Matrix
    """
    @optimize_tail_recursion
    def operator(m, var):
        """
        :type m: Matrix
        :type var: int
        :rtype: Matrix
        """
        # if we have already processed all variables we could
        if var > m.ncol - 1 or var > m.nrow - 1:
            return m
        # if matrix[var][var] is zero and there exist i such that
        # matrix[i][var] > 0, i > j
        elif not m[var][var] and sum(istrue(m.col(var))[var:]):
            i = next_true(istrue(m.col(var)), var)
            return operator(m.permute(var, i), var)
        # if |{matrix[i][var], 0 <= i < nrow(matrix)}| > 1, matrix[var][var]!=0
        elif m[var][var] and sum(istrue(m.col(var))) - 1:
            i = tuple(ind for ind in where(m.col(var)) if ind != var)[0]
            coeff = - m[i][var] / m[var][var]
            return operator(m.add_rows(var, i, coeff), var)
        # if matrix[var][var] is zero and there is no i such that
        # matrix[i][var] > 0, i > j
        # or all possible elements were eliminated
        return operator(m.normalise_row(var, var), var+1)

    return operator(matrix, 0)


def solve_linear_system(augmented_matrix):
    """
    :type augmented_matrix: Matrix
    :return: str | tuple[float]
    """
    row_echelon_m = row_echelon(augmented_matrix)
    print(row_echelon_m)
    left_side, right_side = (row_echelon_m[:, :row_echelon_m.ncol-1],
                             row_echelon_m.col(-1))
    nontrivial = istrue(left_side, key=lambda row: bool(sum(row)))
    rank = sum(nontrivial)

    # if there exists an equation with trivial coefficients and nontrivial
    # right-hand side
    if sum(not sum(l_side) and r_side for l_side, r_side
           in zip(left_side, right_side)):
        return NO

    # rank(matrix) < number of variables
    elif rank < left_side.ncol:
        return INF
    return right_side

Tests

# must be (10, 5, -20.0) - passed
test1 = Matrix(((0.12, 0.18, -0.17, 5.5), (0.06, 0.09, 0.15, -1.95), (0.22, -0.1, 0.06, 0.5)))
# infinite number of solution - passed
test2 = Matrix((((1, 2, -1, 3, 7), (2, 4, -2, 6, 14), (1, -1, 3, 1, -1)))
# no solutions - passed
test3 = Matrix(((2, -1, 3, 1), (2, -1, -1, -2), (4, -2, 6, 0), (6, 8, -7, 2)))
# infinite number of solution - passed
test4 = Matrix(((3, -2, 1, 0), (5, -14, 15, 0), (1, 2, -3, 0)))
# infinite number of solution - passed
test5 = Matrix((((2, 3, -1, 1, 0), (2, 7, -3, 0, 1), (0, 4, -2, -1, 1), (2, -1, 1, 2, -1), (4, 10, -4, 1, 1)))
# no solutions - failed. My algorithm returns Inf
test6 = Matrix(((3, -5, 2, 4, 2), (7, -4, 1, 3, 5), (5, 7, -4, -6, 3)))

Let's see how it transforms the matrix step by step:

solve_linear_system(test6) # -> 

# The integer on the right side denoted the variable that is being eliminated at the moment using the corresponding row. 

((3.0, -5.0, 2.0, 4.0, 2.0),
 (7.0, -4.0, 1.0, 3.0, 5.0),
 (5.0, 7.0, -4.0, -6.0, 3.0)) 0

((3.0, -5.0, 2.0, 4.0, 2.0),
 (0.0, 7.666666666666668, -3.666666666666667, -6.333333333333334, 0.33333333333333304),
 (5.0, 7.0, -4.0, -6.0, 3.0)) 0

((3.0, -5.0, 2.0, 4.0, 2.0),
 (0.0, 7.666666666666668, -3.666666666666667, -6.333333333333334, 0.33333333333333304),
 (0.0, 15.333333333333334, -7.333333333333334, -12.666666666666668, -0.3333333333333335)) 0

((1.0, -1.6666666666666665, 0.6666666666666666, 1.3333333333333333, 0.6666666666666666),
 (0.0, 7.666666666666668, -3.666666666666667, -6.333333333333334, 0.33333333333333304),
 (0.0, 15.333333333333334, -7.333333333333334, -12.666666666666668, -0.3333333333333335)) 1

((1.0, 0.0, -0.13043478260869557, -0.043478260869564966, 0.7391304347826085),
 (0.0, 7.666666666666668, -3.666666666666667, -6.333333333333334, 0.33333333333333304),
 (0.0, 15.333333333333334, -7.333333333333334, -12.666666666666668, -0.3333333333333335)) 1

((1.0, 0.0, -0.13043478260869557, -0.043478260869564966, 0.7391304347826085),
 (0.0, 7.666666666666668, -3.666666666666667, -6.333333333333334, 0.33333333333333304),
 (0.0, 0.0, -8.88e-16, -1.776e-15, -0.9999999999999994)) 1

((1.0, 0.0, -0.13043478260869557, -0.043478260869564966, 0.7391304347826085),
 (0.0, 0.9999999999999999, -0.4782608695652173, -0.826086956521739, 0.04347826086956517),
 (0.0, 0.0, -8.88e-16, -1.776e-15, -0.9999999999999994)) 2

((1.0, 0.0, 0.0, 0.21739130434782616, 146886016451234.4),
 (0.0, 0.9999999999999999, -0.4782608695652173, -0.826086956521739, 0.04347826086956517),
 (0.0, 0.0, -8.88e-16, -1.776e-15, -0.9999999999999994)) 2

((1.0, 0.0, 0.0, 0.21739130434782616, 146886016451234.4),
 (0.0, 0.9999999999999999, 0.0, 0.13043478260869557, 538582060321190.4),
 (0.0, 0.0, -8.88e-16, -1.776e-15, -0.9999999999999994)) 2

((1.0, 0.0, 0.0, 0.21739130434782616, 146886016451234.4),
 (0.0, 0.9999999999999999, 0.0, 0.13043478260869557, 538582060321190.4),
 (-0.0, -0.0, 0.9999999999999999, 1.9999999999999998, 1126126126126125.2)) 3

I get a consistent system with infinite number of solutions. Everything seems right to me, yet I'm supposed to get this inconsistent system (I checked it with NumPy and R):

((1, 0, -3/23, -1/23, 0),
 (0, 1, -11/23, -19/23, 0),
 (0, 0, 0, 0, 1)) 

I hope I'm missing something obvious and simple. I hope the code is documented well enough to be readable. Thank you in advance.

Classes, accessory functions and constants:

NO = "NO"
YES = "YES"
INF = "INF"

DIGITS = 16

def typemap(iterable, types):
    try:
        return all(isinstance(elem, _type)
                   for elem, _type in zip(iterable, types))
    except TypeError:
        return False

class TailRecursionException(BaseException):
    def __init__(self, args, kwargs):
        self.args = args
        self.kwargs = kwargs

def optimize_tail_recursion(g):
    def func(*args, **kwargs):
        f = sys._getframe()
        if f.f_back and f.f_back.f_back and f.f_back.f_back.f_code == f.f_code:
            raise TailRecursionException(args, kwargs)
        else:
            while 1:
                try:
                    return g(*args, **kwargs)
                except TailRecursionException as e:
                    args = e.args
                    kwargs = e.kwargs
    func.__doc__ = g.__doc__
    return func

class Vector(tuple):
    def __add__(self, other):
        if not isinstance(other, tuple):
            raise TypeError
        return Vector(round(a + b, DIGITS) for a, b in zip(self, other))

    def __radd__(self, other):
        return self.__add__(other)

    def __sub__(self, other):
        return self.__add__(-1 * other)

    def __rsub__(self, other):
        return self.__sub__(other)

    def __mul__(self, other):
        if not isinstance(other, (int, float)):
            raise TypeError
        return Vector(round(elem * round(other, DIGITS), DIGITS) for elem in self)

    def __rmul__(self, other):
        return self.__mul__(other)

    def extend(self, item):
        return Vector(super().__add__((item, )))

    def concat(self, item):
        return Vector(super().__add__(item))


class Matrix:
    """
    :type _matrix: tuple[Vector]
    """
    def __init__(self, matrix):
        """
        :type matrix: list[list] | tuple[tuple]
        :return:
        """
        if not matrix:
            raise ValueError("Empty matrices are not supported")
        self._matrix = tuple(map(Vector, map(lambda row: map(float, row),
                                             matrix)))

    def __iter__(self):
        return iter(self._matrix)

    def __repr__(self):
        return "({})".format(",\n ".join(map(repr, self)))

    def __getitem__(self, item):
        """
        Returns a Vector if item is int or slice; returns a Matrix if item is
        tuple of slices
        :param item:
        :rtype: Matrix | Vector
        """
        if isinstance(item, (int, slice)):
            return self._matrix[item]
        elif typemap(item, (slice, slice)):
            row_slice, col_slice = item
            return Matrix(tuple(map(op.itemgetter(col_slice), self[row_slice])))

    def __mul__(self, coeff):
        if not isinstance(coeff, (int, float)):
            raise TypeError
        return Matrix(tuple(vector * coeff for vector in self))

    def __rmul__(self, coeff):
        return self.__mul__(coeff)

    @property
    def nrow(self):
        return len(self._matrix)

    @property
    def ncol(self):
        return len(self.row(0))

    def row(self, i):
        return self[i]

    def col(self, j):
        """
        :type j: int
        :return: tuple[tuple]
        """
        return Vector(self[i][j] for i in range(self.nrow))

    def _replace_row(self, i, replacement):
        new_matrix = tuple(self.row(_i) if _i != i else replacement
                           for _i in range(self.nrow))
        return Matrix(new_matrix)

    def permute(self, a, b):
        """
        Exchange rows a and b
        :type a: int
        :type b: int
        :rtype: Matrix
        """
        return self._replace_row(b, self.row(a))._replace_row(a, self.row(b))

    def multiply_row(self, i, coeff):
        return self._replace_row(i, self.row(i) * coeff)

    def normalise_row(self, i, j):
        coeff = 1 / (self[i][j]) if self[i][j] else 1
        return self.multiply_row(i, coeff)

    def add_rows(self, a, b, coeff=1):
        """
        :return: matrix': matrix'[b] = coef * matrix[a] + matrix[b]
        :rtype: Matrix
        """
        return self._replace_row(b, coeff * self.row(a) + self.row(b))

2 个答案:

答案 0 :(得分:1)

我在最后几步看到了一些非常大而非常小的数字;看起来你是舍入错误的受害者,不是产生0而是非常小的数字(以及它们非常大的反转)。

您需要设置一个(相对)截止值,低于该截止值时,数字被视为零(2e-15似乎是一个好的截止值)。找到适合您系统的epsilon:两个浮点数之间的最小差异,可以正确表示并且是1阶。

请检查normalise_row()等方法,以及

coeff = 1 / (self[i][j]) if self[i][j] else 1

以及其他一些我没有看到使用round()的地方。

另一种可能性是使用Python decimal模块。我对它没有多少经验,但也许它为你的计算提供了必要的精确度。

答案 1 :(得分:1)

我只是快速查看算法,它似乎正在做你期望的。真正的问题在于如何检测不一致的解决方案的“机制”。如果你看一下算法的第六步,你就得到了

 ((1.0, 0.0, -0.13043478260869557, -0.043478260869564966, 0.7391304347826085),
  (0.0, 7.666666666666668, -3.666666666666667, -6.333333333333334, 0.33333333333333304),
  (0.0, 0.0, -8.88e-16, -1.776e-15, -0.9999999999999994))

请注意,与其他数字相比,第三行的第3和第4项非常小。事实上,他们只是完成错误。如果你用精确的数字进行减法,那么第二列会给你46/3 - 2 * 23/2 = 0(你得到的),但是第三列会给你-22 / 3 - 2 *( - 11 / 3)= 0,因为你没有得到。第四列也是如此。然后在下一个阶段,通过将这些零“缩放”到1和2来复合错误,并且从那里开始都将出错。

你有两个选择 - 要么用精确的数字进行计算(我不知道python是否可以做到这一点,但是,例如,在Scheme中有精确的分数不会产生这个问题 - 也许你不得不滚动你自己......) - 或者,使用足够高精度的算术并任意设置小于某个选定值(最好基于原始矩阵中数字的大小)的任何值等于零。如果矩阵确实有一个数量上有各种变化的解决方案,那么这将失败,但在“现实世界”的问题中,它应该解决你遇到的问题。