Advertisement
KingNeil

Polynomial Through Points

Aug 21st, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. import numpy as np
  2. from numpy.linalg import inv
  3. import sys
  4. import math
  5.  
  6. #Gets a valid input an returns it as a list of tuples
  7. def get_input():
  8.     #The message that wil be displayed to the inputter
  9.     print('Type \'exit\' to exit')
  10.     input_message = 'Write you coordinates: '
  11.     #The characters of the input to be replaced, and the characters they should be replaced with
  12.     replace_chars = [(' ', ''), ('(', ''), (')', ' '), (',', ' ')]
  13.     #Repeatedly gets the input, until it is valid
  14.     while True:
  15.         s = input(input_message)
  16.         if (s.lower() == 'exit'):
  17.             sys.exit()
  18.         #Replaces the replace_chars with nothing (deletes them)
  19.         for char_pair in replace_chars:
  20.             s = s.replace(char_pair[0], char_pair[1])
  21.         #Replaces the ',' that split the coordinates, with a space
  22.         s = s.replace(',', ' ')
  23.         #Splits the string into words
  24.         s_list = s.split()
  25.  
  26.         #Is the input valid?
  27.         is_valid = validate_if_float(s_list) and (len(s_list) % 2 == 0)
  28.         #If yes, the function should return the input
  29.         if (is_valid):
  30.             #Initializes the result list
  31.             result = []
  32.             #Loops through the input list, with iterates of two
  33.             for i in range(0, len(s_list), 2):
  34.                 #Appends a touple of the coordinate pair to the result list
  35.                 result.append((float(s_list[i]), float(s_list[i + 1])))
  36.             return result
  37.  
  38.         input_message = 'That input is invalid. Try again: '
  39.            
  40.  
  41. #Validates if string every entry of a list can be converted to a float
  42. def validate_if_float(number_list):
  43.     is_all_numbers = True
  44.     for n in number_list:
  45.         if not is_number(n):
  46.             is_all_numbers = False
  47.             break
  48.     return is_all_numbers
  49.  
  50. def is_number(s):
  51.     try:
  52.         float(s)
  53.         return True
  54.     except ValueError:
  55.         return False
  56.  
  57.  
  58. def create_matrix (coordinates):
  59.     matrix_list = []
  60.     for i in range(len(coordinates)):
  61.         matrix_list.append(list())
  62.         for j in range(len(coordinates)):
  63.             num = math.pow(coordinates[i][0], j)
  64.             matrix_list[i].append(num)
  65.  
  66.     return np.matrix(matrix_list)
  67.  
  68. def create_vector (coordinates):
  69.     vector_list = []
  70.     for i in range(len(coordinates)):
  71.         vector_list.append([coordinates[i][1]])
  72.  
  73.     return np.matrix(vector_list)
  74.  
  75. def calculate_coefficients (A, b):
  76.     v = inv(A) * b
  77.     return v
  78.  
  79. def main ():
  80.     coordinates = get_input()
  81.     A = create_matrix(coordinates)
  82.     b = create_vector(coordinates)
  83.     v = calculate_coefficients (A, b)
  84.  
  85.     formula = 'f(x) = '
  86.     for i in range(len(v)):
  87.         formula = formula + str(float(v[i][0])) + ' * x^' + str(i) + ' + '
  88.     formula = formula[:len(formula)-3]
  89.    
  90.     print(formula)
  91.     input()
  92.    
  93.  
  94. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement