# Commits

committed b1b5b6a

added check for Determinator != 0

# common.py

` from decimal import Decimal`
` `
` `
`+class NoSolutionException(Exception):`
`+    pass`
`+`
`+`
` def get_equations():`
`     in_str = True`
`     matrix = []`
`     while in_str:`
`-        in_str = raw_input(u'Введите аргументы уравнения через пробел, пустая строка для окончания: ')`
`+        in_str = raw_input(u'Args: ')`
`         if in_str:`
`             matrix.append(map(Decimal, in_str.split()))`
`     return matrix`
` `
` if __name__ == "__main__":`
`     import doctest`
`-    doctest.testmod()`
`+    doctest.testmod()`
`+`
`+`
`+def determinant(m):`
`+    """`
`+    Вычисляет Определитель для 2 или 3-мерной матрицы`
`+`
`+    >>> determinant([[2, 4], [3, 3]])`
`+    -6`
`+    >>> determinant([[7, 4], [1, 3]])`
`+    17`
`+    >>> determinant([[2, 3, 1], [2, 0, 4], [4, -1, 1]])`
`+    48`
`+    >>> determinant([[2, 3, 1], [1, 0, 4], [3, -1, 1]])`
`+    40`
`+    >>> determinant([[2, 2, 1], [2, 1, 4], [4, 3, 1]])`
`+    8`
`+    >>> determinant([[2, 3, 2], [2, 0, 1], [4, -1, 3]])`
`+    -8`
`+    """`
`+    if len(m) == 2:`
`+        return m[0][0] * m[1][1] - m[0][1] * m[1][0]`
`+    else:`
`+        return m[0][0] * m[1][1] * m[2][2] +\`
`+               m[2][0] * m[0][1] * m[1][2] +\`
`+               m[0][2] * m[1][0] * m[2][1] -\`
`+               m[2][0] * m[1][1] * m[0][2] -\`
`+               m[0][0] * m[2][1] * m[1][2] -\`
`+               m[1][0] * m[0][1] * m[2][2]`

# gauss.py

` # -*- coding:utf-8 -*-`
` from copy import deepcopy`
` from decimal import Decimal`
`-from common import get_equations, print_matrix`
`+from common import get_equations, print_matrix, determinant, NoSolutionException`
` `
` `
` def swap_lines(matrix, line_number1, line_number2):`
`     """`
`     m = deepcopy(matrix)`
` `
`+    _determinant = determinant(m)`
`+    if _determinant == 0:`
`+        raise NoSolutionException()`
`+`
`     # матрица:`
`     # * * *`
`     # * * *`
` if __name__ == '__main__':`
`     matrix = get_equations()`
` `
`-    x_list = gauss(matrix)`
`-    for i, x in enumerate(x_list):`
`-        print('x%s = %f' % (i, x))`
`+    try:`
`+        x_list = gauss(matrix)`
`+    except NoSolutionException:`
`+        print 'Many or no solutions'`
`+    else:`
`+        for i, x in enumerate(x_list):`
`+            print('x%s = %f' % (i, x))`
` `

# krammer.py

`-#!/bin/env python`
` # -*- coding:utf-8 -*-`
` from copy import deepcopy`
` from decimal import Decimal`
`-from common import get_equations`
`+from common import get_equations, determinant, NoSolutionException`
` `
` `
` def replace_column(m, column_number):`
`     """`
`-    Заменяет элементы столбца с индексом column_number на элементы последнего столбца`
`+    Заменяет элементы столбца с индексом column_number на элементы`
`+    последнего столбца`
`     """`
`     out_matrix = deepcopy(m)`
`     for line in out_matrix:`
`     return out_matrix`
` `
` `
`-def determinant(m):`
`-    """`
`-    Вычисляет Определитель для 2 или 3-мерной матрицы`
`-`
`-    >>> determinant([[2, 4], [3, 3]])`
`-    -6`
`-    >>> determinant([[7, 4], [1, 3]])`
`-    17`
`-    >>> determinant([[2, 3, 1], [2, 0, 4], [4, -1, 1]])`
`-    48`
`-    >>> determinant([[2, 3, 1], [1, 0, 4], [3, -1, 1]])`
`-    40`
`-    >>> determinant([[2, 2, 1], [2, 1, 4], [4, 3, 1]])`
`-    8`
`-    >>> determinant([[2, 3, 2], [2, 0, 1], [4, -1, 3]])`
`-    -8`
`-    """`
`-    if len(m) == 2:`
`-        return m[0][0] * m[1][1] - m[0][1] * m[1][0]`
`-    else:`
`-        return m[0][0] * m[1][1] * m[2][2] +\`
`-               m[2][0] * m[0][1] * m[1][2] +\`
`-               m[0][2] * m[1][0] * m[2][1] -\`
`-               m[2][0] * m[1][1] * m[0][2] -\`
`-               m[0][0] * m[2][1] * m[1][2] -\`
`-               m[1][0] * m[0][1] * m[2][2]`
`-`
`-`
` def krammer(_matrix):`
`     """`
`     >>> m = [[Decimal(2), Decimal(3), Decimal(1), Decimal(2)],`
`     """`
`     _determinant = determinant(_matrix)`
`     if _determinant == 0:`
`-        raise Exception(u'Система не имеет ни одного или имеет бесконечное множество решений')`
`+        raise NoSolutionException()`
`     print('D = %f' % _determinant)`
`     return [determinant(replace_column(_matrix, l)) / _determinant for l in range(len(_matrix))]`
` `
` if __name__ == '__main__':`
`     matrix = get_equations()`
` `
`-    x_list = krammer(matrix)`
`+    try:`
`+        x_list = krammer(matrix)`
`+    except NoSolutionException:`
`+        print 'Many or no solutions'`
`     for i, x in enumerate(x_list):`
`         print('x%s = %f' % (i, x))`
` `