Commits

метод наименьших квадратов

krammer.py

`     ... [Decimal(2), Decimal(0), Decimal(4), Decimal(1)],`
`     ... [Decimal(4), Decimal(-1), Decimal(1), Decimal(3)]]`
`     >>> krammer(m)`
`-    D = 48.000000`
`     [Decimal('0.8333333333333333333333333333'), Decimal('0.1666666666666666666666666667'), Decimal('-0.1666666666666666666666666667')]`
`     """`
`     _determinant = determinant(_matrix)`
`     if _determinant == 0:`
`         raise NoSolutionException()`
`-    print('D = %f' % _determinant)`
`+    # print('D = %f' % _determinant)`
`     return [determinant(replace_column(_matrix, l)) / _determinant for l in range(len(_matrix))]`
` `
` `

smallest_squares.py

`+# -*- coding:utf-8 -*-`
`+from decimal import Decimal`
`+from krammer import krammer`
`+`
`+`
`+def get_f_x(x, xx, ff):`
`+    return dict(zip(xx, ff))[x]`
`+`
`+if __name__ == '__main__':`
`+    x_list = map(Decimal, raw_input('Enter x values: ').split())`
`+    # x_list = map(Decimal, '0 2 3 3.5'.split())`
`+    f_x_list = map(Decimal, raw_input('Enter f(x) values: ').split())`
`+    # f_x_list = map(Decimal, '-1 0.2 0.5 0.8'.split())`
`+    points_to_compute = map(Decimal, raw_input('Enter points (Z): ').split())`
`+    # points_to_compute = map(Decimal, '1 3.2'.split())`
`+`
`+    sum_x = sum(x_list)`
`+    sum_x_squares = sum([pow(x, 2) for x in x_list])`
`+    sum_f_x = sum([f * x for f, x in zip(x_list, f_x_list)])`
`+    sum_f_x_squares = sum([pow(f * x, 2) for f, x in zip(x_list, f_x_list)])`
`+    sum_f = sum(f_x_list)`
`+    sum_x_cube = sum([pow(x, 3) for x in x_list])`
`+    sum_x_4th = sum([pow(x, 4) for x in x_list])`
`+`
`+    for z in points_to_compute:`
`+        # m = 1`
`+        matrix1 = [`
`+            [Decimal(len(x_list)), sum_x, sum_f],`
`+            [sum_x, sum_x_squares, sum_f_x]`
`+        ]`
`+`
`+        a0, a1 = krammer(matrix1)`
`+        # y = a0 + a1*x`
`+        f1 = a0 + a1 * z`
`+`
`+        S1 = sum([pow(a0 + a1 * xi - fi, 2) for xi, fi in zip(x_list, f_x_list)])`
`+`
`+        print('m=1, x=%f, f(x)=%f, S(a0, a1)=%f' % (z, f1, S1))`
`+`
`+        # m = 2`
`+        matrix2 = [`
`+            [Decimal(len(x_list)), sum_x, sum_x_squares, sum_f],`
`+            [sum_x, sum_x_squares, sum_x_cube, sum_f_x],`
`+            [sum_x_squares, sum_x_cube, sum_x_4th, sum_f_x_squares]`
`+        ]`
`+        aa0, aa1, aa2 = krammer(matrix2)`
`+        # y = a0 + a1*x + a2*x*x`
`+        f2 = aa0 + aa1 * z + aa2 * z * z`
`+`
`+        S2 = sum([pow(aa0 + aa1 * xi + aa2 * xi * xi - fi, 2) for xi, fi in zip(x_list, f_x_list)])`
`+        print('m=2, x=%f, f(x)=%f, S(a0, a1, a2)=%f' % (z, f2, S2))`
`+        print('-' * 20)`