# algorithms / piece_line_interpolation.py

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38``` ```# -*- coding:utf-8 -*- from decimal import Decimal 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()) for z in points_to_compute: if z in x_list: print('x=%f, f(x)=%f' % (z, get_f_x(z, x_list, f_x_list))) continue try: lower_x = filter(lambda i: i < z, x_list)[-1] except IndexError: print('No lower border found') continue try: upper_x = filter(lambda i: i > z, x_list)[0] except IndexError: print('No upper border found') continue lower_f = get_f_x(lower_x, x_list, f_x_list) upper_f = get_f_x(upper_x, x_list, f_x_list) k = (upper_f - lower_f) / (upper_x - lower_x) f_z = k * (z - lower_x) + lower_f print('x=%f, f(x)=%f' % (z, f_z)) ```